-3
public class aa {
    public static void main(String[] args) {
        int number = 521;
        String temp1 = "" + number;
        int result = 0;
        int[] temp2 = new int[temp1.length()];
        for(int i=0; i<temp1.length(); i++){
            int len = temp1.length();
            temp2[i] = temp1.charAt(len-i-1);
            System.out.println(temp2[i]);
            System.out.println(temp1.charAt(len-i-1));
        }

    }
    
}

This program should make 521 to 125 (reverse). But when I run this program, the result is

49
1
50
2
53
5

I think that string value is right, but when I add that string value to array, it goes wrong way. Can some one tell me what is wrong?

3
  • For starters, println prints a newline at the end of it output, so there's no way your output will be on one line only if you use that. Second, temp2 is an array of ints, you can't expect println to know you actually wanted chars if you ask it to print numbers. Commented Mar 28, 2021 at 9:48
  • You can easily find 1000s of articles on how to reverse a number in java. Commented Mar 28, 2021 at 9:50
  • @AlexRudenko That doesn't really explain what the problem is with this approach. That question is about alternative approaches. The one OP is using, while not optimal, would work once they fix the type of the array. Commented Mar 28, 2021 at 10:23

3 Answers 3

0

As I was saying in the comments, temp2 is the wrong type. You declared it as an array of ints, so println, of course, is treating it as an array of numbers, not of printable characters.

Just change temp2's type to char[]:

public class aa {
    public static void main(String[] args) {
        int number = 521;
        String temp1 = "" + number;
        int result = 0;
        char[] temp2 = new char[temp1.length()];
        for(int i=0; i<temp1.length(); i++){
            int len = temp1.length();
            temp2[i] = temp1.charAt(len-i-1);
            System.out.println(temp2[i]);
            System.out.println(temp1.charAt(len-i-1));
        }

        // print the whole reversed number on one line
        for(char c : temp2) {
            System.out.print(c);
        }

        System.out.println();
    }
    
}

Output

1
1
2
2
5
5
125

Of course this is not the optimal solution, just a way to fix the code you wrote so that it works. See Alex Rudenko's comment for a link with better solutions to the problem of reversing the digits of a number.

Sign up to request clarification or add additional context in comments.

Comments

0

As temp2 is an array of int values, the following assignment needs to be fixed:

temp2[i] = temp1.charAt(len-i-1);

because it assign an ASCII value of char.

This can be done by subtracting a 0 or by using Character::getNumericValue

temp2[i] = temp1.charAt(len-i-1) - '0';
// or
temp2[i] = Character.getNumericValue(temp1.charAt(len-i-1));

A non-negative integer number may be reversed without using any String conversion:

  1. The "length" of temp2 array is defined by Math.log10
  2. Each digit is calculated with modulo operator % and dividing the input while it is greater than 0
public static void main(String[] args) {
    int number = 521;

    int len = number < 2 ? 1 : (int) Math.ceil(Math.log10(number));
    
    int[] temp2 = new int[len];
    
    for (int i = 0, n = number; n > 0; n /= 10, i++) {
        
        temp2[i] = n % 10;
        
        System.out.println(temp2[i]);
    }
    for (int i : temp2) {
        System.out.print(i);
    }
    System.out.println();
}

Output

1
2
5
125

Comments

-1

You question is not very clear about if you are asking for opinion. But a query that "when I add that string value to array, it goes wrong way."

So , String is a reference type, while your array is of int type. You need to parse the value from String to int

Integer.parseInt(String)

2 Comments

String temp1 = "" + number; works. That's not optimal but it surely is not the problem with this code
@FedericoklezCulloca I think question was related to String value to array and that is what was explain. Since array was of int type. I have not referred to int assigned to String.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.