Answered step by step
Verified Expert Solution
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."
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);
}
}
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