0

I have to do a program that returns the reverse of a number that is input by a user, event the numbers that start and finish with 0 (ex. 00040, it would print 04000)

I was able to do the reverse of the number, but it doesn't print out the 0's and I can't use String variables, just long variables or integers.

Here is my code:

import java.util.Scanner;
public class Assignment_2_Question_2 {

        public static void main(String[] args) {
            Scanner keyboard = new Scanner (System.in);
            System.out.println("Welcome to Our Reversing Number Program");
            System.out.println("-----------------------------------------");
            System.out.println();

            System.out.println("Enter a number with at most 10 digits:");
            long number = keyboard.nextInt();

            long nbDigits = String.valueOf(number).length();

            System.out.println("Number of digits is " + nbDigits);

            System.out.print("Reverse of " + number + " is ");
            long revNumber = 0;
            while (number > 0){
                long digit = number % 10; 
                if (digit == 0){ // The teacher told me to add this
                    nb0 ++; // need to not take into account the 0's inside the number
                }
                revNumber = revNumber * 10 + digit;
                number = number/10;
            }
            for (int i = 0; i < nb0; i++) { // This will print the number of 0's counted by the if statement and print them out.
                  System.out.println("0");
            }       
            System.out.println(revNumber);
            String answer;

            do{
                System.out.println("Do you want to try another number? (yes to repeat, no to stop)");
                answer = keyboard.next();

                if (answer.equalsIgnoreCase("yes")){
                    System.out.println("Enter a number with at most 10 digits:");
                    long otherNumber = keyboard.nextInt();

                    long nbrDigits = String.valueOf(otherNumber).length();

                    System.out.println("Number of digits is " + nbrDigits);

                    System.out.print("Reverse of " + otherNumber + " is ");

                    long reversedNumber = 0;
                    while (otherNumber != 0){
                        reversedNumber = reversedNumber * 10 + otherNumber%10;
                        otherNumber = otherNumber/10;
                    }   
                    System.out.println(reversedNumber);
                }
                else
                    System.out.println("Thanks and have a great day!");
            }while(answer.equalsIgnoreCase("yes")&& !answer.equalsIgnoreCase("no"));
        }
    }

Can someone help me? Thank you

Jennifer
  • 115
  • 9
  • You are already using a `Scanner` variable and a `String` variable. Why can't you store the number in a `String` as well, at least temporarily until you count the number of digits? – that other guy Feb 12 '20 at 19:04
  • @thatotherguy I know that by using a string it will work, but this is an assignment I have to do and the teacher doesn't want us to use string variables – Jennifer Feb 12 '20 at 19:04
  • Are you going to delete the `String answer;` before you submit it? – that other guy Feb 12 '20 at 19:06
  • No, the string answer variable isn't returning an integer, I use it to be able to have the user input his decision to continue using the reverse program – Jennifer Feb 12 '20 at 19:08
  • Are you sure you can't store the user's answer to "Enter a number:" in a String for the purpose of getting its length, as long as you only use integer operations to reverse thee number? – that other guy Feb 12 '20 at 19:15
  • I am going to ask the teacher if I can do that – Jennifer Feb 12 '20 at 19:17
  • Ok, so I talked to my teacher, he said I can't use a String, but he told me that I had to add a condition inside my while loop. The condition should count only the number of 0's there are in the number input by the user that are at the beginning or the end of the input. – Jennifer Feb 13 '20 at 02:30
  • I don't know if this is clear – Jennifer Feb 13 '20 at 02:31
  • Note that you can print one digit at a time with `System.out.print` - and then conclude with a `System.out.println()` - so it would appear on output as a single number - this should get you thinking... –  Feb 13 '20 at 03:25
  • Note also this line `long number = keyboard.nextInt();` will prevent you from ever knowing if use entered leading zeros e.g. 00040. –  Feb 13 '20 at 03:45
  • @Andy, I know that the `keyboard.nextInt();` will prevent me from knowing if the user input a 0, but that is the point of the program, I will never know what is the input of the user – Jennifer Feb 13 '20 at 16:44
  • Of course you can if you receive it as a string. And your example in OP specifically states a test case of input '0040'. –  Feb 13 '20 at 17:10
  • Ok, I understand that by receiving the input as a string, I will be able to output the 0's, but I am not allowed to do so. As I said previously, the teacher doesn't want us to use variables of type String for the number input by the user – Jennifer Feb 13 '20 at 17:18
  • I see - in theory you could change the tokenizer of Scanner to be any character and receive each digit (as an integer) as they are typed. –  Feb 13 '20 at 17:30
  • How do I do that? Do I put `keyboard.next()`? – Jennifer Feb 13 '20 at 17:34
  • see https://stackoverflow.com/a/2913026/2711811. if needed I have a working example –  Feb 13 '20 at 17:50

2 Answers2

0

Probably not what is intended but clearly (based on problem statement) you must see all digits entered (to include leading 0's) otherwise it is an "impossible solution" - and you state you cannot receive input as a String...

So this snippet reads one digit at a time where each digit is received as an int:

Scanner reader = new Scanner(System.in);
reader.useDelimiter("");  // empty string
System.out.print("Enter number: ");

while (!reader.hasNextInt()) reader.next();

int aDigit;
int cnt = 0;

while (reader.hasNextInt()) {

    aDigit = reader.nextInt();
    System.out.println("digit("+ ++cnt + ") "+aDigit);

}

System.out.println("Done");

Prints (assume user enter 012 (enter)):

Enter number: digit(1) 0
digit(2) 1
digit(3) 2
Done

You naturally have more work to do with this but at least you have all user entered digits (including leading zeros).

0

You can use buffer reader; Like this given code And if you want to do some arithmetic operations in the numbers then you can convert it into int using parseInt method.:-

import java.util.Scanner;
import java.lang.*;
class Main {
 public static void main(String args[])
{
System.out.println("ENTER NUM");
Scanner SC = new Scanner(System.in);
String INP = SC.nextLine();
StringBuffer SB = new StringBuffer(INP);
SB.reverse() ;
System.out.println(SB);


}
}