Results 1 to 7 of 7
  1. #1
    silvia is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default Error: incompatible types, found: int required: boolean

    Hi, I keep getting the error: incompatible types, found: int required: boolean.

    I don't know why! I'm trying to write an if statement which calls an array item. Basically, i want the array items: dollars [200] and [100] to print on screen as "$2" or "$1 coins" and want dollars [50] etc to print on screen as "50 cents coins". I thought it would be easiest to do it as an if statement but maybe i'm wrong!

    The output should look something like this:
    Java Code:
    $2 coins 2
    $1 coins 0
    50 cents coins 1
    20 cents coins 1
    10 cents coins 0
    5 cents coins 1
    1 cent coin 2
    Here's the code before i included the if statement:

    Java Code:
    public class ***_Sm2 {
    public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
    int[] dollars = {200, 100, 50, 20 , 10, 5, 1};
    int input;
    int result, remain;
    
    boolean flag = true;
    
    while(flag) {
    System.out.print("Input Integer: ");
    if ((input = Integer.parseInt(in.readLine())) != 0) {
    for (int ctr = 0; ctr < dollars.length; ctr++) {
    if (input >= dollars[ctr]) {
    result = input / dollars[ctr];
    
    System.out.println(dollars[ctr] + " cents coins" + " " +result );
    
    input = input % dollars[ctr];
    }
    }
    }
    else {
    flag = false;
    }
    }
    }
    }
    Here's the code with if statement:

    Java Code:
    import java.io.*;
    public class ***_Sm2 {
    public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int[] dollars = {200, 100, 50, 20 , 10, 5, 1};
    int input;
    int result, remain;
    boolean flag = true;
    while(flag) {
    System.out.print("Input Integer: ");
    if ((input = Integer.parseInt(in.readLine())) != 0) {
    for (int ctr = 0; ctr < dollars.length; ctr++) {
    if (input >= dollars[ctr]) {
    result = input / dollars[ctr];
    if (dollars[200]){
    System.out.println(result + dollars[ctr] + " coins");
    }else{
    System.out.println(result + dollars[ctr] + " cents coins");
    }
    input = input % dollars[ctr];
    }
    }
    }
    else {
    flag = false;
    }
    }
    }
    }
    Thanks

  2. #2
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Java Code:
    if (dollars[200]){
    The input u giv for the if statement was supposed to be boolean, u can't use an int type and hope that it'll turn out into a boolean like in C++, in Java it's either gotta be true or false, and since u didn't enter a logical expression that can return a boolean type or use a boolean type instead in da input, da error occured

    Bout how to fix it, try this

    Java Code:
    	result = input / dollars[ctr];
    	if (input>=dollars[1])
    	{
    		System.out.println(dollars[ctr] + " coins " + result);
    	}
    	else
    	{
    		System.out.println(dollars[ctr] + " cents coins " + result);
    	}
    	input = input % dollars[ctr];
    that should at least give u an output like this

    Java Code:
    Input Integer: 572
    200 coins 2
    100 coins 1
    50 cents coins 1
    20 cents coins 1
    1 cents coins 2
    bout da $ mark to replace 200 into $2, u could use some of da printing editor class, or maybe use some REGEX expression, ill look into it and post it later

    good luck :D

  3. #3
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    I'm having the exact same problem, but I'm not sure exactly what you did to solve the problem. It happens only when I use the if statement. Here's the part that has the problem:

    import java.util.Scanner;
    public class project5 {
    public static void main (String[] args) {
    Scanner scan = new Scanner (System.in);
    System.out.println ("Enter x: );
    int x = scan.nextInt();
    System.out.println ("Enter y:");
    int y = scan.nextInt();

    if ((x-y)=0) {
    System.out.println("x = " + x + ", y = " + y);
    x++;
    }

    if ((x-y)=2) {
    if (x/(x-y)=2) {
    y+=x;
    System.out.println("y = " +y);
    }
    else{
    System.out.println("x = " +x);
    }
    }
    Because, of course x-y can't be boolean, it has to be a number, right? How do I fix this?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    use two == to compare things (like in an if statement), one = to assign a value to a variable. Also, please avoid hijacking old threads. This is a new problem, your problem, and should have been placed in its own thread here.

  5. #5
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    Sorry, no hijacking intended, I was almost out of time in the school's computer lab, after installing Netbeans and registering for this site. I just did a quick search first, not the rules. I shall make my own thread next time.

    Thank you so much, that worked, and now the code works! :D

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    knock this off!!!!! You are on my do-not-help list

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    knock this off!!!!! You are on my do-not-help list
    Hi pal, I've already remove some post of SapphireSpark. And also I'll contact regarding this, and monitoring him continuously. Thanks for your corporation.

Similar Threads

  1. Error: no class definition found
    By toby in forum New To Java
    Replies: 6
    Last Post: 08-28-2011, 10:32 PM
  2. Replies: 3
    Last Post: 06-05-2010, 08:05 PM
  3. Error: javax.servlet.ServletException: Column not found
    By barney in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-07-2007, 07:20 AM
  4. problem with scanner class:incompatible types
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 07:02 AM
  5. JavaMail:Authentication required error
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 04:16 AM

Posting Permissions

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