-3

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).

  • Possible duplicate of [How to get out of while loop in java with Scanner method "hasNext" as condition?](http://stackoverflow.com/questions/10490344/how-to-get-out-of-while-loop-in-java-with-scanner-method-hasnext-as-condition) – Tom Apr 10 '17 at 15:47
  • My apologies, I used another version of the code in which I tried to go around hasNextLine(). I also saw the thread you mentioned, and tried to implement that solution but it didn't work, leading me to suspect the issue may vary. – Ryan Schron Apr 10 '17 at 15:54
  • And how does *"tried to implement that solution"* look like? – Tom Apr 10 '17 at 16:07
  • Edited for clarity. – Ryan Schron Apr 10 '17 at 16:13
  • Works fine for me (both, using console and using a file with `java [classname] < source.txt`). May provide a [mcve] that reproduces your issue. – Tom Apr 10 '17 at 16:21

1 Answers1

0

Yours code seems to work well. Though you can check for hasNextLine as well check for some other condition if EOF is customized (As in yours case there is -1). You may catch some exception as well if yours code is prone to them. Below code worked well for me.

        Scanner scanner = new Scanner(new File("/opt/test.txt"));
        int secondValue = 0;
        double lastValue = 0;
        int firstValue = 0;
        int LineNumber = 0;
        while ((scanner.hasNextLine()) ) {
            try {

                String[] arr = scanner.nextLine().trim().split("\\s+");

                firstValue = Integer.parseInt(arr[0]);
                secondValue = Integer.parseInt(arr[1]);
                lastValue = Double.parseDouble(arr[2]);
                doSomeThing(firstValue,secondValue,lastValue);

                LineNumber++;
            } catch (Exception excep) {

            }
        }
        System.out.println(LineNumber);
}
Anuj Singh
  • 11
  • 4