I'm trying to add all the digits in a value together and to do that I changed the input of a number into a string so I could find each individual character. There are 2 ways I think I can go about this but I have already tried both separately and failed. The first way I was thinking of is to convert the characters back to integers then add them together, but I'm having problems with that because I do not know how to do it. The second way I was thinking of is creating a list then adding the characters into the list so I can add them together in a separate variable. (not sure if I can do that since they aren't integers and I'm trying to do that and keep getting errors)
Code:
System.out.println("Enter a number: ");
int num = kb.nextInt();
System.out.println("num = " + num);
String l = Integer.toString(num);
int numl = l.length();
char[] svlist = new char[] {};
for (int i = 0; i <= numl; i++) {
char sv = l.charAt(i);
svlist.add(sv);
}
System.out.print(svlist);
Everything is initialized before this in the full code and I checked it and haven't found any problems with it and neither has java.