Answered step by step
Verified Expert Solution
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 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.)
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);
}
}
thank you
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started