Results 1 to 17 of 17

Thread: User input

  1. #1
    the ole buc is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: User input

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

  3. #3
    the ole buc is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: User input

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

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

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

  5. #5
    the ole buc is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: User input

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

  6. #6
    CodeAdmiral is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default 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

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

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

  8. #8
    CodeAdmiral is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

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

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default Re: User input

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

  10. #10
    CodeAdmiral is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: User input

    Well, you are right. Sorry.

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: User input

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

  12. #12
    CodeAdmiral is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: User input

    Then what is a cast good for?

  13. #13
    the ole buc is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default 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);

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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?

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default Re: User input

    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.

  16. #16
    CodeAdmiral is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: User input

    Alright, thank you Junky

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default Re: User input

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

Similar Threads

  1. Help with user input
    By sconniegorilla in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 02:00 PM
  2. Need help getting input(first/last name) from user
    By nightrise420 in forum New To Java
    Replies: 11
    Last Post: 09-11-2010, 03:09 AM
  3. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  4. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 AM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM

Posting Permissions

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