Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Language is in Java my question is how do I get my code to give out the current sum that the user inputs rather than
Language is in Java
my question is how do I get my code to give out the current sum that the user inputs rather than make it say 0? Thank you
my current code is
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.println("Enter an integer"); System.out.println("Current sum: " + sum); 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("Final sum: " + sum); } private static String extractDigits(String s) { String digits = ""; for(int i = 0; iAsk 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 nu If the user enters something without any digits, end the program. Test Case 1 - Failed Standard Input 10 end Required Output Enter a integer Current sum: 5 Enter a integer Current sum: 15 Enter a integer There were no digits in that input. In Final sum: 15 Your Program's Output Enter an integer in Current sum: on Enter an integer Current sum: 51n Enter an integern Current sum: 15 There were no digits in that input. An Final sum: 151n B68 mt: 2 Test Case 2 - Failed 868 Standard Standard Input XXX4xxx s good om that te truc Required Output Your Program's Output
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