1
Scanner s = new Scanner(System.in);
List<Integer> solutions = new LinkedList<>();
int o = 0;

while (o != 10) {        // I want to read 2 numbers from keyboard   
    int p = s.nextInt(); // until send and enter, this is where is my
    int c = s.nextInt(); //doubt
    int d = p + c;
    solutions.add(d);  
    o = System.in.read();
}

Iterator<Integer> solution = solutions.iterator();
while (solution.hasNext()) {
    int u = solution.next();
    System.out.println(u);
}

The problem that I have is, how I could send an enter for end the loop? because the System.in.read() takes the first number if I put another 2 numbers and example could be,

entries:

2 3 (enter) read 2 numbers and sum

1 2 (enter) read 2 numbers and sum

(enter) and here end the loop because of the enter and no numbers and gave the solutions

exits:

5

3

I don't know uf I posted well before

Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
limit2001
  • 9
  • 7
  • print 'u' value into while loop and observe what it's while pressing ENTER. accordingly set if and inside if break, this will work for you. – Vishal Gajera Mar 02 '16 at 13:24

1 Answers1

0

Read in the whole line and parse it yourself. If the line is empty exit the loop end the program. If its not empty then pass the line to a new scanner.

List<Integer> solutions = new LinkedList<>();

Scanner systemScanner = new Scanner(System.in);
String line = null;

while ((line = systemScanner.nextLine()) != null && !line.isEmpty()) {
    Scanner lineScanner = new Scanner(line);
    int p = lineScanner.nextInt();
    int c = lineScanner.nextInt();
    int d = p + c;
    solutions.add(d);
}

Iterator<Integer> solution = solutions.iterator();
while (solution.hasNext()) {
    int u = solution.next();
    System.out.println(u);
}
flakes
  • 21,558
  • 8
  • 41
  • 88