Results 1 to 16 of 16
  1. #1
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Reverse a number?

    How to Input a number from the user using Scanner class method nextInt. And use a for loop to display the number in reverse.
    For example, if the user types 1234, the program should display:
    The number in reverse is 4321
    For example, if the user types 370, your program should display:
    The number in reverse is 073
    I CANNOT use String, StringBuffer, arrays, or other advanced structures in this problem.

    This is my code and i its not printing the reverse. yes i do have the import scanner in my program and the }//end class. Im new to programing and need help please
    Java Code:
    public static String Problem7() {
    System.out.println ("\nThis is problem # 7\n");
    
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the Number: ");
    String input = scan.nextLine();
    String reverse = "";
    for(int i = input.length() - 1; i >= 0; i--) {
    reverse += input.charAt(i);
    }
    return reverse;
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Reverse a number?

    1. There is no print statement you just return the whole string, so you have to put the method call in a print statement -> System.out.print(Problem7())!
    2. You should use nextInt and not nextLine! (take a little math to help :))

  3. #3
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reverse a number?

    oh thanks i didnt catch that! but now that i changed it, my code doesnt work now

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Reverse a number?

    How do you change your code? and what is doesn't work? Please tell the details by posting your new code and the error message that you have there.

  5. #5
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reverse a number?

    Quote Originally Posted by wsaryada View Post
    How do you change your code? and what is doesn't work? Please tell the details by posting your new code and the error message that you have there.
    Java Code:
    //
    // problem # 7 - reverse numbers
    //
    
    public static int Problem7() {
       System.out.println ("\nThis is problem # 7\n");
       
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the Number: ");
       int reverse;
       int input = scan.nextInt();
       
          for(int i = input.length() - 1; i >= 0; i--) {
             reverse=input.charAt(i);
          }
          return reverse;
          }
    The errors are:
    Lab6A.java:184: int cannot be dereferenced
    for(int i = input.length() - 1; i >= 0; i--) {
    ^
    Lab6A.java:185: int cannot be dereferenced
    reverse=input.charAt(i);
    ^
    2 errors

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Reverse a number?

    Hi,

    The type of input is int. And an int is a primitive. So it doesn't know methods like length() or charAt(). You cannot just change the variable type from String to int and expect everything will be fine :)

  7. #7
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reverse a number?

    Quote Originally Posted by wsaryada View Post
    Hi,

    The type of input is int. And an int is a primitive. So it doesn't know methods like length() or charAt(). You cannot just change the variable type from String to int and expect everything will be fine :)
    if you dont mind would you help me further please. ive been working on these 7 codes for hours and i just need a bit of help:(

  8. #8
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Reverse a number?

    your code is wrong in this line for(int i = input.length() - 1; i >= 0; i--) because input is a integer so input.length() display error.
    you use this code

    Java Code:
      public static String Problem7() {
    System.out.println ("\nThis is problem # 7\n");
     
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the Number: ");
    String input = scan.nextLine();
    String reverse = "";
    for(int i = input.length() - 1; i >= 0; i--) {
    reverse += input.charAt(i);
    }
    return reverse;
    }
    which you post at first and call that method like below...

    Java Code:
     public static void main(String[] args)
       {
           Reverse re=new Reverse();
           String result=re.Problem7();
           System.out.println(result);
           
       }
    I hop this will be work for you....
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  9. #9
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reverse a number?

    Quote Originally Posted by tamilarasi View Post
    your code is wrong in this line for(int i = input.length() - 1; i >= 0; i--) because input is a integer so input.length() display error.
    you use this code

    Java Code:
      public static String Problem7() {
    System.out.println ("\nThis is problem # 7\n");
     
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the Number: ");
    String input = scan.nextLine();
    String reverse = "";
    for(int i = input.length() - 1; i >= 0; i--) {
    reverse += input.charAt(i);
    }
    return reverse;
    }
    which you post at first and call that method like below...

    Java Code:
     public static void main(String[] args)
       {
           Reverse re=new Reverse();
           String result=re.Problem7();
           System.out.println(result);
           
       }
    I hop this will be work for you....
    thank you, but this was my first code. but i had to change it to nextInt() and thats what messed me up

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Reverse a number?

    Humble humans use iterations for everything, the divine spirits use recursion: suppose a number is less than ten: to print its reverse you simply print the number (it's a single digit); otherwise you print the last digit (n%10) followed by the reverse of the number except for the last digit (n/10).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Reverse a number?

    Quote Originally Posted by Valerie10b View Post
    thank you, but this was my first code. but i had to change it to nextInt() and thats what messed me up

    you try to reverse the number using integer.. you follow this link...

    http://www.java-examples.com/reverse-number-using-java

    you change your code and call this code your main method
    Last edited by tamilarasi; 03-12-2013 at 10:13 AM. Reason: Removed spoonfeeding
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Reverse a number?

    tamilarasi, please provide guidance not full code solutions. We're all here to learn, not to copy.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reverse a number?

    Quote Originally Posted by tamilarasi View Post
    you try to reverse the number using integer.. you follow this link...

    Reverse Number using Java | Java Examples - Java Program Sample Source Code

    you change your code and call this code your main method
    thank you i appreciate it!

  14. #14
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reverse a number?

    Quote Originally Posted by DarrylBurke View Post
    tamilarasi, please provide guidance not full code solutions. We're all here to learn, not to copy.

    db
    BLAH BLAH BLAH.

  15. #15
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reverse a number?

    Quote Originally Posted by JosAH View Post
    Humble humans use iterations for everything, the divine spirits use recursion: suppose a number is less than ten: to print its reverse you simply print the number (it's a single digit); otherwise you print the last digit (n%10) followed by the reverse of the number except for the last digit (n/10).

    kind regards,

    Jos
    thank you very much!

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Reverse a number?

    Thread locked. PMs are being sent to tamilarasi and Valerie10b.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. I want to print a Decimal Number in reverse.
    By LazerJet in forum New To Java
    Replies: 2
    Last Post: 07-24-2012, 04:38 PM
  2. Replies: 10
    Last Post: 03-15-2012, 11:46 AM
  3. Replies: 8
    Last Post: 01-21-2012, 02:14 AM
  4. Dice help. posting the number of times a number is rolled.
    By cookiejarvus in forum New To Java
    Replies: 13
    Last Post: 12-05-2011, 12:08 AM
  5. Reverse Number guessing game
    By rarman555 in forum New To Java
    Replies: 6
    Last Post: 04-24-2011, 02:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •