Write a loop that will continuously prompt the user for integers to sum, -1 to exit. Sum these values and display the sum to the console.

Here is what I have so far, but I feel like I am missing something:

public class Sum1 {

public static void main (String [] args) {

Scanner in = new Scanner(System.in);

final int Sentinel = -1;
int i;
int sum = 0;

System.out.println("Enter an integer:");
i = in.nextInt();

while(i != Sentinel) {

System.out.println("Enter an integer:");
i = in.nextInt();

sum = sum + i;

System.out.println("sum: " + sum + ".");

}

}

}

Yes, something is missing. The program is not complete. The braces do not balance. Here are my comments

public class Sum1 {

public static void main (String [] args) {

Scanner in = new Scanner(System.in);

final int Sentinel = -1; // I would put SENTINEL
int i=0; // initialize to 0
int sum = 0;

System.out.println("Enter an integer:(-1 to start summation)");

while(i != Sentinel) { // 0!=SENTINEL
i = in.nextInt(); // put this in the loop
// note: this does not do any data validation.
// perhaps this will be covered later in your course
sum+=i;
} // end while
// print sum as the output of the summation
} // end main
} // end public class

Thanks for the help. I have another question: We have to open and read a file until the sentinel value of "xxx" appears. Also, we have to count the lines read not including the line with the sentinel value.

This is the file:

A gentle breeze rustling the dry cornstalks.
A sound is heard, a goblin walks.
A harvest moon suffers a black cat'scry.
Oh' do the witches fly!
Bonfire catches a pumpkins gleem.
Rejoice, it's Halloween!
xxx
-Richard Anderson (me) © Copyright 1998

This is what I have written:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Text1 {

public static void main(String[] args) throws IOException {

String line = null;
int linecount = 0;

BufferedReader br = new BufferedReader(new FileReader("input1.txt"));

while((line = br.readLine()) != null && (line = br.readLine()) != "xxx"){

linecount ++;

}

System.out.println("Number of lines in file is: " + linecount + ".");

}

}

Actually the solution to this problem is not that much different from the previous one.

You would open a scanner to read from a file, which would be
Scanner in=new Scanner(new File("filename");
Declare and initialize an integer counter to zero.
Start the while loop, but the pretest condition will be in.hasNextLine(), since you're trying to read in one line at a time.
You'll actually read the line with
String line=in.nextLine();
Check if xxx is in the line using
the condition line.matches("xxx").
If that happens, break from the while loop, otherwise increment counter.
Outside of the while loop, print the number of lines as required.

Make a test and try your best to compile and run it, it's works out well. I got 6 lines for the answer.

Your code looks almost correct. The only issue is that you are prompting the user for input before entering the loop, but not inside the loop after the first input. Here is the corrected code:

```java
import java.util.Scanner;

public class Sum1 {
public static void main (String [] args) {
Scanner in = new Scanner(System.in);
final int Sentinel = -1;
int i;
int sum = 0;

System.out.println("Enter an integer:");
i = in.nextInt();

while(i != Sentinel) {
sum = sum + i;
System.out.println("Enter an integer:");
i = in.nextInt();
}

System.out.println("Sum: " + sum + ".");
}
}
```

In this code, the first input is taken outside the loop, and then inside the loop, the input is taken again before summing it. The loop will continue until the user enters -1, at which point the loop will exit, and the sum will be displayed.