Question

in java, Ask the user for a bunch of integers. Show the sum of those numbers....

in java, Ask the user for a bunch of integers. Show the sum of those numbers.

Make your program handle strange input. Specifically, if they didn't enter a number, scan the String to find just the digits and piece those digits together to form a number.

If the user enters something without any digits, end the program.

input:

5ENTER

10ENTER

endENTER

output:

Enter a integer\n
Current sum: 5\n
Enter a integer\n
Current sum: 15\n
Enter a integer\n
There were no digits in that input.\n
Final sum: 15\n

input

5ENTER
xxx4xxxENTER
z

output:

Enter a integer\n
Current sum: 5\n
Enter a integer\n
Well, that's not a number but here's what I extracted: 4\n
Current sum: 9\n
Enter a integer\n
There were no digits in that input.\n
Final sum: 9\n

input

,,4gg3ENTER
g6g7g8re7ENTER
2gfENTER
jh3ENTER
dfd4f5fENTER
ff21ENTER
********1x2e3errr4ww5ddd6ENTER
g

output:

Enter a integer\n
Well, that's not a number but here's what I extracted: 43\n
Current sum: 43\n
Enter a integer\n
Well, that's not a number but here's what I extracted: 6787\n
Current sum: 6830\n
Enter a integer\n
Well, that's not a number but here's what I extracted: 2\n
Current sum: 6832\n
Enter a integer\n
Well, that's not a number but here's what I extracted: 3\n
Current sum: 6835\n
Enter a integer\n
Well, that's not a number but here's what I extracted: 45\n
Current sum: 6880\n
Enter a integer\n
Well, that's not a number but here's what I extracted: 21\n
Current sum: 6901\n
Enter a integer\n
Well, that's not a number but here's what I extracted: 123456\n
Current sum: 130357\n
Enter a integer\n
There were no digits in that input.\n
Final sum: 130357\n

Homework Answers

Answer #1

Given below is the code for question. Please do rate the answer if it helped. Thank you.
import java.util.Scanner;

public class SumNumbers {
  
   public static void main(String[] args) {
       Scanner keyboard = new Scanner(System.in);
       String str;
       int sum = 0;
       while(true) {
           System.out.print("Enter an integer: ");
           if(keyboard.hasNextInt())
               sum += keyboard.nextInt();
           else {
               str = keyboard.next();
               str = extractDigits(str);
               if(str.equals("")) {
                   System.out.println("There were no digits in that input.");
                   break;
               }
               else {
                   System.out.println("Well, that's not a number but here's what I extracted: " + str);
                   sum += Integer.parseInt(str);
               }
              
               System.out.println("Current sum: " + sum);
           }
       }
      
       System.out.println("Final sum: " + sum);

      
   }
  
   private static String extractDigits(String s) {
       String digits = "";
       for(int i = 0; i < s.length(); i++) {
           char c = s.charAt(i);
           if(Character.isDigit(c))
               digits += c;
       }
       return digits;
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
1. Write a program to: Prompt the user to input an integer, a double, a character,...
1. Write a program to: Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space. Add a statement to also output in reverse. Add a statement to cast the double to an integer, and output that integer. 2. 2. Choose the statement(s) that generate(s) this output: I wish you were here a. printf("I wish", "you were here"); b....
C program fractions.c that does the following: 1. The program starts by making the user enter...
C program fractions.c that does the following: 1. The program starts by making the user enter a non-negative integer number a, called the first numerator. If the user enters a negative number, the program repeats the entry. 2. The program then makes the user enter a positive integer number b, called the first denominator. If the user enters a non-positive number, the program repeats the entry. 3. The program then makes the user enter a non-negative integer number c, called...
This program will output a right triangle based on user specified height triangle_height and symbol triangle_char....
This program will output a right triangle based on user specified height triangle_height and symbol triangle_char. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangle_char character. (1 pt) (2) Modify the program to use a loop to output a right triangle of height triangle_height. The first line will have one user-specified character, such as % or *. Each subsequent line will have...
JAVA (Don't make it too complicated) Write a program that prompts the user for the name...
JAVA (Don't make it too complicated) Write a program that prompts the user for the name of a text file. The file should consist of a sequence of integers, one integer per line. The program will read in each line (using nextLine()), parse it as an int (using Integer.parseInt()), and report the number of values and their average. The average should be computed as a double. Your program should do some basic exception handling: (1) If the file cannot be...
Problem: Our Armstrong number Please write code for C language So far we have worked on...
Problem: Our Armstrong number Please write code for C language So far we have worked on obtaining individual digits from 4 digits or 5 digit numbers. Then added them to find the sum of digits in various examples and assignments. However, the process of extracting individual digits is actually can be solved using a loop as you were doing a repetitive task by using mod operation and division operation. Now, we know how loops work and we can remove the...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to the characters that are in the string until they are ready to stop. We will need to read in several pieces of data from the user, including a string and multiple characters. You can set a maximum size for the user string, however, the specific size of the string can change and you can’t ask them how long the string is (see the sample...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT