469,306 Members | 1,882 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

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

3
Hello,

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

This is what I got:

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.     import java.util.Scanner; 
  3.     public class Lucifer 
  4.  
  5.     {
  6.  
  7.     int aantalLucifers, beurtSpelerEen, beurtSpelerTwee;
  8.     int ingaveSpel;
  9.     Scanner input = new Scanner (System.in);
  10.     String next ="nee";
  11.     String eindeSpel ="nee";
  12.  
  13.  
  14.     public static void main(String[] args) 
  15.  
  16.     {
  17.         Lucifer luci = new Lucifer();
  18.         luci.start();
  19.     }
  20.  
  21.  
  22.     public void start() 
  23.  
  24.     {
  25.         berichtWelkom();
  26.         bepaalAantalLucifers();
  27.     do 
  28.     {
  29.         ingaveSpelerEen();
  30.         bepaalVerliezer();
  31.         ingaveSpelerTwee();
  32.         bepaalVerliezer();
  33.     }    
  34.     while((eindeSpel.equals("nee")) && (aantalLucifers > 0));
  35.     }
  36.  
  37.  
  38.     public void berichtWelkom() 
  39.  
  40.     {
  41.         JOptionPane.showMessageDialog(null,"Let the game begin\n\nPress OK to play");
  42.     }
  43.  
  44.  
  45.     public void bepaalAantalLucifers() 
  46.     {
  47.         aantalLucifers = (int)((Math.random()*20)+21);
  48.         System.out.println("We gaan spelen met "+aantalLucifers+" lucifers.\n");
  49.     }
  50.  
  51.  
  52.     public void ingaveSpelerEen() 
  53.  
  54.     {
  55.     do 
  56.     {
  57.         beurtSpelerTwee = 0;
  58.         beurtSpelerEen++;
  59.         System.out.print("Speler1:");
  60.         ingaveSpel = input.nextInt();
  61.  
  62.         if(ingaveSpel == 1) 
  63.     {
  64.         aantalLucifers -= 1;
  65.         System.out.println("Er blijven nog: "+aantalLucifers+ " lucifers over.\n");
  66.         next = "ja";
  67.        }
  68.         else if(ingaveSpel == 2)
  69.     {
  70.         aantalLucifers -= 2;
  71.         System.out.println("Er blijven nog: "+aantalLucifers+ " lucifers over.\n");
  72.         next = "ja";
  73.     }
  74.         else if(ingaveSpel == 3)
  75.     {
  76.         aantalLucifers -= 3;
  77.         System.out.println("Er blijven nog: "+aantalLucifers+ " lucifers over.\n");
  78.         next = "ja";
  79.     }
  80.     else
  81.     {
  82.         JOptionPane.showMessageDialog(null,"Foutieve ingave...\nGelieve een getal tussen 1 en 3 in te geven.\n");
  83.         next = "nee";
  84.     }
  85.     }   while(next != "ja");
  86.     }
  87.  
  88.  
  89.     public void ingaveSpelerTwee() 
  90.     {
  91.     do 
  92.     {
  93.         beurtSpelerEen = 0;
  94.         beurtSpelerTwee++;
  95.         System.out.print("Speler2:");
  96.         ingaveSpel = input.nextInt();
  97.  
  98.         if(ingaveSpel == 1)
  99.     {
  100.  
  101.         aantalLucifers -= 1;
  102.         System.out.println("Er blijven nog: "+aantalLucifers+ " lucifers over.\n");
  103.         next = "ja";
  104.        }
  105.         else if(ingaveSpel == 2)
  106.     {
  107.         aantalLucifers -= 2;
  108.         System.out.println("Er blijven nog: "+aantalLucifers+ " lucifers over.\n");
  109.         next = "ja";
  110.     }
  111.         else if(ingaveSpel == 3)
  112.     {
  113.         aantalLucifers -= 3;
  114.         System.out.println("Er blijven nog: "+aantalLucifers+ " lucifers over.\n");
  115.         next = "ja";
  116.     }
  117.     else 
  118.     {
  119.         JOptionPane.showMessageDialog(null,"Foutieve ingave...\nGeef een aantal tussen 1 en 3 in aub\n");
  120.         next = "nee";
  121.     }
  122.     }   while(next != "ja");
  123.     }
  124.  
  125.  
  126.     public void bepaalVerliezer() 
  127.     {
  128.         if((aantalLucifers <= 0) && (beurtSpelerEen == 1)) 
  129.     {
  130.         eindeSpel = "ja";
  131.         JOptionPane.showMessageDialog(null,"Speler1 nam de laatste lucifer.\n\nGAME OVER!\n");
  132.     }
  133.         else if((aantalLucifers <= 0) && (beurtSpelerTwee == 1)) 
  134.     {
  135.         eindeSpel = "ja";
  136.         JOptionPane.showMessageDialog(null,"Speler2 nam de laatste lucifer.\n\nGAME OVER!\n");
  137.     }         
  138.     }
  139.     }
  140.  
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..
Dec 15 '07 #1
6 13565
JosAH
11,448 Expert 8TB
Read the API documentation for the Scanner class and pay special attention
to the hasNextInt method.

kind regards,

Jos
Dec 15 '07 #2
jri
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
Dec 15 '07 #3
sukatoa
539 512MB
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...
Dec 15 '07 #4
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. int value= scanner.nextInt();
  2.  
Before you attempt to read anything from the user's input you should call the
method I mentioned first:

Expand|Select|Wrap|Line Numbers
  1. if (scanner.hasNextInt()) 
  2.    value= scanner.nextInt();
  3. else
  4.    // protest int the strongliest way
  5.  
I leave an adequate handling of erroneous input to your imagination ;-)

kind regards,

Jos
Dec 15 '07 #5
jri
3
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
Dec 15 '07 #6
JosAH
11,448 Expert 8TB
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
Dec 16 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by tiyaramunna | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.