public class Example {
public static void main(String[] args) {
String input = "152496"; // Integer string entered
int[] A = new int[input.length()]; // input Prepare array by string length.
for(int i=0;i<input.length();i++) {
When A[i] = input.charAt(i) - '0'; // i=0, '1' - '0' = 1.
}
//Output
for(int i=0;i<A.length;i++) {
System.out.println("A["+i+"]="+A[i]);
}
}
}
If you want to do as you asked, you can do it as above.
When converting a char into a number, it can be obtained as the difference between Ascii, such as '1' and '0'.
To replace a numeric string consisting of more than one char (for example, "123") with a number: (Of course, even if it is a Korean character, it can be as follows.)
public class Example2 {
public static void main(String[] args) {
int value = Integer.parseInt("123");
System.out.println("value="+value);
}
}
.
One more thing, if the purpose is to decompose the input integer (int) into each digit... (without using a string)
public class Example3 {
public static void main(String[] args) {
int input =152496;
// Calculating Digits
int n,temp;
n =0;
temp = input;
while(temp!=0) {
n++;
temp /= 10; // temp = temp / 10;
}
// Create Array
int[] A= new int[n];
// Decompose each digit
temp = input;
for(int i=A.length-1;i>=0;i--) {
A[i] = temp %10; // from the very end of the array, the lowest digit (one digit) of the number received is stored through the remaining operations.
Temp /= 10; // divide by 10 so that the digit of 10 is 1.
}
//Output
for(int i=0;i<A.length;i++) {
System.out.println("A["+i+"]="+A[i]);
}
}
}
public class Main {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
// variable declaration
int num;
int count = 0;
int arrCount = 0;
num=s.nextInt();
// Find a multiple of 10 to divide the integer
int i = 1;
while(i<=num){
i = i*10;
count++;
}
i = i/10;
// Declare the array and divide it by the above i and put it in the array
int[] arr = new int[count];
while(num>0){
arr[arrCount] = num/i;
num = num%i;
i = i/10;
arrCount++;
}
//Output
for(int j = 0 ; j<arr.length;j++){
System.out.println(arr[j]);
}
}
© 2024 OneMinuteCode. All rights reserved.