Problem Exception in thread "main" java.util.InputMismatchException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jri
    New Member
    • Dec 2007
    • 3

    Problem Exception in thread "main" java.util.InputMismatchException

    Hello,

    I got an assignement for school where I have to make a game with matches.

    This is what I got:

    [code=java]
    import javax.swing.JOp tionPane;
    import java.util.Scann er;
    public class Lucifer

    {

    int aantalLucifers, beurtSpelerEen, beurtSpelerTwee ;
    int ingaveSpel;
    Scanner input = new Scanner (System.in);
    String next ="nee";
    String eindeSpel ="nee";


    public static void main(String[] args)

    {
    Lucifer luci = new Lucifer();
    luci.start();
    }


    public void start()

    {
    berichtWelkom() ;
    bepaalAantalLuc ifers();
    do
    {
    ingaveSpelerEen ();
    bepaalVerliezer ();
    ingaveSpelerTwe e();
    bepaalVerliezer ();
    }
    while((eindeSpe l.equals("nee") ) && (aantalLucifers > 0));
    }


    public void berichtWelkom()

    {
    JOptionPane.sho wMessageDialog( null,"Let the game begin\n\nPress OK to play");
    }


    public void bepaalAantalLuc ifers()
    {
    aantalLucifers = (int)((Math.ran dom()*20)+21);
    System.out.prin tln("We gaan spelen met "+aantalLucifer s+" lucifers.\n");
    }


    public void ingaveSpelerEen ()

    {
    do
    {
    beurtSpelerTwee = 0;
    beurtSpelerEen+ +;
    System.out.prin t("Speler1:") ;
    ingaveSpel = input.nextInt() ;

    if(ingaveSpel == 1)
    {
    aantalLucifers -= 1;
    System.out.prin tln("Er blijven nog: "+aantalLucifer s+ " lucifers over.\n");
    next = "ja";
    }
    else if(ingaveSpel == 2)
    {
    aantalLucifers -= 2;
    System.out.prin tln("Er blijven nog: "+aantalLucifer s+ " lucifers over.\n");
    next = "ja";
    }
    else if(ingaveSpel == 3)
    {
    aantalLucifers -= 3;
    System.out.prin tln("Er blijven nog: "+aantalLucifer s+ " lucifers over.\n");
    next = "ja";
    }
    else
    {
    JOptionPane.sho wMessageDialog( null,"Foutieve ingave...\nGeli eve een getal tussen 1 en 3 in te geven.\n");
    next = "nee";
    }
    } while(next != "ja");
    }


    public void ingaveSpelerTwe e()
    {
    do
    {
    beurtSpelerEen = 0;
    beurtSpelerTwee ++;
    System.out.prin t("Speler2:") ;
    ingaveSpel = input.nextInt() ;

    if(ingaveSpel == 1)
    {

    aantalLucifers -= 1;
    System.out.prin tln("Er blijven nog: "+aantalLucifer s+ " lucifers over.\n");
    next = "ja";
    }
    else if(ingaveSpel == 2)
    {
    aantalLucifers -= 2;
    System.out.prin tln("Er blijven nog: "+aantalLucifer s+ " lucifers over.\n");
    next = "ja";
    }
    else if(ingaveSpel == 3)
    {
    aantalLucifers -= 3;
    System.out.prin tln("Er blijven nog: "+aantalLucifer s+ " lucifers over.\n");
    next = "ja";
    }
    else
    {
    JOptionPane.sho wMessageDialog( null,"Foutieve ingave...\nGeef een aantal tussen 1 en 3 in aub\n");
    next = "nee";
    }
    } while(next != "ja");
    }


    public void bepaalVerliezer ()
    {
    if((aantalLucif ers <= 0) && (beurtSpelerEen == 1))
    {
    eindeSpel = "ja";
    JOptionPane.sho wMessageDialog( null,"Speler1 nam de laatste lucifer.\n\nGAM E OVER!\n");
    }
    else if((aantalLucif ers <= 0) && (beurtSpelerTwe e == 1))
    {
    eindeSpel = "ja";
    JOptionPane.sho wMessageDialog( null,"Speler2 nam de laatste lucifer.\n\nGAM E OVER!\n");
    }
    }
    }
    [/code]

    The problem I have is when I enter a string; example -> test or anything else. Then he gives an error instead of asking for an integer.

    I hope you guys can help me out..
    Last edited by JosAH; Dec 15 '07, 01:08 PM. Reason: added [code] ... [/code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Read the API documentation for the Scanner class and pay special attention
    to the hasNextInt method.

    kind regards,

    Jos

    Comment

    • jri
      New Member
      • Dec 2007
      • 3

      #3
      Hey,

      I have read the API about Scanner but I'm not able to find the problem inside my program. Could you help me out with what I have to adjust?

      Regards

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        From you program!!!

        That exception occurs when:


        integer is required but a non-integer was found....


        try&catch....

        Just read the book... Exception Handling and data type conversion...

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by jri
          Hey,

          I have read the API about Scanner but I'm not able to find the problem inside my program. Could you help me out with what I have to adjust?

          Regards
          Well, now you blindly trust the user that s/he will type an int:

          [code=java]
          int value= scanner.nextInt ();
          [/code]

          Before you attempt to read anything from the user's input you should call the
          method I mentioned first:

          [code=java]
          if (scanner.hasNex tInt())
          value= scanner.nextInt ();
          else
          // protest int the strongliest way
          [/code]

          I leave an adequate handling of erroneous input to your imagination ;-)

          kind regards,

          Jos

          Comment

          • jri
            New Member
            • Dec 2007
            • 3

            #6
            Hello,

            If I try to use that code my Jcreator gives an error that he can't find the method.. not sure what the problem is, can someone show me how I have to implement it inside my existing program? I hoped to find it myself but I keep failing in it.
            I hope someone can help me with it cause I need to drop it at the lector's site on Monday :x

            Kind regards,

            Jri

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by jri
              Hello,

              If I try to use that code my Jcreator gives an error that he can't find the method..
              I hope you didn't simply copy/pasted my code snippet because the intention of it
              was to show you the structure of what you should implement, there was no inention
              to be running code. Read the API docs for the Scanner class and see that these
              methods I mentioned are really there.

              kind regards,

              Jos

              Comment

              Working...