Results 1 to 7 of 7
- 01-06-2011, 05:40 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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:
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 getJava 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(); } }
Any help would be greatly appreciated.
Many thanks
fugazi
- 01-06-2011, 05:43 PM #2
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!
- 01-06-2011, 05:47 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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.
It's up to your imagination to do with the input what you want.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 ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-06-2011, 05:54 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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
- 01-06-2011, 05:58 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
Thank you Jos for your informative reply.
- 01-06-2011, 06:04 PM #6
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:
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.Java Code:int i = 0; if(i == 0){ //...
This might demonstrate what I mean:
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.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
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!
- 01-06-2011, 07:40 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Need help getting input(first/last name) from user
By nightrise420 in forum New To JavaReplies: 11Last Post: 09-11-2010, 03:09 AM -
User Input
By brmcdani in forum New To JavaReplies: 2Last Post: 02-05-2010, 01:59 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 -
Basic keyboard input and edit
By BHCluster in forum New To JavaReplies: 18Last Post: 08-08-2008, 11:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks