Hey everyone, im new to java and been watching tutorials online and stuff. Ive been trying to create this simple program and been having problems. Ive been trying to figure out how to give an string input worth a number value. For example say the user inputs the word "cat". I would want that "cat" to have a number value so i can use in later to solve a problem. Any help would be apreciated thanks.

12-07-2011, 04:10 AM

Junky

Re: User input

What value would "cat" have? Anyway try a Map.

12-07-2011, 04:14 AM

the ole buc

Re: User input

It would be like 16, i have no idea what a Map is lol, could you explain?

12-07-2011, 04:24 AM

Norm

Re: User input

Map is a class. Read the API doc for a complete description.
It's a way to associate a String like "cat" with a value like 16.

12-07-2011, 04:30 AM

the ole buc

Re: User input

Oh i see, ty norm. Ill give it a look thru

12-08-2011, 06:44 PM

CodeAdmiral

Re: User input

Try something like this inside of a try block...

Scanner cat = (int) int cat

the int inside paran. is a cast, which means it will try to set the Scanner as an int, but will throw and exception if it can't.

Hope that helps :(happy):

12-08-2011, 06:59 PM

Norm

Re: User input

@CodeAdmiral
What are you trying to show in your code sample?
Try putting that statement in a compiler and count the number of error messages it causes.
Can you rewrite your example to something that will compile?
Otherwise you are just confusing the OP.

12-08-2011, 07:12 PM

CodeAdmiral

Re: User input

I got it to work once... In a book I have (entitled Head First Java) they were able to change an input (string) into an int.
:s:

12-08-2011, 07:16 PM

Norm

Re: User input

What does that have to do with the "code" you posted.
It's not close to being anything useable.

12-08-2011, 07:16 PM

CodeAdmiral

Re: User input

Well, you are right. Sorry.

12-08-2011, 07:16 PM

al_Marshy_1981

Re: User input

Well Map was the right choice whether you believed that weird cast would work or not.

12-08-2011, 07:17 PM

CodeAdmiral

Re: User input

Then what is a cast good for?

12-08-2011, 10:17 PM

the ole buc

Re: User input

@Norm
Idk if im doing this right but when i run the program when i type in "bronze" it doesnt return the value i gave it. Do i have use a different type of scanner so it can see this? I know i can use "Get" but i need it to be from the user. Any ideas?

import java.util.HashMap;
import java.util.Scanner;

class putoway{
public static void main (String args[]){
HashMap<String,Double> bartype = new HashMap<String,Double>();
Scanner smithing = new Scanner(System.in); // How would i make the scanner know the word "bronze" has a value of 12.5?
bartype.put("Bronze",12.5);

You can use casting to convert one type to another SIMILAR type. For example you can cast a long to an int/short/byte. You can cast a double/float to a long/int/short/byte. What you cannot do is cast a Dog to a Chair. Makes sense doesn't it. You also cannot cast a String to an int. Why? What integer value would "hello world" have? But but but you say "123" can be cast to 123. But if they allowed that then how is the compiler or JVM supposed to handle Strings that do not contain a valid number as per my example above?

12-08-2011, 11:39 PM

Norm

Re: User input

Quote:

it doesnt return the value i gave it.

Add some printlns to your code to show what the variables contain and
Please show the console with the output that shows what you are doing.

System.out.println(smithing.nextLine());
What do you want to happen to the value that is read here with the nextLine method? It will only be printed. It does not get assigned to a variable.

12-08-2011, 11:42 PM

CodeAdmiral

Re: User input

Alright, thank you Junky

12-11-2011, 08:08 PM

Norm

Re: User input

Did you change your code to read into a variable and then use the contents of the variable?
See post#14