Results 1 to 7 of 7
  1. #1
    fugazi is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Very basic question regarding user input

    Hi there, I'm new to this forum and Java in general. I was just wondering if you can give me a little bit of help with my query on user input. I am pretty much a complete newbie and learning Java from the ground up so please bear with me.

    Basically, what I am trying to do is give the user a choice of two options, and depending on what option they choose, the program will do one of two things.

    All I have so far is the following:

    Java Code:
    import java.util.Scanner;
    
    public class options
    {
    
    	public static void main (String[] args) 
    	{
            Scanner scan = new Scanner(System.in);
            System.out.print("Please enter 'A' for option A, or 'B' for option 'B': ");
            String option = scan.nextLine();
            }
    
    }
    From there, I want the program to do either one thing or the other (I haven't decided what it will do yet!), depending on what option the user chooses. I understand this is about as basic as you can get

    Any help would be greatly appreciated.

    Many thanks
    fugazi

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default

    Methinks you're looking for an if statement?

    Java Code:
    String animal = "cat";
    
    if(animal.equals("cat")){
       System.out.println("Meow!");
    }
    else if(animal.equals("tribble"){
       System.out.println("trill, trill, purr, purr");
    }
    else{
       //...
    }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Well, the user typed something, pressed enter and the result ends up as a String value named 'option'. You can do with that String whatever you want, e.g.

    Java Code:
    if (option == null) 
       // the user typed an end-of-fIle key combination
    // get rid of any leading and trailing white space
    option= option.trim();
    if (option.length() == 0)
       // the user just pressed the enter key
    if (option.charAt(0) == 'A')
       // the user typed an A
    ...
    It's up to your imagination to do with the input what you want.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    fugazi is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    Many thanks Kevin for your very prompt reply!

    I had been messing around with the if statement but my condition left a lot to be desired:

    if(option = ("A"))

    Like I said, I'm a complete newbie. I should hopefully now be able to make some progress!

    Thanks again
    fugazi

  5. #5
    fugazi is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    Thank you Jos for your informative reply.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default

    Quote Originally Posted by fugazi View Post
    Many thanks Kevin for your very prompt reply!

    I had been messing around with the if statement but my condition left a lot to be desired:

    if(option = ("A"))

    Like I said, I'm a complete newbie. I should hopefully now be able to make some progress!

    Thanks again
    fugazi
    Well, you're making two mistakes there.

    First off, assignment is done with a single equals sign, but equality is checked with two equals signs:

    Java Code:
    int i = 0;
    if(i == 0){
    //...
    Second off, when dealing with Objects, testing for equality using == is testing whether the references are equal, not the content. Instead, use .equals when testing for an Object's content.

    This might demonstrate what I mean:
    Java Code:
    Integer i = new Integer(7);
    Integer j = new Integer(7);
    System.out.println(i == j); //false, because they are not the same instance
    System.out.println(i.equals(j)); //true, because they have the same content
    This is especially troublesome with Strings because of the String pool, as == might actually work some of the time, leading you to believe that everything is hunky dory when in fact it is not.

    Hope that helps.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by KevinWorkman View Post
    This might demonstrate what I mean:
    Java Code:
    Integer i = new Integer(7);
    Integer j = new Integer(7);
    System.out.println(i == j); //false, because they are not the same instance
    System.out.println(i.equals(j)); //true, because they have the same content
    This is especially troublesome with Strings because of the String pool, as == might actually work some of the time, leading you to believe that everything is hunky dory when in fact it is not.

    Hope that helps.
    It sure helps; but have a look at this (a continuation of your example)

    Java Code:
    System.out.println(i == 7); // true because of that nasty auto boxing
    Maybe we'd better shouldn't uncover this tricky stuff ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. 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
  2. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 01:59 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. Basic keyboard input and edit
    By BHCluster in forum New To Java
    Replies: 18
    Last Post: 08-08-2008, 11:52 PM

Posting Permissions

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