#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    12
    Rep Power
    0

    Boolean variable issue


    Hey everyone,
    I'm getting the following errors when trying to compile my code:

    Code:
    4 errors found:
    File: /media/LaCie/MathTest.java  [line: 14]
    Error: Type mismatch: cannot convert from int to boolean
    File: /media/LaCie/MathTest.java  [line: 15]
    Error: Type mismatch: cannot convert from int to boolean
    File: /media/LaCie/MathTest.java  [line: 41]
    Error: Type mismatch: cannot convert from int to boolean
    File: /media/LaCie/MathTest.java  [line: 52]
    Error: Type mismatch: cannot convert from int to boolean
    They refer to the boolean variables I've declared. At line 14 I'm calling a method (test method) by passing it a boolean value (bool). The method returns an integer value, which is what I'm originally storing it in (a int declared variable). I'm really stumped here. Here's the code:


    Code:
    import java.util.*;
    class MathTest {
      
      public static void main (String[] args) {
        System.out.println("Welcome to the Math Tester!");
        
        int counter = 0;
        int count = 0;
        boolean bool;
        int result;
        
        while (counter < 10) {
          bool = 
          result = test(bool);
          if (result = 1) {
            count++;
          }
          counter++;
        }
        
        if (count >= 6) {
          System.out.println("Congratulations!");
        } else {
          System.out.println("Please ask your teacher for help.");
        }
        
      }
      
      public static int test(boolean bool) {
        Scanner in = new Scanner(System.in);
        Random gen = new Random();
        int correct = 0;
        int answer;
        int rand1 = gen.nextInt(9) + 1;
        int rand2 = gen.nextInt(9) + 1;
        
        if (bool) {
          System.out.println("What is " + rand1 + " times " + rand2 + "?");
          answer = in.nextInt();
          
          if (answer = rand1*rand2) {
            correct = 1;
          } else {
            System.out.println("Wrong. The correct answer was " + rand1*rand2);
          }
        } 
        
        else {
          System.out.println("What is " + rand1 + " divided by " + rand2 + "?");
          answer = in.nextInt();
          
          if (answer = rand1/rand2) {
            correct = 1;
          } else {
              System.out.println("Wrong. The correct answer was " + rand1/rand2);
          }
        }
        
        return correct;
      } 
    }
  2. #2
  3. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    619
    You have the line
    Code:
    bool =
    followed by
    Code:
    result = test(bool);
    What do those two lines tell the compiler to do?
    All science is either physics or stamp collecting. - Ernest Rutherford

    How To Ask Questions The Smart Way by Eric S. Raymond
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    12
    Rep Power
    0
    Oh crap. I never realized I didn't fill in that line.

    It should read:
    Code:
    bool = gen.nextBoolean();
    Where gen is a generator I forgot to declare at the beginning.
    I added that and I'm still getting three errors, it only seemed to fix the first one (line 14). I feel it might be a problem with how my method is being called?

    Thanks
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    London, ON
    Posts
    322
    Rep Power
    214
    java Code:
    if(answer = rand1*rand2)
    if (result = 1)
    if(answer = rand1/rand2)


    ==, baby ;D
    <Tetrad> the program I just wrote 1) compiled the first time without any errors and 2) worked like it was supposed to
    <Tetrad> I don't know whether to be proud or scared to death
    Originally Posted by DaWei_M
    That covers a multitude of your sins, right there.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    12
    Rep Power
    0
    Wow. I'm an idiot. Totally forgot about that.
    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo