Computer Sciene-java

posted by .

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(;

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 + ".");




  • Computer Sciene-java -

    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(;

    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
    } // end while
    // print sum as the output of the summation
    } // end main
    } // end public class

  • Computer Sciene-java -

    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!
    -Richard Anderson (me) © Copyright 1998

    This is what I have written:


    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 + ".");



  • Computer Sciene-java -

    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.

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. Reply to grant about a regression problem

    This is a reply to the question posted here
  2. Computer Programming

    Java Programming Question: basically want to keep the user in a loop that displays the menu and requests a user choice again and again until the user enters choice option 0 and exits the program. When Option 1 is chosen, I want to …
  3. Java

    I am supposed to convert a program i already made into a Java console application that uses the Scanner class to read the user's input. This is the code i have that i need to convert. I don't know where to start. Here is the code import …
  4. programming

    I am supposed to modify this program so it begins by asking the user for the number of values. The program then uses a loop to read the specified number of values and then display their average. program is below that i worked on in …
  5. college math

    I do not understand a problem from a text book or how to solve the problem for the answer. Could shomeone show me the steps (show work) on how to solve this question. The sum of the intergers from 1 through n is n(n+1)/2. the sum of …
  6. java

    Write a java application that finds the smallest of several integers. Prompt the user for the number of integers that will be input, then prompt the user for that number of integers. Evaluate the integers to determine the smallest …
  7. computer science

    Writing a c++ program that stores the following values in five different variables:28,32,37,24,and 33.We have been ask to get the average of the series of values given above. Here I what I got /// This program calculates the sum and …
  8. Computer science

    How to convert from decimal to binary in matlab.....this is what I have so far..... dec = 43; i = 1; sum=0; while dec>0 r = rem(dec, 2); sum=sum+(i*rem); dec=dec/2; i=i*10; end sum
  9. Computer Science/Pseudo Code Python

    Hi, I'm confused about an assignment. I'm not sure what to do and would like some help. An example: Write a program that obtains two integer numbers from the user. It will print out the sum of those numbers. Pseudo code: Prompt user …
  10. Computer Science

    Write a program that Will prompt a user to enter his/her name and number.use a while loop that will display the user name times the entered number

More Similar Questions