Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Answer always false.

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Answer always false.

    Ok, take a look at this:
    import static java.lang.System.out;
    import java.util.*;
     
    public class mainMenu {
    	private static String Choice;
    	private static String Fight;
    	private static String Options;
    	private static String Info;
    	private static String Help;
    	private static String Quit;
    	//private static String choice;
    	private static String fight;
    	private static String options;
    	private static String info ;
    	private static String help;
    	private static String quit;
    	private static String Carrer;
    	private static String carrer;
     
    	public static void main(String[] args){
    		Scanner keyboard = new Scanner(System.in);
     
    		out.println("**************************************");
    		out.println("*  Choice  *          Info.          *");
    		out.println("**************************************");
    		out.println("*  Fight   *   Fight instantly!      *");
    		out.println("*  Carrer  *   Begin your carrer     *");
    		out.println("*  Options *   Coming Soon           *");
    		out.println("*  Info    *   Making of the game.   *");
    		out.println("*  Help    *   Learn how to play.    *");
    		out.println("*  Quit    *   Leave the game.       *");
    		out.println("**************************************");
    		out.println("Pick one:");
    		out.println("");
    		Choice = keyboard.nextLine();
    		out.println("Your choice was " + Choice);
    		if (Choice == Fight || (Choice == fight)) {
     
    		} else if (Choice == Carrer || (Choice == carrer)){
     
    		} else if (Choice == Options || (Choice == options)){
     
    		} else if (Choice == Info || (Choice == info)){
     
    		} else if (Choice == Help || (Choice == help)){
     
    		} else if (Choice == Quit || (Choice == quit)){
    			System.exit(1);
    		} else {
    			out.println("Not a choice.");
    		}
     
    	}
     
    }
    Ok, here is the point of the code, when you type a choice, it will become the 'choice' right? and then as the else if statements read, it will do certain things, (I will add these later) but no matter what I type in it always reads out, "Not a choice." what do I do?


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Answer always false.

    You never set the String "Fight" to anything so it should only go into if statement if Choice happens to be null.

    Actually, none of the other variables are set, so unless choice is null, it'll never be any.

    (On the other hand, if it is null for some odd reason, it will act like you've selected all the choices.

    Also, it's

    System.out.prinltn()

    not

    out.println()

    You don't have any class variables called out, and even if you did, none with a println method.

    Oh, you imported the out variable. You know that by default, all programs import the java.lang package, right?

    (However, if you wish to call that method, you must use System.out.println(), not out.println().)

    Use the .equals(Object) method to compare Objects like Strings.

    Try this:

    import java.util.*;
     
    public class mainMenu {
     
     
     
    	public static void main(String[] args){
    		Scanner keyboard = new Scanner(System.in);
     
                        String choice = "";
    		System.out.println("**************************************");
    		System.out.println("*  Choice  *          Info.          *");
    		System.out.println("**************************************");
    		System.out.println("*  Fight   *   Fight instantly!      *");
    		System.out.println("*  Carrer  *   Begin your carrer     *");
    		System.out.println("*  Options *   Coming Soon           *");
    		System.out.println("*  Info    *   Making of the game.   *");
    		System.out.println("*  Help    *   Learn how to play.    *");
    		System.out.println("*  Quit    *   Leave the game.       *");
    		System.out.println("**************************************");
    		System.out.println("Pick one:");
    		System.out.println("");
    		Choice = keyboard.nextLine();
    		System.out.println("Your choice was " + Choice);
    		if (Choice.equals("Fight") || (Choice.equals("fight")) {
     
    		} else if (Choice.equals("Carrer") || (Choice.equals("carrer"))){
     
    		} else if (Choice.equals("Options")|| (Choice.equals("options"))){
     
    		} else if (Choice.equals("Info") || (Choice.equals("info"))){
     
    		} else if (Choice.equals("Help") || (Choice.equals("help"))){
     
    		} else if (Choice.equals("Quit")|| (Choice.equals("quit"))){
    			System.exit(1);
    		} else {
    			System.out.println("Not a choice.");
    		}
     
    	}
     
    }
    Last edited by javapenguin; October 7th, 2011 at 01:42 AM. Reason: Forgot a ")"

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Answer always false.

    Strings look like this

    String str = "bob";

    It looked like you were trying to use the variable names as the String text. It doesn't work that way. (At least not in Java, C++, or C.)



    String fight = "Fight";
    String career = "Career";

    A String is basically like the text inside your println() methods (actually, that's what's passed to it, though if you use primitives, it will automatically call the toString() of their wrapper classes or maybe it just knows how to convert them to a String (Can't recall which one of those two it was.) If you use non-String objects, it will call the toString() of that class, which often looks like gibberish(or more accurately, like a bunch of addresses and words.)

    As for equals, it's part of the Comparable interface, which String implements. It's used for comparing Objects that implement Comparable.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Answer always false.

    Just a few tips for you..

    Rather than have multiple String variables written out like:

    	private static String Choice;
    	private static String Fight;
    	private static String Options;
    	private static String Info;
    	private static String Help;
    	private static String Quit;
    	private static String fight;
    	private static String options;
    	private static String info ;
    	private static String help;
    	private static String quit;
    	private static String Carrer;
    	private static String carrer;

    You could do it like this:

    private static String Choice, Flight, Options, Info, Help, Quit, flight, options, info, help, quit, Carrer, carrer;

    Also, instead of checking against 2 different String variables for the possability of a capital letter, you should do:

    		if (Choice.toLowerCase().equals("flight")) {			
     
    		} else if (Choice.toLowerCase().equals("carrer")){
     
    		} else if (Choice.toLowerCase().equals("options")){
    ...

    This way eliminates the need for those String variables. It also allows the user to enter FLIGHT or FlIgHt or any combination and it would still recognise the input.

    This will fix your original issue too..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    tyb97 (October 7th, 2011)

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Answer always false.

    Quote Originally Posted by javapenguin
    Also, it's

    System.out.prinltn()

    not

    out.println()
    The original poster is using a static imports, so this is completely valid syntax.

  7. #6
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Answer always false.

    Thanks all, it works great now, I have always had issues with this, so I now can continue on Thanks.

Similar Threads

  1. What's ur answer?
    By Tanmaysinha in forum The Cafe
    Replies: 6
    Last Post: October 12th, 2011, 06:26 AM
  2. Pls answer( urgent)
    By sar in forum Java Theory & Questions
    Replies: 1
    Last Post: September 20th, 2011, 11:28 AM
  3. need answer for these
    By satishbs in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2011, 01:46 AM
  4. would this expression be true or false??
    By robertsbd in forum Java Theory & Questions
    Replies: 1
    Last Post: October 24th, 2010, 10:00 PM
  5. why is this statement evaluating false??
    By humdinger in forum Object Oriented Programming
    Replies: 2
    Last Post: November 3rd, 2009, 04:28 PM