You need to convert the integer to an actual int using Integer.parseInt()
System.out.println("nums:");
Scanner myScanner = new Scanner(System.in);
String values = myScanner.nextLine();
int count = 0;
int sum = 0;
for (String s : values.split("\\s+")) {
sum+= Integer.parseInt(s);
}
System.out.println(sum);
Prints
6
Assuming you had a string or digits, you could also do it like this.
String str = "12345";
int sum = 0;
for (char c : str.toCharArray()) {
// convert the ascii value to a usable int, then add it to the sum
sum = sum * 10 + c - '0';
}
System.out.println(sum);
Prints
12345
Why does the previous solution work? Characters are encoded in different ways to be printed. Characters 0 thru 9 are ASCII encoded (in decimal) as 48, 49, 50, 51 ... So when you added 1, 2, and 3 you really added 49, 50, and 51 and obtained 150. So you need to subtract '0' or 48 from each character digit to get the actual binary value that it represents. Above, I just multiplied each time by 10 to move the current value left to make room for the next digit.
0 * 10 + 1 = 1
1 * 10 + 2 = 12
12 * 10 + 3 = 123
123 * 10 + 4 = 1234
1234 * 10 + 5 = 12345