0

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 :)

DaMadApe
  • 5
  • 1
  • Regarding `sc.nextByte();` and `ans = sc.nextLine();`: possible duplicate of [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx). Also you are unnecessary `sc.nextLine();` right after `ans = sc.nextLine();` – Pshemo Apr 17 '14 at 20:55

3 Answers3

1

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

RNJ
  • 15,272
  • 18
  • 86
  • 131
1

Try replacing

ret = sc.nextLine();

With

ret = sc.nextLine();
ret = ret.replace("\n","").replace("\t","").trim();

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.

Flyingfirepig
  • 223
  • 1
  • 8
  • Still getting same output. The problem is with string ans inside the switch statement, but I used this metod replacing each ret with ans. – DaMadApe Apr 17 '14 at 22:00
  • Yeah, I guess now I'll treat those issues like strings from now on. I had already found an answer from a fine gentleman that fixed a previous problem in the same code. He used qnum as an int and wrote: qnum = Integer.parseInt(sc.nextLine()); For the sake of simplicity, I'll go with strings next time, until I get the hang of it :) – DaMadApe Apr 19 '14 at 18:34
0

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":
    ....
Jamie
  • 1,888
  • 1
  • 18
  • 21
  • Actually, it works fine in that part, as you can chose a number and it selects the correct question. The problem shows up later – DaMadApe Apr 17 '14 at 22:02