Reverse Number and eliminate zero

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JanineXD
    New Member
    • Feb 2010
    • 18

    Reverse Number and eliminate zero

    How's this an error? can you help me find out please.

    thanks!

    Code:
    import java.io.*;
     import java.lang.*;
      public class ReverseTheNumber{
      	public static void main(String []args)throws IOException{
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    
      	try
      	{
      	int num;
      	DataInputStream in= new
      	DataInputStream(System.in);
    	
    	System.out.println("Reverse the Numbers:");
        num=Integer.parseInt(in.readLine());
      	int len=String.valueOf(num).length();
      	int[] digits= new int[len];
      	int i=0;
      	while( num !=0){
      	digits[i++]=num% 10;
      	num/=1000;
      	}
      	
      	for(i=0;i<digits.length;i++)
      	System.out.print(digits[i]);
      	}
      	catch(IOException e){}
      	}
      }
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    Perhaps you could say what the error is.

    Does it compile? If not, what is the compiler message? And what lines of your code is it referring to?

    Does it exhibit unexpected or unintended behaviour when you run it? If this is an exception, what is the stack trace? If not, what was the input, the expected output and the actual output?

    Comment

    • JanineXD
      New Member
      • Feb 2010
      • 18

      #3
      Here's what the error says:
      Code:
      C:\Documents and Settings\User\Desktop\ReverseTheNumber.java:10: in is already defined in main(java.lang.String[])
          DataInputStream in= new
                          ^
      Note: C:\Documents and Settings\User\Desktop\ReverseTheNumber.java uses or overrides a deprecated API.
      Note: Recompile with -Xlint:deprecation for details.
      1 error
      
      Process completed.

      someone just gave me the program, i'm not sure if its the correct Reverse number but eliminate any zeros program.

      Thank you for your help!

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        Is that the code you originally posted?

        Any way, you can't declare the variable in twice (ie both as a BufferedReader and as a DataInputStream ). So get rid of the DataInputStream . This will also help with the deprecation warning as the readLine() method of DataInputStream shouldn't be used.

        Comment

        • JanineXD
          New Member
          • Feb 2010
          • 18

          #5
          oh. I figured the problem. now the process is completed, but the output is wrong, I mean the program is wrong and won't reverse the number then eliminate the zero:

          here's the NEW code:
          Code:
          import java.io.*;
           import java.lang.*;
            public class ReverseTheNumber{
            	public static void main(String []args)throws IOException{
          BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
          
          
            	int num;
          	
          	System.out.println("Reverse the Numbers:");
              num=Integer.parseInt(in.readLine());
            	int len=String.valueOf(num).length();
            	int[] digits= new int[len];
            	int i=0;
            	while( num !=0){
            	digits[i++]=num% 10;
            	num/=1000;
            	}
            	
            	for(i=0;i<digits.length;i++)
            	System.out.print(digits[i]);
            	}
          
            	}
          and here's the output:
          Code:
          Reverse the Numbers:
          8420
          0800
          Process completed.
          the output is wrong it should be 248 instead of 0800

          Comment

          • pbrockway2
            Recognized Expert New Member
            • Nov 2007
            • 151

            #6
            Notice how a couple of the digits (the '4' and the '2') have disappeared completely. It's as if you skip over them.

            The problem is in the while loop. You might get a better indication of what is going on if you print out the value of num each time around the loop:

            Code:
            while( num !=0){
                System.out.println("num is " + num);
                digits[i++]=num% 10;
                num/=1000;
            }

            Comment

            • JanineXD
              New Member
              • Feb 2010
              • 18

              #7
              It still won't reverse:
              Code:
              import java.io.*;
               import java.lang.*;
                public class ReverseTheNumber{
                    public static void main(String []args)throws IOException{
              BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
               
               
                    int num;
               
                  System.out.println("Reverse the Numbers:");
                  num=Integer.parseInt(in.readLine());
                    int len=String.valueOf(num).length();
                    int[] digits= new int[len];
                    int i=0;
                 
              while( num !=0){
                  System.out.println("num is " + num);
                  digits[i++]=num% 10;
                  num/=1000;
              }
              }
              }
              the output is:
              Reverse the Numbers:
              706050
              num is 706050
              num is 706

              Process completed.

              Comment

              • pbrockway2
                Recognized Expert New Member
                • Nov 2007
                • 151

                #8
                Originally posted by JanineXD
                It still won't reverse:
                Putting in the println() wasn't supposed to make it work! You will only make it work by fixing the bug. The println() was supposed to highlight what the bug is.

                num is 706050
                num is 706
                Do you see how the while loop chopped off three digits from the end? Since you are building up the output string one digit at a time, it doesn't make sense to remove three digits each time you go around the loop.

                Change the line of code that removes the righthand three digits, so that it only removes one digit.

                Comment

                • JanineXD
                  New Member
                  • Feb 2010
                  • 18

                  #9
                  I still dont get it.

                  I can't find a number 3 in the program and I cant find what command is used to cut off the 3 digits

                  Comment

                  • pbrockway2
                    Recognized Expert New Member
                    • Nov 2007
                    • 151

                    #10
                    Originally posted by JanineXD
                    I still dont get it.

                    I can't find a number 3 in the program and I cant find what command is used to cut off the 3 digits
                    Well, there are just two lines in that while loop. So I don't think it will be too difficult to decide which of them is removing the righthand three digits (and how it has to be changed so that only one digit is removed.

                    Consult your textbook and figure out what each of the lines is doing. Neither are all that complex, but to progress from here you will have to understand the ++ and % operators as well as the two assignment operators: = and \=.

                    Comment

                    • jkmyoung
                      Recognized Expert Top Contributor
                      • Mar 2006
                      • 2057

                      #11
                      Why are you dividing by 1000?

                      num/=1000;

                      Comment

                      Working...