Results 1 to 4 of 4
  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default problems with parsing a string into int,

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

  2. #2
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    solved, tnx ..!! sorry...

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    In

    Java Code:
        System.out.format("%2d %10s %10s %10s", firstSlot, secondSlot, thirdSlot);
    You pass three arguments but have 4 format specifiers.

  4. #4
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    It would be helpful if you can mark this thread as [SOLVED].
    To do so, do the following:
    1. Find "Post Reply" button on the top of this thread.
    2. Look exactly right of it until you see "Thread Tools." (between LinkBack and Thread Tools)
    3. Click on "Thread Tools" and on the drop down menu, click on "Mark as solved."

Similar Threads

  1. Parsing string and simple calculation
    By sapina007 in forum Advanced Java
    Replies: 4
    Last Post: 08-21-2009, 12:07 PM
  2. Parsing XML
    By virvalid in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2009, 12:40 PM
  3. Multi Delimeter String Parsing
    By yuriyl in forum Advanced Java
    Replies: 5
    Last Post: 07-13-2009, 03:34 PM
  4. parsing numbers in a string
    By rsoler in forum Advanced Java
    Replies: 4
    Last Post: 03-31-2009, 06:05 AM
  5. Problems implementing Java string methods
    By everlast88az in forum New To Java
    Replies: 8
    Last Post: 11-06-2008, 04:17 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
  •