13 Replies - 11214 Views - Last Post: 07 October 2010 - 12:12 AM Rate Topic: -----

#1 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 06:32 AM

The problem I am having is that I read in from a file using
 File file = new File(fileName);
     Scanner input = new Scanner(file);
     //input.useDelimiter("\n");   
    
     double c;
     int q;
     String d ;
     
         while(input.hasNextLine()){               
          
         c = input.nextDouble(); //get the double value cost
         
         q = input.nextInt();  //get the integer value quantity
         
         d = input.nextLine(); //get the String description
         
        
         items[countItems] = new Item(d,c,q); 
        
         countItems++;        
      
     }


and it creates an array of Item class objects, then I can use a copy constructor and print the array and it's fields.

But when I want to add to the array, a new Item, I create the item
Item add = new Item(d,c,q);

and I want to check if there is already an item that matches the description of the new item
for(int i = 0; i<countItems; i++){
              
              s1 = add.getDescription();
              s2 = it[i].getDescription();
              System.out.println(s1);
              System.out.println(s2);  
                          
              if(s1.equalsIgnoreCase(s2))
                  System.out.println("There is a match");
              else    
                  System.out.println("No match found");                       
        }


When I print to the screen I see the string in s2 has a few space before it, therefore I never get a match on the s1 and s2 descriptions. I believe this is caused from using
 q = input.nextInt();  //get the integer value quantity
         
         d = input.nextLine(); //get the String description

but I'm not sure how to get around it.

Is This A Good Question/Topic? 0
  • +

Replies To: Scanner read nextLine() after nextInt()

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10650
  • View blog
  • Posts: 39,559
  • Joined: 27-December 08

Re: Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 06:47 AM

Can you post a sample of the data in the File as well?
Was This Post Helpful? 1
  • +
  • -

#3 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 06:53 AM

When you use input.nextInt() it is left in the buffer a linefeed ('\n'). Then when you use input.nextLine() this method return that character and the execution of the program continue. So you can help that placing an input.nextLine() after the input.nextInt() in order to get rid the '\n'.
q = input.nextInt();
input.nextLine();
d = input.nextLine();


Was This Post Helpful? 1
  • +
  • -

#4 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

Re: Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 07:04 AM

View Postmacosxnerd101, on 04 October 2010 - 05:47 AM, said:

Can you post a sample of the data in the File as well?

2.10 2 Apples
3.65 1 Peaches
Was This Post Helpful? 0
  • +
  • -

#5 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

Re: Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 07:10 AM

View Postguidojavier, on 04 October 2010 - 05:53 AM, said:

When you use input.nextInt() it is left in the buffer a linefeed ('\n'). Then when you use input.nextLine() this method return that character and the execution of the program continue. So you can help that placing an input.nextLine() after the input.nextInt() in order to get rid the '\n'.
q = input.nextInt();
input.nextLine();
d = input.nextLine();



Adding this to the code
q = input.nextInt();
input.nextLine();
d = input.nextLine();


messes up how the Strings are read in, i.e. with this extra line(input.nextLine(); ) to clear the buffer, the Scanner skips every second String entry in the file.
The file reads like this

5.33 6 Apples
6.98 1 Coco pops
5.87 9 Beer
5.78 4 Cheetos
0.50 6 Candy
1.70 1 Steak

with the line to clear the buffer the output is
5.33 6 6.98 1 Coco pops
5.87 9 5.78 4 Cheetos
0.50 6 1.70 1 Steak

This post has been edited by dunsta: 04 October 2010 - 07:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10650
  • View blog
  • Posts: 39,559
  • Joined: 27-December 08

Re: Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 07:16 AM

It is probably safer to read in each line, then parse it.
String temp = input.nextLine();
String[] parse = temp.split(" ");

Item i = new Item(Double.parseDouble(parse[0]), Integer.parseInt(parse[1]), parse[2]);


Was This Post Helpful? 1
  • +
  • -

#7 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 07:23 AM

Ok, sorry what I said just apply when the text is enter by an user from the console. How is created the 'it' array? because is there where the description string have a blank space. Althought you might get rid of it using the trim() method.
Was This Post Helpful? 1
  • +
  • -

#8 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

Re: Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 07:32 AM

View Postguidojavier, on 04 October 2010 - 06:23 AM, said:

How is created the 'it' array? because is there where the description string have a blank space. Althought you might get rid of it using the trim() method.

The "it" array is created using a copy constructor from the Item class, it is just a copy of the items array created as the file data is read in;
items[countItems] = new Item(d,c,q);


//new array for deep copy
          Item it[] = new Item[countItems];
          
          //use Item copy constructor to make copy of items array 
          for(int i = 0; i<countItems; i++){
              it[i] = new Item(items[i]);
              }

Was This Post Helpful? 0
  • +
  • -

#9 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

Re: Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 07:50 AM

View Postmacosxnerd101, on 04 October 2010 - 06:16 AM, said:

It is probably safer to read in each line, then parse it.
String temp = input.nextLine();
String[] parse = temp.split(" ");

Item i = new Item(Double.parseDouble(parse[0]), Integer.parseInt(parse[1]), parse[2]);



I don't know how to do any of that yet macMan. I have tried to write a program to read the file with bufferedReader and FileReader, but I think we can only read a whole line at a time, not individual parts of it, i.e. double cost String
is this true? the only way to read a 'data type' at a time, from a file, is with the Scanner class?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10650
  • View blog
  • Posts: 39,559
  • Joined: 27-December 08

Re: Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 07:58 AM

If you want to read the widest number of different types, then Scanner is the tool. I'm still using Scanner to read in the line though. I provided an example to demonstrate how I parse the line to help you out some with that. :)
Was This Post Helpful? 0
  • +
  • -

#11 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

Re: Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 08:35 AM

do you mean like this:
I had to change the order around becasue my constructor takes parameters String, double, int -- in that order. I assume parse[2] is the String, so I moved it to the first argument

while(input.hasNextLine()){ 
            String temp = input.nextLine();
            String[] parse = temp.split(" ");
 
            //Item i = new Item(Double.parseDouble(parse[0]), Integer.parseInt(parse[1]), parse[2]);
            items[countItems] = new Item(Double.parseDouble(parse[0]), Integer.parseInt(parse[1]), parse[2]);
        
        
         
         countItems++; //increase the amount of items read in by Scanner
         
      
     }


the code compiles, but I get an error
Exception in thread "main" java.lang.NumberFormatException:empty String
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 03:34 PM

Some line that you are reading has an empty String. I suggest printing out the parse array as you go along, so that you can see what it's failing on. You might have a double space or a missing value in there somewhere where Integer.parseInt() will return that.
Was This Post Helpful? 1
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Scanner read nextLine() after nextInt()

Posted 04 October 2010 - 04:20 PM

View Postdunsta, on 04 October 2010 - 09:35 AM, said:

the code compiles, but I get an error
Exception in thread "main" java.lang.NumberFormatException:empty String

Probably an empty line at the end of your file
Check the length() of temp before splitting it
Was This Post Helpful? 1
  • +
  • -

#14 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

Re: Scanner read nextLine() after nextInt()

Posted 07 October 2010 - 12:12 AM

View Postguidojavier, on 04 October 2010 - 06:23 AM, said:

Although you might get rid of it using the trim() method.


The trim() method is what I was looking for.
Thanks all for the input.

I decided to go with trim(), and not with macosxnerds' idea because that code is a little ahead of me (for the moment) but I have taken onboard the idea and will, revisit this thread when I am better versed in text processing. It's the next chapter anyway, so not too far off.

This post has been edited by dunsta: 07 October 2010 - 12:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1