I've seen similar topics around but can't seem to piece together a working solution. I am writing a program that (in part) reads values from stdin to perform some operations on. I am, however, intending to redirect the input from a text file at run time, so I can't just issue an EOF using Ctrl+Z (windows).
String s;
int v2 = 0;
double w = 0;
int v1 = 0;
int i = 0;
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
s = sc.nextLine();
String[] arr = s.trim().split("\\s+");
v1 = Integer.parseInt(arr[0]);
v2 = Integer.parseInt(arr[1]);
w = Double.parseDouble(arr[2]);
i++;
}
System.out.println(i);
sc.close();
I can never seem to get to the final print statement, as (my guess anyway) it seems hasNextLine() is indefinitely blocking.
Here is an example of the format of the input file:
0 1 5.0
1 2 5.0
2 3 5.0
3 4 5.0
4 5 5.0
12 13 5.0
13 14 5.0
14 15 5.0
...
-1
It is always in the format of int int double, but the spacing between can vary, hence the string operations. It will also always terminate with a -1.
I've tried the following to no avail
String s;
int v2 = 0;
double w = 0;
int v1 = 0;
int i = 0;
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
s = sc.nextLine();
String[] arr = s.trim().split("\\s+");
v1 = Integer.parseInt(arr[0]);
if(v1 < 0){
break;
}
v2 = Integer.parseInt(arr[1]);
w = Double.parseDouble(arr[2]);
i++;
}
System.out.println(i);
sc.close();
I've left out most the code that isn't relevant to avoid clutter. I can post if needed.
Thanks for taking a look.
EDIT: Edited for clarity. Also noticed that it is hanging on last line of input file regardless of line content (that is, valid lines hanging). I am redirecting via eclipse (Neon).