-2

I'm a Java beginner so please pardon me if the question seems silly but I already searched the forums but it seems like no one has my problem.

I need to reverse the digits of an integer, and my class hasn't covered while or if loops yet, so I can't use those. All answers I can find on stackoverflow use those, so I can't use those.

the input I am given is below 10000 and above 0 and the code I have written has no problem reversing the integer if the input is 4 digits (e.g. 1000 - 9999) but once the input is between 1 - 999 it creates zeroes on the right hand side but according to the answer sheets its wrong.

For example: 1534 gets turned into 4351, but 403 becomes 3040 instead of the 304 it should be, and 4 becomes 4000 instead of 4.

I've tried different things in the code but it seems to just keep giving the same answer. Or maybe I'm just missing some key mathematics, I'm not sure.

Scanner scan = new Scanner(System.in);
System.out.println ("Enter an integer:");

int value = scan.nextInt();    
int digit = (value % 10); 
value = (value / 10); 
int digit2 = (value % 10); 
value = (value / 10); 
int digit3 = (value % 10); 
value = (value / 10); 
int digit4 = (value % 10); 
String reversednum = ("" + digit + digit2 + digit3 + digit4);
System.out.println ( reversednum);

and

 Scanner scan = new Scanner(System.in);
 System.out.println ("Enter an integer:");

int value = scan.nextInt();
int digit = (value % 10);  
int reversednum = (digit);
value = (value /10); 
digit = (value % 10); 
reversednum = (reversednum * 10 + digit); 
value = (value / 10); 
digit = (value % 10); 
reversednum = (reversednum * 10 + digit); 
value = (value / 10); 
digit = (value); 
reversednum = (reversednum * 10 + digit); 
System.out.println (reversednum);

What am I doing wrong?

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Cas
  • 11
  • 2
  • 3
    Welcome to Stackoverflow. Please be aware that while it's okay to ask about homework questions on SO, you are still expected to have read ["how to ask a good quesiton"](/help/how-to-ask) and follow that advice. Just explain what you need to do, what you did, why that didn't work, what you tried to fix it (including what you (re)searched and didn't work. That part is important to know so people won't suggest what you already did). Also try to be concise: you start with a list that talks about "the input number". Which number? Remember: your post is where details go, the title's just a title. – Mike 'Pomax' Kamermans Mar 20 '19 at 23:54
  • @GBlodgett I already saw that post but I don't think it answers my problem or I just don't understand something. Thank you for answering – Cas Mar 20 '19 at 23:58
  • Again, welcome to StackOverflow. In addition to the above, please also do not link to images on external sites, but instead ensure that elements like "expected results" are in the text of the question (remember that questions will remain viewable for years, but your external site may disappear next week, rendering the question unusable for future readers). Offhand, for your problem, the important point from the expected results is that you do not want to append to the result string after "(value / 10)" reaches zero. – racraman Mar 21 '19 at 00:04

4 Answers4

1

You can convert from int to String -> reverse String -> convert again in int. This is a code example.

public int getReverseInt(int value) {
    String revertedStr = new StringBuilder(value).reverse().toString();
    return Integer.parseInt(revertedStr);   
}
lquitadamo
  • 633
  • 9
  • 22
0

Your code assumes that the number can be divided by 1000, which is clearly not the case for numbers below 1000. So add some if statements:

public int reverseNumber(int n) {
  // step one: we find the factors using integer maths
  int s = n;
  int thousands = s / 1000; // this will be 0 if the number is <1000
  s = s - thousands*1000;
  int hundreds = s / 100;   // this will be 0 if the number is <100
  s = s - hundreds*100;
  int tens = s / 10;        // etc.
  s = s - tens*10;
  int ones = s;

  // then: let's start reversing. single digit?
  if (n<10) return n;

  // two digits?
  if (n<100) {
    return ones*10 + tens;
  }

  // etc.
  if (n<1000) {
    return ones*100 + tens*10 + hundreds;
  }

  if (n<10000) {
    return ones*1000 + tens*100 + hundreds*10 + thousands;
  }

  // if we get here, we have no idea what to do with this number.
  return n;
}
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • Thank you for the answer. I see what you did with the " if " statements. Is it possible to reverse 1 to 4 digit integers without using "if" statements (doesn't matter if it makes the code longer) or is it a must? Sorry I forgot to mention that we haven't gone through "if" statement as well. But I'm willing to learn if that's what's needed. Thanks for your time. – Cas Mar 21 '19 at 00:22
  • If you got this question without having covered `if`, or `while`, or `for`, then it feels like you missed something in class, because this is not the kind of exercise you give without either explaining the built in java functions (such as explained in a different answer), or by explaining code logic. – Mike 'Pomax' Kamermans Mar 21 '19 at 00:32
0

Using modulus and division:

  int nbr = 123;  // reverse to 321 or 3*10*10 + 2*10 + 1
  int rev = 0;
  while(nbr > 0) {
     rev *= 10;             //  shift left 1 digit
     int temp = nbr % 10;  // get LO digit
     rev += temp;    // add in next digit
     nbr /= 10;    // move to next digit
  }

Or a recursive method:

   public static int reverseInt(int number, int value) {
      switch(number) {         // is this conditional statement allowed???
         case 0:
            return value;
      }
      value *= 10;
      int lod = number % 10;
      value += lod;
      number /= 10;
      return reverseInt(number, value);
   }
NormR
  • 336
  • 3
  • 12
0

Without spoon-feeding you code (leaving the value of writing your own homework code intact)...

Although you've said you can't use a loop, I don't think there's a sane approach that doesn't use one. Your basic problem is you have hard-coded a solution that works when the number happens to have 4 digits, rather than using code that adapts to a variable length. ie, are not using a loop.

All is not lost with your code however. You have figured out the essence of the solution. You just need to convert it to work processing one digit at a time. Consider using recursion, that divides the number by 10 each time and continues until the number is zero. Of course, you’ll have to capture the end digit before it’s lost by division.

Pseudo code may look like:

  • pass in the number and the current result
  • if the number is 0 return result
  • multiply result by 10 and add remainder of number divided by 10
  • return the result of calling self with number divided by 10 and result

then call this passing number and zero

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • even when the question says they haven't covered for/while yet and they probably shouldn't be using one? – Mike 'Pomax' Kamermans Mar 21 '19 at 00:33
  • @mike hmmm. good point. but I don't see any sane approach that doesn't use a loop. I've put a caveat in my answer... – Bohemian Mar 21 '19 at 00:53
  • recursion? *reverse* (N) => (N%10) in front of *reverse* (N/10), where "in front of" needs a little further design. –  Mar 21 '19 at 01:24