problems with parsing a string into int,
Code:
public static void main(String[] args) throws IOException {
String firstSlot,
secondSlot,
thirdSlot;
int firstPiece,
secondPiece,
thirdPiece,
payOff;
System.out.print("Enter The Coins: ");
int coins = Integer.parseInt(br.readLine());
System.out.print("\n");
while (coins != 0) {
System.out.print("Bet(Max: 1 - 4 Coins): ");
int bet = Integer.parseInt(br.readLine());
firstPiece = (int) Math.floor(Math.random() * MAX_PIECES) + 1;
secondPiece = (int) Math.floor(Math.random() * MAX_PIECES) + 1;
thirdPiece = (int) Math.floor(Math.random() * MAX_PIECES) + 1;
switch (firstPiece) {
case 1: firstSlot = "BELL"; break;
case 2: firstSlot = "CHERRY"; break;
case 3: firstSlot = "GRAPE"; break;
case 4: firstSlot = "------"; break;
// just in case, there is still an option
default: firstSlot = "------"; break;
}
switch (secondPiece) {
case 1: secondSlot = "BELL"; break;
case 2: secondSlot = "CHERRY"; break;
case 3: secondSlot = "GRAPE"; break;
case 4: secondSlot = "------"; break;
// just in case, there is still an option
default: secondSlot = "------"; break;
}
switch (thirdPiece) {
case 1: thirdSlot = "BELL"; break;
case 2: thirdSlot = "CHERRY";; break;
case 3: thirdSlot = "GRAPE"; break;
case 4: thirdSlot = "------"; break;
// just in case, there is still an option
default: thirdSlot = "------"; break;
}
if (firstSlot.equals("BELL") && secondSlot.equals("BELL") && thirdSlot.equals("BELL")) {
System.out.println("Lucky One!");
}
System.out.print("\n");
System.out.format("%2d %10s %10s %10s", firstSlot, secondSlot, thirdSlot);
System.out.print("\n\n");
coins = coins - bet;
} // end of while loop
}
}
output:
Code:
Enter The Coins: 50
Bet(Max: 1 - 4 Coins): Bet(Max: 1 - 4 Coins):
2
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3999)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2709)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2661)
at java.util.Formatter.format(Formatter.java:2433)
at java.io.PrintStream.format(PrintStream.java:920)
at xxTestxx.Ch6Exercise31.main(Ch6Exercise31.java:83)
help! :confused: