Reputation: 2679
I am using Eclipse as my IDE. I am reading in a simple file:
1 Andrew
2 Peter
3 Andrew
3 Peter
3 Andrew
My problem is that sc.next() on the last line for Andrew hangs and never finishes. Is there a way to have it take in Andrew on the last line and then exit the while loop? I believe standard in is waiting for more input instead of just grabbing Andrew and saying i've got what i need and moves on.
Here is the part of the code that i am having trouble with
sc = new Scanner(System.in);
while(sc.hasNextLine()){
String number = sc.next();
String name = sc.next();
int numberI = Integer.parseInt(number);
Usage usage = new Usage(name);
computerNames[numberI].addObsseration(usage);
}
sc.close();
Thanks in advance to any help.
Upvotes: 0
Views: 675
Reputation: 7920
You're using hasNextLine()
mixed with next()
. You should use hasNext()
when you're grabbing data with next()
. Additionally, you should know that you can combine the parsing of the integer into the same step by using sc.nextInt()
instead of sc.next()
to get an integer.
sc.nextLine()
gobbles up everything until the next newline. sc.next()
takes the next whitespace-separated token. nextLine()
may have another newline available even though we've already captured all the tokens.
You may have to press Control+D after typing the input if you're not reading from a file but typing the data at the command line, to simulate the end of the file.
Upvotes: 1
Reputation: 442
It is often better to read a file, and parse it later (rather than at the same time). An example would be as such:
sc=new Scanner(System.in);
String input="";
while((input=sc.nextLine())!=null) {
String[] arr=input.split(" ");
int numberI=Integer.parseInt(arr[0]);
Usage usage=new Usage(arr[1]);
computerNames[numberI].addObsseration(usage);
}
sc.close();
Upvotes: 0
Reputation: 4052
You are reading in input, not from a file, but from standard in. Because of this, unless you close the standard input stream into the running java process, the Scanner
will always think that there is more input available.
If you do manage to close your standard input stream, your code will also fail if there is a trailing newline. You should use hasNext()
instead of hasNextLine()
to combat this problem.
If you want to read from a stream, I would recommend using a BufferedReader
on an InputStreamReader
with readLine()
and split()
. This will not only solve your problem but also make the input faster.
Upvotes: 0
Reputation: 638
Not a big IO guy, but from what I remember in school (using C, not Java), often times we would put an escape at the end of a file (in your case, the last line could be "0 EOF") and then when number == 0 and name.equals("EOF"), you know to break out of the loop.
Edit: Jakob seems to have the appopriate to answer why this is happening tho.
Upvotes: 1
Reputation: 41
Your Scanner is reading from standard in (System.in
) rather than from your file (/YourComputerPath/.../filename
). You need to change your instantiation of the scanner.
sc = new Scanner(new File("/PathToMyFile/.../myfile"));
Upvotes: 0