473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 13812
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

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

Similar topics

0
by: Phillip Montgomery | last post by:
Hello all; I'm trying to debug an issue with a java script called, SelectSockets. It appears to be a fairly common one found on the web. I downloaded the SGI Java v1.4.1 installation from SGI's...
1
by: Andy Howells | last post by:
Can anybody help me on this? I am getting the below error but have not got a clue why. The file in my classpath eing used has the class that it says is not defined. Any ideas? I am running java...
12
nomad
by: nomad | last post by:
Hi everyone; My Class has ended and I was not able to solve this problem in time, and I would still like to solve it. I got these error code. Exception in thread "main"...
3
by: Ananthu | last post by:
Hi This is my codings in order to access mysql database from java. Codings: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
4
by: HaifaCarina | last post by:
here's the complete lines of errors.. Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:332) at...
9
by: tiyaramunna | last post by:
I am trying to configure my system with Java program just to practice on the coding....when i compile a test.java program i am able to see the class file but i cant run the program ... I am getting...
4
by: jmitch89 | last post by:
I don't why I get this error: Exception in thread "main" java.lang.NoClassDefFoundError The statement below works just fine: java -cp...
1
by: jimgym1989 | last post by:
I dont get it..why is the error: Exception in thread "main" java.util.InputMismatchException this is my code /** * @(#)textFileRead.java * * * @author * @version 1.00 2008/10/17 */
3
by: ohadr | last post by:
hi, i get Exception in thread "main" java.lang.NullPointerException when i run my application. the exact error is: "Exception in thread "main" java.lang.NullPointerException at...
1
by: onlinegear | last post by:
HI i am writing this for college i know i have loads of combo boxes with nothing in the i havent got that far yet. but every time i run this is comes up with this erro run: Exception in thread...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.