Results 1 to 17 of 17
Thread: User input
- 12-07-2011, 03:02 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
User input
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, 03:10 AM #2
Re: User input
What value would "cat" have? Anyway try a Map.
- 12-07-2011, 03:14 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Re: User input
It would be like 16, i have no idea what a Map is lol, could you explain?
- 12-07-2011, 03:24 AM #4
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, 03:30 AM #5
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Re: User input
Oh i see, ty norm. Ill give it a look thru
- 12-08-2011, 05:44 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 9
- Rep Power
- 0
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
- 12-08-2011, 05:59 PM #7
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, 06:12 PM #8
Member
- Join Date
- Dec 2011
- Posts
- 9
- Rep Power
- 0
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.
- 12-08-2011, 06:16 PM #9
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, 06:16 PM #10
Member
- Join Date
- Dec 2011
- Posts
- 9
- Rep Power
- 0
Re: User input
Well, you are right. Sorry.
- 12-08-2011, 06:16 PM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: User input
Well Map was the right choice whether you believed that weird cast would work or not.
- 12-08-2011, 06:17 PM #12
Member
- Join Date
- Dec 2011
- Posts
- 9
- Rep Power
- 0
Re: User input
Then what is a cast good for?
- 12-08-2011, 09:17 PM #13
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
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);
double nbars, xpgained;
System.out.println("Enter bar type:");
System.out.println(smithing.nextLine());
System.out.println("How many bars are you using?:");
nbars = smithing.nextDouble();
System.out.println(smithing.nextDouble());
xpgained = nbars*_____; // how would i get the value of my hash here?
System.out.println("Xp gained;");
System.out.println(xpgained);
- 12-08-2011, 10:30 PM #14
Re: User input
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, 10:39 PM #15
Re: User input
Add some printlns to your code to show what the variables contain andit doesnt return the value i gave it.
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, 10:42 PM #16
Member
- Join Date
- Dec 2011
- Posts
- 9
- Rep Power
- 0
Re: User input
Alright, thank you Junky
- 12-11-2011, 07:08 PM #17
Similar Threads
-
Help with user input
By sconniegorilla in forum New To JavaReplies: 2Last Post: 02-16-2011, 02:00 PM -
Need help getting input(first/last name) from user
By nightrise420 in forum New To JavaReplies: 11Last Post: 09-11-2010, 03:09 AM -
how to get input from User
By Alvaro in forum New To JavaReplies: 7Last Post: 01-15-2010, 11:02 PM -
User input- Pop Up Box
By dedachi in forum AWT / SwingReplies: 3Last Post: 03-23-2009, 04:47 AM -
cant take input from user
By new_1 in forum New To JavaReplies: 6Last Post: 12-25-2007, 07:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks