Output

Variable Name      Value in Memory
data
sum
answer
  1  import java.util.Scanner; 
  2  
  3  public class SentinelValue {
  4    /** Main method */
  5    public static void main(String[] args) {
  6      // Create a Scanner
  7      Scanner input = new Scanner(System.in);
  8  
  9      // Read an initial data
 10      System.out.print(
 11        "Enter an integer (the input ends if it is 0): ");
 12      int data = input.nextInt();
 13  
 14      // Keep reading data until the input is 0
 15      int sum = 0;
 16      while (data != 0) {
 17        sum += data;
 18  
 19        // Read the next data
 20        System.out.print(
 21          "Enter an integer (the input ends if it is 0): ");
 22        data = input.nextInt();
 23      }
 24  
 25      System.out.println("The sum is " + sum);
 26    }
 27  }