Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

language is in java Ask the user for a bunch of integers. Show the sum of those numbers. Make your program handle strange input. Specifically,

language is 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.
My problem is making my code output "Well, thats not a number but here is what i extracted " + num"
Current code is.
import java.util.Scanner;
public class hwtwo
{
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String str;
int sum = 0;
while(true) {
System.out.println("Enter a integer");
int a=0,l=0,m=0;
str =keyboard.next();
for (int i = 0; i
if (Character.isDigit(str.charAt(i)))
{l=1;
m=str.charAt(i);
a=a*10+m-48;}
if(l==0)
{ System.out.println("There were no digits in that input.");
break;}
else
{sum += a;
System.out.println("Current sum: " + sum);}
}
System.out.println("Final sum: " + sum);
}
}
image text in transcribed
thank you
Test Case 3 - Failed Standard Input ..4993 969798re7 2gf jh3 df64f57 f121 x2e3errrw5ddd6 Required Output Your Program's Output Enter a integerin Well, that's not a number but here's what I extracted : 43 in Current sum: 43 Enter a integer in Well, that's not a number but here's what I extracted : 6787 in Current sum: 6830 in Enter a integer in Well, that's not a number but here's what I extracted : 2 Current sum: 6832 Enter a integert Well, that's not a number but here's what I extracted : 3 Current sum: 6835 in Enter a integer in Well, that's not a number but here's what I extracted : 451 Current sum: 6880 Enter a integer in Well, that's not a number but here's what I extracted : 211 Current sum: 6901 Enter a integer in Well, that's not a number but here's what I extracted : 123456n Current sum: 130357 In Enter a integer in There were no digits in that input. In Enter a integerin Current sum: 43 Enter a integer in Current sum: 6830 in Enter a integerlin Current sum: 6832 Enter a integer in Current sum: 6835 Enter a integer in Current sum: 6880 Enter a integer Current sum: 690111 Enter a integer in Current sum: 130357in Enter a integerin There were no digits in that input. An Final sum: 130357n (Your output is too short.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions

Question

Methods of Delivery Guidelines for

Answered: 1 week ago