for example, to take the list 1, 2 ,5 ,12 , 15, 8
and do the following:
1+2+5+(1+2) + (1+5) + 8
the numbers that need to be split have been added to an array list i just need to know how i can split single integers?




Posted 21 July 2010 - 10:05 PM
Posted 21 July 2010 - 10:45 PM
Posted 21 July 2010 - 10:47 PM
Posted 21 July 2010 - 10:53 PM
bcranger, on 22 July 2010 - 04:45 AM, said:
int aInt = 9881;
String aString = Integer.toString(aInt);
int y = aString.charAt(0); // <-- You will get the ascii value
char x = aString.charAt(0); // So do this
System.out.println(aString.charAt(0)); // Or this to get the actual digit
System.out.println(y);
System.out.println(x);
Posted 22 July 2010 - 12:06 AM
Crunch, on 21 July 2010 - 09:53 PM, said:
bcranger, on 22 July 2010 - 04:45 AM, said:
int aInt = 9881;
String aString = Integer.toString(aInt);
int y = aString.charAt(0); // <-- You will get the ascii value
char x = aString.charAt(0); // So do this
System.out.println(aString.charAt(0)); // Or this to get the actual digit
System.out.println(y);
System.out.println(x);
Posted 22 July 2010 - 12:25 AM
bcranger, on 22 July 2010 - 06:06 AM, said:
Crunch, on 21 July 2010 - 09:53 PM, said:
int aInt = 9881;
String aString = Integer.toString(aInt);
int y = aString.charAt(0); // <-- You will get the ascii value
char x = aString.charAt(0); // So do this
System.out.println(aString.charAt(0)); // Or this to get the actual digit
System.out.println(y);
System.out.println(x);
System.out.println(aString.charAt(0) -48);
This post has been edited by Crunch: 22 July 2010 - 12:27 AM
Posted 22 July 2010 - 01:52 AM
int number = 58173;
int sumDigits = 0;
while (number > 0) {
int lastDigit = number % 10;
sumDigits += lastDigit;
number /= 10;
}
Posted 22 July 2010 - 05:36 AM
String stringNum = s.charAt(0) + ""; // Converts to String int num = Integer.parseInt(stringNum);
Posted 22 July 2010 - 05:50 AM
Posted 22 July 2010 - 05:55 AM
macosxnerd101, on 22 July 2010 - 07:50 AM, said:
Posted 22 July 2010 - 06:19 AM
String s = ""//list of numbers here
int sum = 0;
char c = '';
for(int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if(c.isDigit()) {
sum += Integer.parseInt(c.toString()); //can't parse chars
} //end if
} //end for
//display sum
This post has been edited by NeoTifa: 22 July 2010 - 06:20 AM
Posted 22 July 2010 - 06:25 AM
Posted 22 July 2010 - 06:28 AM
NeoTifa, on 22 July 2010 - 08:19 AM, said:
String s = ""//list of numbers here
int sum = 0;
char c = '';
for(int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if(c.isDigit()) {
sum += Integer.parseInt(c.toString()); //can't parse chars
} //end if
} //end for
//display sum
Character.isDigit(c)
Character.toString(c)
