Here is the question: Write a program that asks the user to enter a series of si
ID: 3615337 • Letter: H
Question
Here is the question:Write a program that asks the user to enter a series of singledigit numbers with nothing separating them. the program shoulddisplay the sum of all the single digit numbers in the string. Forexample, if the user enters 2514, the method should return 12,which is the sum of 2, 5, 1, and 4. The program should also displaythe highest and lowest digits in the string (Hint: convert thestring to an array).
I don't know my code is answering the above question, but I gotcorrect answer for summing up the digits:
java.util.*;
SumDigits
main(String[] args)
Scanner keyboard = Scanner(System.in);
System.out.print();
String digits = keyboard.nextLine();
[] b = [digits.length()];
String [] a = String[digits.length()];
sum = 0;
( i = 0; i < digits.length(); i++)
a[i] = digits.substring(i,i+1);
sum = sum + Integer.parseInt(a[i]);
System.out.print();
System.out.println(sum);
System.out.println( + Integer.MIN_VALUE);
System.out.println( + Integer.MAX_VALUE);
However, I cannot display the highest and lowest digits of what theuser typed. It displayed:
The lowest digits is -2147483648
The highest digits is 2147483647
So I think MIN_VALUE and MAX_VALUE is not appropriate code to findthe user's digits.
How should I change my code to display the highest and lowestdigits?
I really appreciate for any answers! Thank you.
Explanation / Answer
please rate - thanks you were displaying, the largest and smallest numbers that can berepresented. nothing to do with the number you entered. It's a lot easier if you make the array integer, not String, sinceyou are referencing the digits multiple times. I made thatchange import java.util.*; public class SumDigits { public static void main(String[] args) { // Create a Scanner object to read keyboard input. Scanner keyboard = new Scanner(System.in); // Get number. System.out.print("Enter Number:"); String digits = keyboard.nextLine(); int [] a = new int[digits.length()]; int max=-1000,min=10000; int sum = 0; for (int i = 0; i max) max=a[i]; else if(a[i]Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.