我需要帮助解析我的输入文件,我应该使用arraylist吗?

时间:2016-02-17 23:43:00

标签: java arrays parsing arraylist input

使用Java,我使用扫描仪读取输入文件,我能够读取和打印输入文件。但我现在很难解析它。

示例输入文件如下:

height 6

weight 120

name john

team played for team1 start 2010 end 2012

team played for team1 start 2010 end 2012

team played for team2 start 2013 end 2015

我该如何解析这些信息。目前我正在按行扫描输入,如:

while (scanner.hasNext()) {
    String line = scanner.nextLine();
    System.out.println(line);
}
我会做类似的事吗

int height = height.nextInt();
int weight = weight.nextInt();
String name = name.nextLine();

现在我坚持使用最后3行,假设前3行被正确解析

2 个答案:

答案 0 :(得分:0)

正如@pshemo所说,我错过了添加line = line.replace(..)

int height = 0;
int weight = 0;
String name = "";

while (scanner.hasNextLine())   // use hasNextLine()  
{

 String line = scanner.nextLine();

 if(line.startWith("height "))  // do the same for weight 
 {
    line = line.replace("height ", "");
    try
    {
      height = Integer.parseInt(line);
    }
    catch(NumberFormatException e1)
    {

    }
 }

 else if(line.startWith("weight "))  // do the same for weight 
 {
   line = line.replace("weight ", "");
   try
   {
     weight = Integer.parseInt(line);
   }
   catch(NumberFormatException e1)
   {

   }
 }

 else if(line.startsWith("name "))
 {
   line = line.replace("name ", "");
   name = line;
 }

 else if(line.startsWith("team played for "))
 {
   line = line.replace("team played for ", "");

   String teamName = "";
   int i = 0;
   while(line.charAt(i) != ' ')
   {
      teamName = teamName + line.charAt(i); // copying the team name till space
      i++;
   }
   line = line.replace(teamName+" ","");
   // now we will be left with a string that contains start year 1 end year 2

   line = line.replace("start ", "");

   i = 0; // using the same i variable

   String startYear = "";

   while(line.charAt(i) != ' ')
   {
      startYear = startYear+ line.charAt(i);
      i++
   }
   line = line.replace(startYear+" end ", "");

   String endYear = line;
    // just make this as global variables if u need to use them in a different method also if you need to parse string to integer use  Interger.parseInt(String)

  }

  System.out.println("Name :-" + name);
  System.out.println("Weight :-" + weight);
  System.out.println("Height :-" + height);

}

// by now all the data has been stored to their respective variables

答案 1 :(得分:0)

我认为这个解决方案更优雅一点。

顺便说一句,我真的不知道你想对这些信息做什么,所以我就把它留下来了。您可以创建一个对象或只将其打印到控制台:

while (scanner.hasNext()) {

        scanner.next(); // skip "height"
        int height = scanner.nextInt(); // get height
        scanner.next(); // skip "weight"
        int weight = scanner.nextInt(); // get weight
        scanner.next(); // skip "name"
        String name = scanner.next(); // get name

        while (scanner.hasNext("team")) {
            // parse the "team info" similarly, as shown above.
            // make sure that you parse it correctly, so that
            // in the next inner while loop, the scanner.hasNext("team")
            // will be positioned just before the next line of "team"
            // or a next line that containes a new record.
        }


    }