Search code examples
javajava.util.scannerjava-6

Using Scanner in Java to parse a text file or console input and ran into errors


I am trying to read in values from a file separated by white space between the individual items and carriage returns.

So the text file is something like

1 lunch box 3.99
2 jelly donuts 1.99
4 cases of beer 36.99
2 chess boards 24.99

With these values I am trying to construct objects... So the object will be made up of the quantity, the first thing on the line, a name - the second thing on the line, and a price the last thing on the line. So the constructor is like:

product(int x, String name, float price) { 
 this.x = x;
 this.name = name;
 this.price = price;
}

My problem is the way I have coded the scanner is running into trouble when trying to parse everything. So I receive a java.util.InputMismatchException. If I use an algorithm like:

if (scannerRef.hasNextInt()) 
  arraylistRef.intThing = scannerRef.nextInt();

if (scannerRef.hasNext())
   arraylistRef.name = scannerRef.next();

if (scannerRef.hasNextFloat())
   arrayListRef.price = scannerRef.nextFloat();

then the code the way I have it puts each individual primitive or String in its own object instead of looping through an entire line then adding it to an object

I hope this makes sense. Thanks for reading if you are reading this now.

the main part of my code I am concerned about is this: the way I have it here with the above type of algorithms commented out puts everything in to the same object but runs into the java.util.InputMismatchException dingo brains

// loop while there is text to read
while (s1.hasNextLine()) {
    Product p = new Product(p.quantity = s1.next);
  //if (s1.hasNextInt())
     p.quantity = s1.nextInt();


//  else 
     p.name = s1.next(); // to get strings

     // else if(s1.hasNextFloat())
     p.price = s1.nextFloat();

 // now add the line to the product arraylist
 foo.add(p);

}

Solution

  • You want to take the file data line by line into a String (whatever Collection type you prefer) and then split it wherever you find white space and store the data accordingly. To take in the whole line you'd use:

    nextLine();
    

    That way you'd get a String say "1 lunch box 3.99". You would then split this wherever you find white space and store 1 as an int, lunch and box as Strings, and 3.99 as a double (using doubles for monies is not recommended).

    You can search for useful methods here: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html For example, the split() method.

    Here's an example on how to use split():

    Let's say your input line is 1 lunch box 3.99. You have already read it into a String as follows:

    String example = "1 lunch box 3.99"
    

    To split this String at white space using the split() method, you'd do the following:

    String[] splitString = example.split(" ");
    

    This will give you an array of String with the following values:

    {"1", "lunch", "box", "3.99"};
    

    Then you can parse these values into their proper type using things like Integer.parseInt, Double.parseDouble, etc.