There are a number of ways, one of which would be...
String ss[] = "123".split("");
int i =
Integer.parseInt(ss[0]) +
Integer.parseInt(ss[1]) +
Integer.parseInt(ss[2]);
System.out.println(i);
another would be...
String s = "123";
int i =
Character.getNumericValue(s.charAt(0)) +
Character.getNumericValue(s.charAt(1)) +
Character.getNumericValue(s.charAt(2));
System.out.println(i);
and still another would be...
String s = "123";
int i =
s.charAt(0) +
s.charAt(1) +
s.charAt(2) -
(3 * 48);
System.out.println(i);
BUT hard coding for 3 numbers isn't very useful beyond this simple case. So how about recursion??
public static int addDigis(String s) {
if(s.length() == 1)
return s.charAt(0) - 48;
return s.charAt(0) - 48 + addDigis(s.substring(1, s.length()));
}
Answer from ChiefTwoPencils on Stack OverflowOutput for each example: 6
There are a number of ways, one of which would be...
String ss[] = "123".split("");
int i =
Integer.parseInt(ss[0]) +
Integer.parseInt(ss[1]) +
Integer.parseInt(ss[2]);
System.out.println(i);
another would be...
String s = "123";
int i =
Character.getNumericValue(s.charAt(0)) +
Character.getNumericValue(s.charAt(1)) +
Character.getNumericValue(s.charAt(2));
System.out.println(i);
and still another would be...
String s = "123";
int i =
s.charAt(0) +
s.charAt(1) +
s.charAt(2) -
(3 * 48);
System.out.println(i);
BUT hard coding for 3 numbers isn't very useful beyond this simple case. So how about recursion??
public static int addDigis(String s) {
if(s.length() == 1)
return s.charAt(0) - 48;
return s.charAt(0) - 48 + addDigis(s.substring(1, s.length()));
}
Output for each example: 6
you can use integer math to come up with the three numbers seperately
int first = threedigitinput / 100;
int second = (threedigitinput % 100) / 10;
int third = threedigitinput % 10;
Split an int into 2-digit chunks and store in an array
java - Convert an integer to an array of digits - Stack Overflow
Java- Converting a string of numbers separated by spaces into an array?
[Java] Separate numbers of an integer, then add them together.
Been stuck on this for a little while. Basically, if I have the int/Integer: 1425273622
I need to split it apart and put it in an array like: [14,25,27,36,22]
I could’ve sworn I learned a way to do something like this (.toString() and substring the first 2 characters at a time, then parseInt()?) but I can’t for the life of me get the right functions calls.
The immediate problem is due to you using <= temp.length() instead of < temp.length(). However, you can achieve this a lot more simply. Even if you use the string approach, you can use:
String temp = Integer.toString(guess);
int[] newGuess = new int[temp.length()];
for (int i = 0; i < temp.length(); i++)
{
newGuess[i] = temp.charAt(i) - '0';
}
You need to make the same change to use < newGuess.length() when printing out the content too - otherwise for an array of length 4 (which has valid indexes 0, 1, 2, 3) you'll try to use newGuess[4]. The vast majority of for loops I write use < in the condition, rather than <=.
You don't need to convert int to String. Just use % 10 to get the last digit and then divide your int by 10 to get to the next one.
int temp = test;
ArrayList<Integer> array = new ArrayList<Integer>();
do{
array.add(temp % 10);
temp /= 10;
} while (temp > 0);
This will leave you with ArrayList containing your digits in reverse order. You can easily revert it if it's required and convert it to int[].
For example, I have String input which will look something like "5 6 6" but I need it to be converted into an array. What is the easiest way to do this? I have messed around with .split and .parseInt but have not come to any solid conclusions. Thanks!
Not sure where to go with this one. I've tried a couple methods, like converting a string to a char array and outputting that, although it prints the number back without spaces, which isn't what I need.
I know how to do the addition of each individual numbers using modulo, but outputting the individual numbers of the int, in order, is the part tripping me up.
I figured putting the numbers into an array would work, but since Java doesn't allow for dynamically changing the length of an array, I'm not sure how efficient that would have been. Also, since I do need the int output for the sake of adding the numbers together, I'm lost as to how to do this.
It should look like this:
input : 222294 output : 2 2 2 2 9 4 addition output : 21
I can do the last part no problem, but getting the numbers IN ORDER and separated is difficult.
Also, since I can't control what the number is or how many digits it is, that is also a problem.
What would be an efficient way to get them separated in order?
public class Kata {
public static int[] digitize(long n) {
String numStr = String.valueOf(n);
int digits[] = new int[numStr.length()];
int counter = 0;
for(int i = numStr.length() - 1; i >= 0; i--) {
digits[i] = Integer.parseInt(Character.toString(numStr.charAt(counter++)));
}
return digits;
}
}
Your method returns Float[] instead of int[]. Try this:
int[] ints = Arrays.stream(array).mapToInt(Integer::parseInt).toArray();
return ints;
int[] test={10212,10202,11000,11000,11010};
ArrayList<Integer> test2 = new ArrayList<Integer>();
for(int i = test.length -1; i >= 0; i--){
int temp = test[i];
while(temp>0){
test2.add(0, temp%10); //place low order digit in array
temp = temp /10; //remove low order digit from temp;
}
}
This will do exactly what you want by placing the lowest order digit of an entry into the "front" of an arraylist, and therefore in front of the previous low order digits/entries.
If you need it to be in an Array, ArrayList has a toArray method.
You can go as suggested by Mark, or convert them to String to get the single digits:
int test[]={10212,10202,11000,11000,11010};
ArrayList<Integer> test2 = new ArrayList<Integer>();
for (int i : test)
{
String str = String.valueOf(i);
for (int j = 0; j < str.length(); ++j)
test2.add((int)(str.charAt(j)-'0'));
}
As more memory efficient approach that still involves string would be to keep all the digits as just one string and calculate the int value on the fly:
class Digits
{
String str;
Digits(int[] nums)
{
StringBuilder sb = new StringBuilder();
for (int i : nums)
sb.append(String.valueOf(i));
str = sb.toString();
}
int length()
{
return str.length();
}
int nth(int i)
{
return (int)(str.charAt(i)-'0');
}
}
Mind that CheesePls solution is the right one because it uses math as it is intended to be used. Mine is just for noobs (and just to give another approach to the problem)..