Use the wrapper classes to create an application that reads a 5 digit number from the user and then reports back the sum of those digits. Next, perform the same operation without using wrapper classes.
I'm still stuck on the first part using wrapper classes, so I decided to try the second part of the problem and work on that part. Well, I got myself stuck on that part too.
So here's my program so far. Everything works except for at the end, when I add the numbers together, the calculations are wrong. I'm assuming that has something to do with they way I pulled the numbers from a string. What am I doing wrong?
public static void main(String[] args) {
String num = null;
int num6;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a five digit number: ");
num = scan.next();
// System.out.println(num);
// This gets the first number in the string
String input = num;
char num1 = input.charAt(0);
System.out.println(num1);
// This gets the second number in the string
String input2 = num;
char num2 = input2.charAt(1);
System.out.println(num2);
// This gets the third number in the string
String input3 = num;
char num3 = input3.charAt(2);
System.out.println(num3);
// This gets the fourth number in the string
String input4 = num;
char num4 = input4.charAt(3);
System.out.println(num4);
// This gets the fifth number in the string
String input5 = num;
char num5 = input5.charAt(4);
System.out.println(num5);
// This adds the five numbers together
num6 = num1 + num2 + num3 + num4 + num5;
System.out.println(num6);
}
}

New Topic/Question
Reply



MultiQuote




|