0

Using Java, im reading in an input file using scanner and i am able to read and print out the input file. But i am having difficulty parsing it now.

The example input file is as follows :

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

how do i go about parsing this information. currently im scanning the input by line like:

while (scanner.hasNext()) {
    String line = scanner.nextLine();
    System.out.println(line);
}

would i do something like

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

and now im stuck with the last 3 lines, assuming the first 3 lines were parsed correctly

Alexander Mikhalchenko
  • 4,315
  • 3
  • 28
  • 54
dre
  • 25
  • 4
  • 1
    Be careful with using [`nextLine()` after other `nextSomething` methods](https://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods). – Pshemo Feb 17 '16 at 23:46
  • 1
    Anyway we can't really help you without knowing what are you trying to achieve. – Pshemo Feb 17 '16 at 23:48
  • i want to read the input file and save each of the contents that i need in their respected variables. (height, weight, name). and i want to save the lines that say "team played for STRING start INT end INT" the same way. for ex: String teamPlayed = ""; int start = ""; , and int end = "". – dre Feb 17 '16 at 23:54

2 Answers2

0

As Stated by @pshemo i missed out to add 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
  • thank you for your response, and i understand what your doing. definitely helps, but how do i go about doing the "team played for..." lines. keeping in mind i can have an x amount of entries of those lines. assuming i would do a while loop? – dre Feb 17 '16 at 23:58
  • @dre Are you familiar with regex? – Pshemo Feb 17 '16 at 23:59
  • @dre the while loop runs till all data has been read from the file if you need to get the years of the team as you might have noticed the common strings aming them is "team played for team1 start " and " end " so if you replace those two you will be left with a string that contains 8 characters of which the 1st four will be start year and second four will be the end year, ill edit the answer with oen for the team too in a minute –  Feb 18 '16 at 00:00
  • im going to give it a try and ill let you guys know how it goes! thanks guys – dre Feb 18 '16 at 00:04
  • 1
    @NullSaint `line.replace("team played for ", "");` will change nothing (strings are immutable, so `replace` doesn't affect `line` but creates new string based on line). You may be looking for `line = line.replace(...)`. – Pshemo Feb 18 '16 at 00:14
  • hey guys, Integer.parseInt(line); doesnt seem to be working. any ideas ? – dre Feb 18 '16 at 01:01
  • the try catch is catching an error. the parseint function doesn't seem to be working. idk why – dre Feb 18 '16 at 03:10
0

I think that this solution is a bit more elegant.

BTW, I don't really know what do you want to do with the information so I've just left It as is. You can create an object or just print it to console:

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.
        }


    }
MaxG
  • 962
  • 2
  • 12
  • 25