1
Using this code as a test, whose goal is to invert a string containing numbers.
Input: 0123456789
Output: [57, 56, 55, 54, 53, 52, 51, 50, 49, 48]
unlike [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
.
public class exercise33 {
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
String hold = "";
int j = 0;
System.out.print("Enter a number: ");
hold = sc.next();
int[] test = new int[hold.length()];
for(int i = hold.length() - 1 ; i >= 0 ; i--) {
test[j++] = hold.charAt(i);
}
System.out.print(Arrays.toString(test));;
sc.close();
}
}
Other form is, instead of using an array, use a string to receive the result:
test += hold.charAt(i);
– ramaral