Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Language Is In Java. I need help in making my code output like the required text in the left, Specifically Make your program handle strange

Language Is In Java. I need help in making my code output like the required text in the left,
Specifically "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."
image text in transcribed
CURRENT CODE
import java.util.*;
public class SumNum {
public static void main(String[] args) {
Scanner sc = 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 = sc.nextLine();
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
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, If the user enters something without any digits, end the program. Test Standard Input 10 end Required Output Enter a integerin Current sum: 5 Enter a integer In Current sum: 15 kn Enter a integer There were no digits in that input. In Final sum: 15

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

More Books

Students also viewed these Databases questions

Question

Define cognition, and describe the functions of concepts.

Answered: 1 week ago