Loop termination confusion

advertisements

So I have this code to enter a series of integers to a sequence but I would like to know how can I terminate the while loop just by entering no values and pressing enter. I know that I can put a condition like terminate the loop if the x integer value is 0.

public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
    SimpleWriter out = new SimpleWriter1L();
    Sequence<Integer> s = new Sequence1L<>();
    Sequence<Integer> temp = s.newInstance();
    System.out.println("Enter ");
    int x = in.nextInteger();
    int i = 0;
    while (in.nextLine()) {
        s.add(i, x);
        x = in.nextInteger();
        i++;
    }
    System.out.println(s);
}


You need to change the way you're reading input if you want to achieve what you described - enter nothing.

First having a while (in.nextLine()) eats an extra line from your input. So half of your input lines are just lost.

I'd suggest reading the line like String line = in.nextLine(). Then something like:

if (line.equals("")) break;
int x = Integer.parseInt(line);

Sorry, not doing java lately to give you the whole loop. But I think you should get the idea.