tags:

views:

370

answers:

2

On my computer programming class in college, we were asked to create a Java program to read and store various elements on a list. Storing is not a problem, but I'm having some issues regarding the reading of the values. I'm using "variable = input.nextLine();" (if the variable is String), and the problem is that when I read several values in a row, sometimes the program will just skip past the reading one of them. My teacher recommended me to use input.nextLine() to fix that, but it's not working perfectly, and it seems like a workaround to me. I believe the problem is buffer-related. Is there something similar to C's fflush or fpurge functions on Java?

+1  A: 

With a few exceptions, the input I/O classes don't have a flush method equivalent and shouldn't need one. The output I/O classes do have flush methods. You can see this at the JDK 6 Javadoc index for F and scrolling down the the collection of flush() methods.

Are you sure that you're not accidentally reading input.nextLine(); twice in a row and thus discarding one line?

We need to know more information before we can help solve your problem.

NOTE: Please edit your question to add additional information. Look for a small "edit" "button" below your question.

Eddie
A: 

If you are reading from a file, you could use a BufferedReader:

    BufferedReader reader = new BufferedReader(new FileReader("somelines_oh_yeah.txt"));
    String line;
    while((line = reader.readLine()) != null){
          //do something.
    }
Tom