473,699 Members | 2,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem Exception in thread "main" java.util.Input MismatchExcepti on

3 New Member
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 13828
JosAH
11,448 Recognized Expert MVP
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 New Member
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 Contributor
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 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
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
2998
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 webpage and installed it using SGI's swmgr application. The installation was very straight forward and there were no errors when I installed the package. Then I ran /usr/java2/bin/javac SelectSockets.java to make the SelectSockets.class file.
1
9114
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 version 1.4.0 and WMQ Series version 5.3 with CSD04. Exception in thread "main" java.lang.NoClassDefFoundError: com/ibm/mq/server/MQSESSION at com.ibm.mq.MQSESSIONServer.getMQSESSION(MQSESSIONServer.java:67) at...
12
7121
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" java.lang.NullPointerException at ticketSales.TicketSales.makeEvent(TicketSales.java:185) at ticketSales.TicketInput.main(TicketInput.java:56) Anyway I have several Class This one is called TransAction
3
6439
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
16710
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 CsvTest.readFile(CsvTest.java:26) at Trial.main(Trial.java:24) I have no idea what to change here because I don't know what is wrong..please help me out... import java.io.*; import java.util.*;
9
3282
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 the following error Exception in thread "main" java.lang.NoClassDefFoundError: test Caused by: java.lang.ClassNotFoundException: test at java.net.URLClassLoader$1.run(Unknown Source) at...
4
14721
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 "appframework-1.0.3.jar;swing-worker-1.1.jar";CurrentStrobe.jar com.visionpro.currentstrobe.CurrentStrobeApp However, the statement below produces the error: java -cp "appframework-1.0.3.jar;swing-worker-1.1.jar" -jar CurrentStrobe.jar Exception in thread "main"...
1
13728
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
7652
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 sortmergejoin.MergeJoin.Field(MergeJoin.java:204) at sortmergejoin.MergeJoin.SMJoin(MergeJoin.java:84) at sortmergejoin.MergeJoin.<init>(MergeJoin.java:34) at sortmergejoin.Main.main(Main.java:24) Java Result: 1"
1
6753
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 "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1041) at java.awt.Container.add(Container.java:365) at orderingsystem.OrderingSystem.<init>(OrderingSystem.java:261) at...
0
8705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8628
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8899
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
3075
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.