-3

I want to write a program which reverses all the integers entered in an array but my code displayed here isn't working properly. Here is my code:

import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
    public static void main (String[] args) throws Exception {
        //code
          Scanner ss=new Scanner(System.in);
       int[] arr = new int[31];
       int T=ss.nextInt();
       int rem,p=0;

       for(int i=1;i<=T;i++){
           int a=ss.nextInt();
           if(a<=1000000000){
              while(a!=0){
                  rem=a%10;
                  p=p*10+rem;
                  a=a/10;                     
             }
           System.out.println(p);
         } else
           System.out.println("wrong input");
       }
    }
}

input:

2
56
78

expected output:

65
87

actual output:

65
6578

What is wrong?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    I don't see a legitimate question. – Andrew Li Aug 04 '16 at 03:42
  • I don't see any way you could reverse the integers 2, 56, and 78 and expect to get 65 and 87. – Robert Columbia Aug 04 '16 at 03:44
  • 1
    There's a difference between the requirement and the expected output. – Raman Sahasi Aug 04 '16 at 03:45
  • 1
    Besides the very unclear question, some hints: your naming is terrible. Dont be stingy with **letters**. Dont call something "arr" (which says **nothing**) when you could call it "initialNumbers" ... and maybe have a second array that you would call "reversedNumbers" for example. Then: separate concerns. Dont do everything in your main method; for example you could write a method that reverses a single number; which is then used by your main method. Meaning: split up the work that needs to be done into smaller elements; and build your solution from there. That also makes testing easier! – GhostCat Aug 04 '16 at 03:48
  • @GhostCat if you had an array of PirateShip, I could see naming it "arr" as funny and meaningful. In this case, however, you are right. – Robert Columbia Aug 04 '16 at 03:52
  • I didn't get the point for the question – Reza Maulana Aug 04 '16 at 03:54
  • @RobertColumbia Arr, that's funny. But honestly, newbies shouln't try to write funny programs. Most of the time, when people try to be funny in code, the result is A) not funny B) actually much harder to read/understand C) coming with high risk of being misinterpreted ;-) – GhostCat Aug 04 '16 at 04:01

3 Answers3

0

Possible solution could be

if(a<=1000000000){
               p=0; // reinitialize p
              while(a!=0){
                  rem=a%10;
                  p=p*10+rem;
                  a=a/10;

              }

For better understanding just add this line System.out.println("P is :"+ p); in if and you will see why p=0; was required.

if(a<=1000000000){
               System.out.println("P is :"+ p);
              while(a!=0){
                  rem=a%10;
                  p=p*10+rem;
                  a=a/10;
}

OutPut:

2
65
P is :0
56
78
P is :56
5687
AJ.
  • 4,526
  • 5
  • 29
  • 41
0

Probably you are just learning how to do things; thus you want to solve this problem "mathematically", but there is a different perspective in here.

It seems that you simply want to reverse the digits within any number; and you treat this as mathematical problem. But one can see this as string manipulation problem; which allows you to use existing functions to solve the problem:

int originalNumber = ....
String originalNumberAsString = Integer.toString(originalNumber);
String reversedNumberAsString = new StringBuilder(originalNumberAsString).reverse().toString();
int reversedNumber = Integer.parseInt(reversedNumberAsString);

Please note: there is also no need to check for the size of your input here; but you probably should check for >=0 (or do what is appropriate in your case for negative numbers).

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

There are couple of mistakes in your code (arguably), if reviewed by peer. You are trying to print p, which is not being re-initialized. Lot of unused variables in your code are removed in this answer.

  • You just declared an array, but never used in your program. Not sure whether you wanted to store the user inputs in the array and once all the inputs are received, you might want to compute the reverse and display or compute the reverse and store them in your array directly. So, I just removed it
  • The check a <= 1000000000 I believe is to ensure that values entered are within int range. Java provides you a constant for this in Integer wrapper class, Integer.MAX_VALUE (which is 2^31 - 1)
  • The code was lacking modularity. Hence, I extracted it to the method reverse(int num) which takes care of computation
  • Importing all classes from the package is not recommended, rather import only those classes that are used in your code.

    import java.util.Scanner;

    public class GFG {

    public static void main(String[] args) throws Exception {
        Scanner ss = new Scanner(System.in);
        int T = ss.nextInt();
    
        for (int i = 1; i <= T; i++) {
            int a = ss.nextInt();
    
            if (a <= Integer.MAX_VALUE) {
                a = reverse(a);
                System.out.println(a);
            } else {
                System.out.println("wrong input");
            }
        }
    }
    
    private static int reverse(int num) {
        int reverse = 0;
        while( num != 0 ) {
            reverse = reverse * 10;
            reverse = reverse + num % 10;
            num = num/10;
        }
        return reverse;
    }
    

    }

On a side note, if you look at GhostCat's answer of converting the number to String and reversing it, the approach fails for negative numbers.

Example: When user input is -51, the output would be 15-

However, it is common practice in industry to store certain long values as String. One such example is credit/debit card numbers

Community
  • 1
  • 1
JavaHopper
  • 5,567
  • 1
  • 19
  • 27
  • What is the point of asking if an int value is eq/less than Integer.MAX_VAUE? This test will **always** return true?! – GhostCat Aug 04 '16 at 15:51
  • @GhostCat, I was just showing the OP how we can use built in constants to put such checks. I understand it doesn't make sense, but it is not clear from the question, as to why the OP has put such check in place – JavaHopper Aug 04 '16 at 16:04
  • And hint: your code formatting is broken. That is ok for his input; but you should do better! – GhostCat Aug 04 '16 at 16:21
  • Not sure what's going wrong, but couldn't format it properly, the first 2 lines and the last one are going out of format – JavaHopper Aug 04 '16 at 16:46