'Java read pasted input from consol and then stop

I'm currently trying to solve following problem on onlinge judge: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=310.

I wonder how to determine when the program should exit, in other words when I should stop the input loop and exit the program?

Sample code:

public static void main(String[] args) 
{   
    //Something here

    Scanner in = new Scanner(System.in);
    while(?) //How do I determine when to end?
    {
        //Doing my calculation
    }
}

My only idea is to make the input reader stop when all input has been pasted in the console, but I've no idea how I will do that.



Solution 1:[1]

maybe this would help: http://uva.onlinejudge.org/data/p100.java.html

its a sample Java code from Online Judge, you can customize void Begin() and do your calculation there

Solution 2:[2]

if the input is System.in I would do this:

Scanner s = new Scanner(System.in);

int r, b, p, m;

while (true) {
    b = Integer.parseInt(s.nextLine());
    p = Integer.parseInt(s.nextLine());
    m = Integer.parseInt(s.nextLine());

    r = doYourWoodooMagic(b, p, m);

    System.out.println(r);

    s.nextLine(); //devour that empty line between entries
}

So one question: why "devour" that empty line AFTER printing r? easy answer: after the last set of three numbers, there probably will not be any lines at all, so s.nextLine(); will get stuck forever.

I dont know about UVa Online Judge, but i made similar program that terminated your program after getting the right output, so this solution would be fine, but again, i dont know how UVa Online Judge works.

IF THAT DOESNT WORK

If Judge is still giving you error, replace s.nextLine(); with little more sophisticated code:

while (true) {
    // ...

    if(s.hasNextLine()) {
        s.nextLine(); //devour that empty line between entries
    } else {
        break;
    }
}

This however expects input to end with last number, if there is one more empty line after the last number you must

while (true) {
    // ...

    s.nextLine(); //devour that empty line between entries
    if(!s.hasNextLine()) {
        break;
    }
}

to eat that last empty line

Solution 3:[3]

decide an input which is a breaking condition. e.g "EXIT"
then

  if(in.nextLine().equalsIgnoreCase("EXIT")){
     break;
  }

Or, if not possible,like this

 public static void main(String[] args) 
 {   
  //Something here
  int i = 0
  Scanner in = new Scanner(System.in);
  while(in.hasNext()) //How do I determine when to end?
  {
    //code
    i++;
    if(i==3){

     //Doing my calculation
     break;
    }
}

}

Solution 4:[4]

You can try something like this

    Scanner in = new Scanner(System.in);
    System.out.println("Your input: \n");
    List<String> list=new ArrayList<>();
    while(in.hasNextLine()) 
    {
       list.add(in.nextLine());
        if(list.size()==5){ //this will break the loop when list size=5
            break;
        }
    }
    System.out.println(list);

You have to use a trick like above to break the while loop. otherwise while loop keep running continuously.

My input:

hi
hi
hi
hi
hi

Out put:

[hi, hi, hi, hi, hi]

Solution 5:[5]

I had a similar problem when solving a question online. I found a way to do using List and while() loop.

Input - 

1
4
1 2 3 4
import java.util.*;
import java.lang.*;
import java.io.*;

class input {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        
        List<String> list = new ArrayList<>();
        while(sc.hasNext()){
           list.add(sc.nextLine());
        }
        System.out.println(list);
    
    }
}
Output - 

[1, 4, 1 2 3 4]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JohnnyAW
Solution 2
Solution 3
Solution 4
Solution 5 Ambuj Singh