Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-18-2007, 09:10 PM
Member
 
Join Date: Jul 2007
Posts: 35
silvia is on a distinguished road
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:
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:

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:

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-19-2007, 08:33 PM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
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

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

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error: javax.servlet.ServletException: Column not found barney JavaServer Pages (JSP) and JSTL 1 08-07-2007 08:20 AM
Error: no class definition found toby New To Java 4 07-27-2007 08:01 PM
problem with scanner class:incompatible types fred New To Java 1 07-20-2007 08:02 AM
Error: invalid method declaration; return type required silvia AWT / Swing 1 07-19-2007 02:51 PM
JavaMail:Authentication required error bbq Advanced Java 1 07-05-2007 05:16 AM


All times are GMT +3. The time now is 12:27 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org