简体   繁体   中英

If statement evaluating incorrectly a string

When compiling this program, the if statement inside each case always evaluates to false (wrong) when given the correct answer, here's the code:

import java.util.Scanner;

class GameStarts {
  public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   String ret;
   byte qnum;
   String ans;

   String correct = "Awesomely correct!";
   String wrong = "Darn it! Almost got it!";

   System.out.println("Do you think you know your stuff?");
   ret = sc.nextLine();

   if (ret.equals("yes") || ret.equals("Yes"))
   {
    System.out.println("Well, then let's test what you know! Choose a number from 1 to 5!");
    qnum = sc.nextByte();
      switch (qnum)
      {
     case 1:
    System.out.println("In what year did the French Revolution start?");
    ans = sc.nextLine();
    sc.nextLine();
      if (ans.equals("1789") || ans.equalsIgnoreCase("seventeen eighty nine"))
      {System.out.println(correct);}
      else
      {System.out.println(wrong);}

     break;

    case 2:
    System.out.println("How many protons does a sodium atom have?");
    ans = sc.nextLine();
    sc.nextLine();
      if (ans.equals("11") || ans.equalsIgnoreCase("eleven"))
      {System.out.println(correct);}
      else
      {System.out.println(wrong);}

     break;

    case 3:
    System.out.println("What is 2^6*0.5-12?");
    ans = sc.nextLine();
    sc.nextLine();
      if (ans.equals("20") || ans.equalsIgnoreCase("twenty"))
      {System.out.println(correct);}
      else
      {System.out.println(wrong);}

     break;

    case 4:
    System.out.println("Which is the lowest numbered element in the periodic table?");
    ans = sc.nextLine();
    sc.nextLine();
      if (ans.equalsIgnoreCase("hydrogen"))
      {System.out.println(correct);}
      else
      {System.out.println(wrong);}

     break;

    case 5:
    System.out.println("Which is the unit that measures Coulombs per second?");
    ans = sc.nextLine();
    sc.nextLine();
      if (ans.equalsIgnoreCase("ampere"))
      {System.out.println(correct);}
      else
      {System.out.println(wrong);}

     break;
    default:
    System.out.println("Stick to the rules! 1-5!");
     }

  }
   else
   {System.out.println("Not liking that attitude, I want to hear a big yes!");}

 }
}

I'm not sure if it's skipping the ans definition or if I'm missing something. Also, I'd appreciate any other suggestions :)

The readline may have a new line character at the end. Try

ans.trim().equalsIgnoreCase...

The trim will get rid of any spurious spaces or new line characters you may have

Try replacing

 
 
 
 
  
  
  ret = sc.nextLine();
 
 
  

With

Scanner sc = new Scanner(System.in);
       String ret;
       String qnum;
       String ans;

       String correct = "Awesomely correct!";
       String wrong = "Darn it! Almost got it!";

       System.out.println("Do you think you know your stuff?");
       ret = sc.nextLine();

       if (ret.equals("yes") || ret.equals("Yes"))
       {
        System.out.println("Well, then let's test what you know! Choose a number from 1 to 5!");
        qnum = sc.nextLine();
          switch (qnum)
          {
         case "1":
        System.out.println("In what year did the French Revolution start?");
        ans = sc.nextLine();
          if (ans.equals("1789") || ans.equalsIgnoreCase("seventeen eighty nine"))
          {System.out.println(correct);}
          else
          {System.out.println(wrong);}

         break;

        case "2":
        System.out.println("How many protons does a sodium atom have?");
        ans = sc.nextLine();
        sc.nextLine();
          if (ans.equals("11") || ans.equalsIgnoreCase("eleven"))
          {System.out.println(correct);}
          else
          {System.out.println(wrong);}

         break;

        case "3":
        System.out.println("What is 2^6*0.5-12?");
        ans = sc.nextLine();
        sc.nextLine();
          if (ans.equals("20") || ans.equalsIgnoreCase("twenty"))
          {System.out.println(correct);}
          else
          {System.out.println(wrong);}

         break;

        case "4":
        System.out.println("Which is the lowest numbered element in the periodic table?");
        ans = sc.nextLine();
        sc.nextLine();
          if (ans.equalsIgnoreCase("hydrogen"))
          {System.out.println(correct);}
          else
          {System.out.println(wrong);}

         break;

        case "5":
        System.out.println("Which is the unit that measures Coulombs per second?");
        ans = sc.nextLine();
        sc.nextLine();
          if (ans.equalsIgnoreCase("ampere"))
          {System.out.println(correct);}
          else
          {System.out.println(wrong);}

         break;
        default:
        System.out.println("Stick to the rules! 1-5!");
         }

      }
       else
       {System.out.println("Not liking that attitude, I want to hear a big yes!");}

What is does it remove any newlines (Or tabs) and removes all trailing and leading whitespace (These things often creep in)

Edit:

Just put lines like that after all lines using sc.nextLine(), and if I'm right, it should sort things out

Edit: That wasn't the problem. When you did sc.readByte, it read 2 bytes - the character and a newline. When you next did sc.nextLine, it would get the leftover newline character. The solution:

 Scanner sc = new Scanner(System.in); String ret; String qnum; String ans; String correct = "Awesomely correct!"; String wrong = "Darn it! Almost got it!"; System.out.println("Do you think you know your stuff?"); ret = sc.nextLine(); if (ret.equals("yes") || ret.equals("Yes")) { System.out.println("Well, then let's test what you know! Choose a number from 1 to 5!"); qnum = sc.nextLine(); switch (qnum) { case "1": System.out.println("In what year did the French Revolution start?"); ans = sc.nextLine(); if (ans.equals("1789") || ans.equalsIgnoreCase("seventeen eighty nine")) {System.out.println(correct);} else {System.out.println(wrong);} break; case "2": System.out.println("How many protons does a sodium atom have?"); ans = sc.nextLine(); sc.nextLine(); if (ans.equals("11") || ans.equalsIgnoreCase("eleven")) {System.out.println(correct);} else {System.out.println(wrong);} break; case "3": System.out.println("What is 2^6*0.5-12?"); ans = sc.nextLine(); sc.nextLine(); if (ans.equals("20") || ans.equalsIgnoreCase("twenty")) {System.out.println(correct);} else {System.out.println(wrong);} break; case "4": System.out.println("Which is the lowest numbered element in the periodic table?"); ans = sc.nextLine(); sc.nextLine(); if (ans.equalsIgnoreCase("hydrogen")) {System.out.println(correct);} else {System.out.println(wrong);} break; case "5": System.out.println("Which is the unit that measures Coulombs per second?"); ans = sc.nextLine(); sc.nextLine(); if (ans.equalsIgnoreCase("ampere")) {System.out.println(correct);} else {System.out.println(wrong);} break; default: System.out.println("Stick to the rules! 1-5!"); } } else {System.out.println("Not liking that attitude, I want to hear a big yes!");} 

What I changed in your code was I changed the type of qnum to a String, changed the case statement to work with strings, and changed readByte to readLine. Also, I removed the second unneeded new line.

You also are using sc.nextByte to try and read 1 through 5. That won't work.

You will have to read it as a String, and then trim it as above.

In earlier versions of Java, you would then convert it to an int using Integer.valueOf(), but starting with java 7, you can switch on a string:

switch(str)
{
    case "1":
    ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM