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 10 of 10

Thread: Yes or No answers,

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

    Default Yes or No answers,

    I am trying to add in a program where I would type "Yes or No" for the answer, but I can't get it to work, I know how to get number answers to work, with something like
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Pick a number 1 - 10:");
    		int WinnerPoints;
    		WinnerPoints = 0;
    		System.out.println("Your current amount of Points is " + WinnerPoints);
    		int numberPicked = keyboard.nextInt();
    		switch (numberPicked)
    		{
    		case 1:
    		case 2:
    		case 3:
    		case 4:
    		case 5:
    		case 6:
    		case 8:
    		case 9:
    		case 10:
    			System.out.println("Sorry, you lost :(");
    			System.out.println("Good Bye!");
    			System.exit(0);
    			break;
    		case 7:
    			System.out.println("YAY! You Won!");
    			WinnerPoints = 0+5;
    			System.out.println("Welcome to level 2!");
    			break;
    But when I try to make it to where I use Word answers instead and it won't work, This is the best I've gotten so far,
    	Scanner keyboard = new Scanner(System.in);
    		System.out.println("Hello and welcome to my JAVA program, Math Test!");
    		System.out.println("This Test will be over: Addition");
    		System.out.println("Are You Ready to go?");
    		System.out.println("Yes or No?");
    		String yes_or_no = keyboard.nextLine();
    		switch (yes_or_no)
    		{
    		case Yes:
    		}
     
     
    	}
     
    }
    But my IDE says that
    	Cannot switch on a value of type String. Only convertible int values or enum constants are permitted
    	Yes cannot be resolved to a variable
    What do I do to get words activate something?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Yes or No answers,

    What do I do to get words activate something?
    You'll have to get java 1.7 for that style of switch statement. Until then only integers work.

    A work around is a chain of if/else if/else if /else statements to compare the Strings.
    Be sure to use the equals method.

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

    Default Re: Yes or No answers,

    I have the newest Java...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Yes or No answers,

    What error message do you get from the 1.7 javac compiler?

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Yes or No answers,

    What is 'Yes' in your source? If you want to switch on 'yes_or_no' against String literals, you need to write
    case "Yes":
    At least, that's my guess - I'm not on Java 7 either.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Yes or No answers,

    Cannot switch on a value of type String
    The IDEs message looks like it doesn't know about the new switch in java 1.7
    @Sean4u - your syntax looks right.

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

    Default Re: Yes or No answers,

    Quote Originally Posted by Sean4u View Post
    What is 'Yes' in your source? If you want to switch on 'yes_or_no' against String literals, you need to write
    case "Yes":
    At least, that's my guess - I'm not on Java 7 either.
    Thanks that fixed that error, but I have this one still:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Cannot switch on a value of type String. Only convertible int values or enum constants are permitted
     
    	at Addition.main(Addition.java:15)
    Line 15 is:
    switch (yes_or_no)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Yes or No answers,

    If you use Java 1.7 to compile(javac) it and execute(java) it, it should work.

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

    Default Re: Yes or No answers,

    Quote Originally Posted by Norm View Post
    If you use Java 1.7 to compile(javac) it and execute(java) it, it should work.
    I am using the IDE Eclipse, which compiles it for you, and runs it, It's always worked before.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Yes or No answers,

    Sorry, I know nothing about your IDE.
    This code works for me with 1.7:
          String key = "ABC";
          switch (key) {
             case "ABC":
                 System.out.println("works");    // works is printed
                 break;
             default:
                 System.out.println("not working");
          } // end switch

Similar Threads

  1. eclipse interview questions and answers
    By saggammahesh in forum Java IDEs
    Replies: 1
    Last Post: April 17th, 2012, 02:07 AM
  2. Replies: 10
    Last Post: April 5th, 2011, 09:09 AM
  3. Is My answers correct??
    By Java.Coder() in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 28th, 2010, 05:22 AM