Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Get user input of letters w/ numerical values, and straight numbers.

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Get user input of letters w/ numerical values, and straight numbers.

    I don't know why I am having such a hard time figuring this out, but here's my seemingly simple situation (Try and figure out what I'm trying to make ).

    I got 12 variables, 2 of each equal the same numerical value, and a variable known as hex which I have made a String currently; which I have demonstrated as such:

    String hex;
    int A,a = 10;
    int B,b = 11;
    int C,c = 12;
    int D,d = 13;
    int E,e = 14;
    int F,f = 15;

    Now I am prompting the user to enter either one of the several listed variables (they will already know which are allowed to enter) and/or any one-digit number. (1) If 694 is entered, it should be read as '6' and '9' and '4', not as a three digit number. I don't want the data to be mixed up into one big number value either, I need each number to be accepted as is and not added in.

    (2) Thing is, I'm not exactly sure how to accept both kinds of data while maintaining the variables values, and without an error. (3) I need the data to be displayed back as is, just like how the user entered it. So if the user entered AD6F9, I need it to be displayed like that but still have A = 10, D = 13, 6 = 6, F = 15, and 9 = 9.

    This is what I have for displaying the output, which isn't working the way I like right now.

    System.out.print("Enter Hexadecimal: ");
                hex = in.nextLine();
     
                int hexInt = Integer.parseInt(hex);
                System.out.println(hex);
    Last edited by mwebb; November 26th, 2011 at 05:03 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    int A,a = 10;
    The first variable gets the default value the other gets a value of 10;

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    So really just a = 10? What value does the A have then? Its ASCII value, or is it just A?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    In the code you posted, A is an int variable with the default value of 0.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    Oh so I should have done it like:

    int A = 10, a = 10;

    if I want both to equal 10?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    Yes, that would set the values of both those variables.

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    I see, well now that I got that established, my other current major issues are numbered in my starting post...

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    Please post what you are currently working on and would like help with.
    Show any code that you are having problems with and add an explanation of what your problem is.

  9. #9
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    *Sigh*, well I am trying to add a feature to a converter script of mine that also accepts Hexadecimal input and outputs it in decimal, binary, and text. There's not really any code that is causing trouble, it's more like coming up with code to solve my (1), (2), & (3). I guess I'll tackle this one at a time.

    Lets start with (2), I'm not sure how to accept those a,A,b,B,c,C,d,D,e,E, variables that are declared and initialized, into an input that's accepted as numbers. Because when the user is prompt, and they enter a letter from those list of variables, its accepted as a letter regardless. I can't do in.next(); because it will take it as a String, and I can't seem to do in.nextInt() because it takes it only sees it as a letter and returns an error. You see what I mean?

    In brief: Can I get user input to accept both letters and numbers without error?
    Last edited by mwebb; November 26th, 2011 at 06:19 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    Can I get user input to accept both letters and numbers without error?
    How are you reading the user's input? Are you using the Scanner class to read in a String?

    how to accept those a,A,b,B,c,C,d,D,e,E, variables that are declared and initialized, into an input that's accepted as numbers.
    This doesn't make any sense to me. How are the 10 variables you show related to what the user inputs?
    Why did you chose those variable names?

    when the user is prompt, and they enter a letter from those list of variables, its accepted as a letter regardless.
    How do you think that the user's input has a relationship with the names of your variables?

    Please explain what you want the user to enter?
    If you want both letters and digits as input you will have to read the input as a String.

  11. #11
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    Yes I am using the Scanner class to read.

    Those 10 variables are the only letters the user can enter, and any number. I chose those because they are necessary for Hexadecimal code lines, like 0xF7E902A.

    It has a relationship because if the user enters in one of those letter variables, it would represent whatever number it is assigned to since Hexadecimal only uses single digit representing numbers and A-F to represent 10-15. Basically I'm trying to recreate the workings of the Hexadecimal number system.

    I want the user to enter a some sort of Hexadecimal line. I recently included the 0x so that the user wouldn't have to enter that part.

    And thanks for the hint, I figured I should accept it as a String but I couldn't decide if accepting it as and integer (somehow) then converting it to String later on would be more beneficial.

  12. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    Here is how I would do it. Get rid of all those variables. Read user input as a String. Iterate over the String one char at a time and use a switch statement.
    Improving the world one idiot at a time!

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    What do you intend to do with the variables: a,A, ... f,F?
    Will they be declared as final and their values never changed?

    How will you use those variables to convert what the user enters to hexidecimal?

  14. #14
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Get user input of letters w/ numerical values, and straight numbers.

    Quote Originally Posted by Junky View Post
    Here is how I would do it. Get rid of all those variables. Read user input as a String. Iterate over the String one char at a time and use a switch statement.
    Might have to go that route, though I am not familiar with a "switch statement".

    Quote Originally Posted by Norm View Post
    What do you intend to do with the variables: a,A, ... f,F?
    Will they be declared as final and their values never changed?

    How will you use those variables to convert what the user enters to hexidecimal?
    The data in the variables will just be used to compute an answer from hex to decimal, binary, and char, and vice versa. Though I will need it to be printed later too, the variables themselves that is, to the terminal screen. I never thought of declaring them as a final.. but no, their values will never change.

    I'm going to use them in some sort of algorithm that is commonly or uncommonly used by people to figure out hex on paper.

    ---Now that I'm thinking about it, I might have to just use if and else if statements to give me what I want. If it seems not much else can be done.
    Last edited by mwebb; December 6th, 2011 at 02:20 PM.

Similar Threads

  1. Straight and Straight Flush?!?!
    By hiimjoey11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2010, 10:44 PM
  2. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM
  3. [SOLVED] Method of Sorting? by assiging numerical values to Strings
    By Mirage in forum Java Theory & Questions
    Replies: 0
    Last Post: June 16th, 2010, 07:49 PM
  4. HELP! Input, numbers, nonsense
    By Corvus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2010, 03:18 PM
  5. Generating random letters and numbers together
    By newJava in forum Java Theory & Questions
    Replies: 3
    Last Post: March 19th, 2010, 04:08 AM