Results 1 to 20 of 20
Like Tree1Likes
  • 1 Post By Games2Design

Thread: Need help with "Found: Int, Req: Boolean" compile error!

  1. #1
    homburgerc is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Need help with "Found: Int, Req: Boolean" compile error!

    I'm making a quick guess number game, and the only problem I'm running into is replaying/exiting. I'm doing it through a for loop and input at the end:
    Java Code:
        Scanner console = new Scanner(System.in);
        int again = 1;
        
        System.out.println("Initializing...");
        
        for (int j=0; again = 1; j++)
        {
          System.out.println("Ready!");
          game();
          System.out.println("Play again? (1=yes, 0=no");
          again = console.nextInt();
          
        }
    The method "game();" is where the actual guess/check takes place and works perfectly. Once you guess, though, the option to quit the program by breaking the for loop didn't work (I previously had again as a string that needed to equal "yes", but that didn't work).

    When I compile this, I get the error:
    GuessNumber.java:15: incompatible types
    found : int
    required: boolean

    I'm confused, since "again" is an integer variable. What's going on here?

  2. #2
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    Could you please post whole program? So i can see the context :D

  3. #3
    homburgerc is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    Of course! I'm not familiar with the workings of the random number generator, but I don't think that would factor in to this. Anyways.

    Edit: I was getting this error also in the "game" method when I had an if statement to check if the answer was correct.

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class GuessNumber
    {
      public static void main(String[] args)
      {
        Scanner console = new Scanner(System.in);
        int again = 1;
        
        System.out.println("Initializing...");
        
        for (int j=0; again = 1; j++)
        {
          System.out.println("Ready!");
          game();
          System.out.println("Play again? (1=yes, 0=no");
          again = console.nextInt();
          
        }
        
      }
      
      public static void game()
      {
        Scanner console = new Scanner(System.in);
        System.out.println("Please enter a number from 1-100.");
        
        Random randomGenerator = new Random();
        int answer = randomGenerator.nextInt(100)+1;
        
        int guess = -1;
        int i=0;
        
        for (i=1; guess != answer; i++)
        {
          guess = console.nextInt();
          if (guess > answer)
          {
            System.out.println("PSYCH! That's the wrong number!");
            System.out.println("It's lower than "+guess+".");
          }
          else if (guess < answer)
          {
            System.out.println("PSYCH! That's the wrong number!");
            System.out.println("It's higher than "+guess+".");
          }
         }
        System.out.println("Congratulations! You're right, the number was "+answer+".");
        System.out.println("The average number of guesses is 7.");
        System.out.println("It took you "+i+" guesses.");
      }
    }

  4. #4
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    I am not familiar with Random number generator (have never gotten into it ...YET). I know i will learn it some day :) Because it seems useful for me.

    But i hope i am of help by suggesting you use a while loop to keep the game going. Write this code:

    while (again!=1){
    System.out.println("Ready!");
    game();
    System.out.println("Play again? (1=yes, 0=no");
    again = console.nextInt();
    }

    This will make the while loop go on until the player enters a int that's not 1. Example if player enters 0,2,3,4,5,6 or any other number that's not 1 the program will terminate. But if the player wants to guess again he or she can do that by entering the number 1 as you instructed.

    Oh and can you write the full error message? because it seems you've taken a part of it only :P

    Anyhow while loop should be better than the for loop for continueing the game until the player says "no i want to stop" :p


    Let me know how it goes :)

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    What's going on here?
    Java Code:
    for (int j=0; again = 1; j++)
    The middle expression (again = 1) in the for loop needs to return a boolean value. again = 1 is an assignment statement which returns the value of again (an int)
    You need to change the middle expression so that it returns a boolean. Replacing the assignment operator (=) with the comparison operator (==) would do that.


    Would a while() loop show better what you are doing?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    homburgerc is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    Cool, I'll try out the while loop in a moment here.

    The rest of the error message is just the directory I'm working in, but sure:

    1 error found:
    File: /Users/homburgerc/Documents/CPSC_112_programs/GuessNumber.java [line: 13]
    Error: /Users/homburgerc/Documents/CPSC_112_programs/GuessNumber.java:13: incompatible types
    found : int
    required: boolean

  7. #7
    homburgerc is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    So I put in the "while" loop, and it doesn't start up so. That's fun. I'm probably gonna keep the for loop. Thanks for the help though!

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    You will have to post the code if you want help fixing it.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    Yes, post the code. I am currently working on downloading JDK for my home pc. So i will try to join you in getting your program to work. Though i must read into Random generator method first.

  10. #10
    homburgerc is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    It should be the same code as before (with the "==" instead of just "="), but here it is again:


    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class GuessNumber
    {
      public static void main(String[] args)
      {
        Scanner console = new Scanner(System.in);
        int again = 1;
        
        System.out.println("Initializing...");
        
        for (int j=0; again == 1; j++)
        {
          System.out.println("Ready!");
          game();
          System.out.println("Play again? (1=yes, 0=no)");
          again = console.nextInt();
          
        }
        
      }
      
      public static void game()
      {
        Scanner console = new Scanner(System.in);
        System.out.println("Please enter a number from 1-100.");
        
        Random randomGenerator = new Random();
        int answer = randomGenerator.nextInt(100)+1;
        
        int guess = -1;
        int i=0;
        
        for (i=1; guess != answer; i++)
        {
          System.out.println(" ");
          guess = console.nextInt();
          
          if (guess > answer)
          {
            System.out.println("PSYCH! That's the wrong number!");
            System.out.println("It's lower than "+guess+".");
          }
          else if (guess < answer)
          {
            System.out.println("PSYCH! That's the wrong number!");
            System.out.println("It's higher than "+guess+".");
          }
         }
        System.out.println("Congratulations! You're right, the number was "+answer+".");
        System.out.println("The average number of guesses is 7.");
        System.out.println("It took you "+(i-1)+" guesses.");
      }
    }

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    Where is the while() statement that was the problem?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    homburgerc is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    It was suggested above, but this for() loop performs just as well, it seems.

  13. #13
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    Quote Originally Posted by Norm View Post
    Where is the while() statement that was the problem?
    I suggested to him that that instead of using a for loop that he should used a while loop covering the setences including the call of the method "game()".

    like this:
    while(again!=1){
    setences in here
    }

    But i realize now i misguided him. Because the expression must be while(again==1) otherwise it will do the opposite.
    It will run the while loop when it's not 1 (which is what he wants when again is 1), and not run when the player of the game wants to play again and presses 1 to indicate that.
    Last edited by Games2Design; 09-28-2012 at 09:50 PM.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    Yes you are using the for loop just like a while() loop.
    However it will be confusing for anyone reading your code wondering what the code is doing.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    106
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    Note: your program asks for a guess from 1-100 though does not stop me from entering a number larger. X Did run into run time errors. Try entering a number outside your type range. Program crashes.

  16. #16
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    @
    homburgerc

    I made a mistake suggesting the expression for while(again!=1)

    I believe it should work if you use while(again==1).

    But i understand if your not familiar with while loops.

    I am trying to get java working dammit!!


    I downloaded Jdk1.7.0.07 , and made "Path" for javacompilator.
    But now when trying to compile a program (with .java extension) - it gives me error about not finding the file:/

  17. #17
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    I got java set up on my home pc. Here is how i suggested you used while loop for your programme. Note, this is a modified version of your program code, and only purpose is to show you the function of While loop. If you have any question - ASK AWAY.

    import java.util.Scanner;

    public class GuessNumber
    {
    public static void main(String[] args)
    {
    Scanner console = new Scanner(System.in);
    int again = 1;

    while (again==1) // This expression says that as long as the int variable again is 1, while loop should keep repeating. Since again is 1 in beginning, the while loop will be run once, and if the player wants to the Guess game again he or she will need to type 1 again.
    {
    game();
    System.out.println("Play again? (1=yes, 0=no");
    again = console.nextInt();
    System.out.println();

    }
    System.out.println("Program terminated"); //If the player types any number except 1, the while loop will end, and this setence will be printed out indincating the program has ended.

    }

    public static void game()
    {
    System.out.println("Method game() has been run."); // Just a print setence i made to show you that the method "game()" will go on until you/or player decides "I want to stop playing" and enters the number 0, when asked if they want to play again.
    }
    }

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    @Games2Design: Please go through these: Guide For New Members and Smilies - Java Programming Forum
    do=bbcode#code

    Also, don't spoonfeed. That denies the questioner the satisfaction of having learned something, and solved their own problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  19. #19
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    Quote Originally Posted by DarrylBurke View Post
    @Games2Design: Please go through these: Guide For New Members and Smilies - Java Programming Forum
    do=bbcode#code

    Also, don't spoonfeed. That denies the questioner the satisfaction of having learned something, and solved their own problem.

    db
    By spoonfeed your refering to me giving a finished code? Where the contrast of that would me saying "I suggest you use While loop"?
    If so i will do that.

    Thanks :)
    DarrylBurke likes this.

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Need help with "Found: Int, Req: Boolean" compile error!

    Yes, that's what I meant. Thank you for understanding this.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 02-02-2012, 07:24 AM
  2. -jGRASP wedge2 error: command "javac" not found.
    By ncneeds in forum New To Java
    Replies: 0
    Last Post: 09-28-2011, 09:21 PM
  3. Replies: 3
    Last Post: 11-06-2010, 12:33 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Strange error message "Source not found"
    By ppayal in forum Eclipse
    Replies: 0
    Last Post: 11-25-2007, 06:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •