Question

In: Computer Science

Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers...

Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers using a sentinel while loop. The program should accept integer inputs until the user enters the value -1 (negative one is the sentinel value that stops the while loop). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output. The program should ignore any other negative input and should continue to run.

Hint: to remember/save the entered (good) positive values, you can concatenate them into a string (separated by comas) that you can output later on.

The following are sample runs showing the input prompt and the outputs: (Notice the user may enter one value per line. DO NOT read inputs as String type)

Enter positive integers (-1 to quit): 10 -5 2 -1

Entered Numbers: 10, 2

The Sum: 12

Enter positive integers (-1 to quit): 1 2 3 4 5 6 -1

Entered Numbers: 1, 2, 3, 4, 5, 6

The Sum: 21

Enter positive integers (-1 to quit): 1 1 -7 1 1 -9 100 -1

Entered Numbers: 1, 1, 1, 1, 100

The Sum: 104

Make sure the program validates each entered number before processing it as the user may enter negative numbers (other than the sentinel value -1).

Design your program such that it allows the user to re-run the program with a different set of inputs in the same run.

Document your code, and organize and space out your outputs as shown above.

Solutions

Expert Solution

Code:

import java.util.Scanner;
class InputSum{
   public static void main(String args[]){
       Scanner s=new Scanner(System.in);
       String str="";
       int r=1,tot=0,x;
       do{
           tot=0;
           System.out.print("Enter positive integers(-1 to quit):");
           while(true){
               x=s.nextInt();
               if(x==-1){
                   break;
               }
               else if(x>=1){
                   tot=tot+x;
                   str=str+x+",";
               }
           }
           System.out.println("Entered Numbers:"+str.substring(0,str.length()-1));
           System.out.println("The Sum:"+tot);
           System.out.print("want to re-run enter 1 otherwise 0:");
           r=s.nextInt();
           str="";
          
       }while(r!=0);
      
   }
}

Output:


Related Solutions

JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
Write a new program named Bar that prompts the user to enter a positive integer. The...
Write a new program named Bar that prompts the user to enter a positive integer. The program should then display a line consisting of the entered number of asterisks using a while loop. If the user enters a number that is not positive, the program should display an error message (see example below). Example 1: Enter a positive number: 6 ****** Example 2: Enter a positive number: 11 *********** Example 3: Enter a positive number: -4 -4 is not a...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT