Results 1 to 8 of 8
  1. #1
    mcgillstudent is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Unhappy Simple Java Help: Inverse of 4 digit Number without Strings

    I have searched the forum already and not found anything applicable to my problem. So I need to be able to invert ANY 4 digit number using java. Ex. Input 1234 --> Output 4321

    I'm receiving the wrong answer with what I currently have:

    public class InvertID {
    public static void main(String[]args){
    System.out.println("Enter ID");
    java.util.Scanner keyboardReader;
    keyboardReader = new java.util.Scanner(System.in);
    int ID = keyboardReader.nextInt();
    int A,B,C,D;
    A=(ID/1000);
    B=(ID-(A*1000))/100;
    C=(ID-((B*100)+A))/10;
    D=(ID-(A*1000)-(B*100)-(C*10));
    System.out.println("Your Inverted ID is:");
    System.out.print(D);
    System.out.print(C);
    System.out.print(B);
    System.out.println(A);
    }
    }

    Answer = -99610321

    I have no idea what I'm doing wrong.

    This is a question on an assignment and I'm not allowed to convert to strings (I think) then back again, as it needs to remain basic.

    Any help is MUCH appreciated. Thank you so much!

  2. #2
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: Simple Java Help: Inverse of 4 digit Number without Strings

    look up modulus operation and there will be your answer
    int a = 1234
    int temp = a%10

  3. #3
    mcgillstudent is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default Re: Simple Java Help: Inverse of 4 digit Number without Strings

    Quote Originally Posted by Danieldcc View Post
    look up modulus operation and there will be your answer
    int a = 1234
    int temp = a%10
    I don't seem to understand. I watched a video explaining Modulus. But now the answer is: 410321

    with
    public class Learning {
    public static void main(String[]args){
    System.out.println("Enter ID");
    java.util.Scanner keyboardReader;
    keyboardReader = new java.util.Scanner(System.in);
    int ID = keyboardReader.nextInt();
    int A,B,C,D;
    A=(ID/1000);
    B=(ID-(A*1000))/100;
    C=((ID-((B*100)+A))/10);
    D=ID%10;
    System.out.println("Your Inverted ID is:");
    System.out.print(D);
    System.out.print(C);
    System.out.print(B);
    System.out.println(A);
    }
    }

  4. #4
    serotonin is offline Member
    Join Date
    Jan 2013
    Location
    england
    Posts
    9
    Rep Power
    0

    Default Re: Simple Java Help: Inverse of 4 digit Number without Strings

    i don't think the use of modulus is the problem..

    C=((ID-((B*100)+A))/10);

    you are missing something in the above line of code, you used it correctly in the other lines of code, get this line correct and the code works

  5. #5
    serotonin is offline Member
    Join Date
    Jan 2013
    Location
    england
    Posts
    9
    Rep Power
    0

    Default Re: Simple Java Help: Inverse of 4 digit Number without Strings

    i don't think the use of modulus is the problem..

    Java Code:
    C=((ID-((B*100)+A))/10);
    you are missing something in the above line of code, you used it correctly in the other lines of code, get this line correct and the code works

  6. #6
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: Simple Java Help: Inverse of 4 digit Number without Strings

    Quote Originally Posted by mcgillstudent View Post
    I don't seem to understand. I watched a video explaining Modulus. But now the answer is: 410321

    with
    public class Learning {
    public static void main(String[]args){
    System.out.println("Enter ID");
    java.util.Scanner keyboardReader;
    keyboardReader = new java.util.Scanner(System.in);
    int ID = keyboardReader.nextInt();
    int A,B,C,D;
    A=(ID/1000);
    B=(ID-(A*1000))/100;
    C=((ID-((B*100)+A))/10);
    D=ID%10;
    System.out.println("Your Inverted ID is:");
    System.out.print(D);
    System.out.print(C);
    System.out.print(B);
    System.out.println(A);
    }
    }
    Quick way i did it....you might want to put it in a loop so you can have a longer number but here you are....

    Java Code:
    int a = 4321;
    		
    		int tempa = a%10;
    		a /= 10;
    		int tempb = a%10;
    		a /= 10;
    		int tempc = a%10;
    		a /= 10;
    		int tempd = a%10;
    		a /= 10;
    		System.out.print(tempa);
    		System.out.print(tempb);
    		System.out.print(tempc);
    		System.out.print(tempd);

  7. #7
    serotonin is offline Member
    Join Date
    Jan 2013
    Location
    england
    Posts
    9
    Rep Power
    0

    Default Re: Simple Java Help: Inverse of 4 digit Number without Strings

    danielcc produced the correct solution.
    but just so you know, your original code actually works for 4 digits if you change
    Java Code:
    C=(ID-((B*100)+A))/10
    to
    Java Code:
    C=(ID-((B*100)+(A*1000)))/10

  8. #8
    mcgillstudent is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default Re: Simple Java Help: Inverse of 4 digit Number without Strings

    Thanks everyone. I ended up solving it. The problem was with C, and I used an alternative method to "serotonin." Thanks for the input and I hope to be able to give back to the community as my knowledge continues to grow. Cheers.

Similar Threads

  1. Possible combinations for a n- digit number???
    By anishr6 in forum New To Java
    Replies: 4
    Last Post: 05-25-2011, 08:55 AM
  2. Replies: 0
    Last Post: 01-23-2011, 12:37 AM
  3. select a digit in a number
    By navid in forum New To Java
    Replies: 3
    Last Post: 12-12-2010, 10:47 AM
  4. Replies: 9
    Last Post: 07-17-2010, 01:21 PM
  5. convert getValue result in a 4 digit number
    By roseline43 in forum New To Java
    Replies: 0
    Last Post: 09-02-2008, 08:44 PM

Tags for this Thread

Posting Permissions

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