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

• November 25th, 2011, 01:09 PM
mwebb
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 :rolleyes:).

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:

Code :

```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.

Code :

```System.out.print("Enter Hexadecimal: "); hex = in.nextLine();   int hexInt = Integer.parseInt(hex); System.out.println(hex);```
• November 25th, 2011, 02:07 PM
Norm
Re: Get user input of letters w/ numerical values, and straight numbers.
Quote:

int A,a = 10;
The first variable gets the default value the other gets a value of 10;
• November 25th, 2011, 04:58 PM
mwebb
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?
• November 25th, 2011, 05:00 PM
Norm
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.
• November 26th, 2011, 11:24 AM
mwebb
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?
• November 26th, 2011, 11:36 AM
Norm
Re: Get user input of letters w/ numerical values, and straight numbers.
Yes, that would set the values of both those variables.
• November 26th, 2011, 04:07 PM
mwebb
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...
• November 26th, 2011, 04:18 PM
Norm
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.
• November 26th, 2011, 05:02 PM
mwebb
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?
• November 26th, 2011, 05:26 PM
Norm
Re: Get user input of letters w/ numerical values, and straight numbers.
Quote:

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?

Quote:

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?

Quote:

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.
• November 26th, 2011, 07:42 PM
mwebb
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.
• December 5th, 2011, 06:48 PM
Junky
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.
• December 5th, 2011, 07:19 PM
Norm
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?
• December 6th, 2011, 01:17 PM
mwebb
Re: Get user input of letters w/ numerical values, and straight numbers.
Quote:

Originally Posted by Junky
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
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.