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