1,105,363 Community Members

InputMisMatchException

Member Avatar
usiyalla
Newbie Poster
21 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi guys i'm new in java and this site too
i was making a small program but this mismatchexception making me angry
i really appreciate if u answer my these stupid questions
1. what is token...
2. Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.nextInt(Unknown Source)[/TEX][/TEX][/TEX]
at java.util.Scanner.nextInt(Unknown Source)
at Main.check(Main.java:63)
at Main.doMain(Main.java:22)
at MiniCinwma.main(MiniCinwma.java:6)
From MiniCinema class

Main mini =new Main();
        	mini.doMain();// this is error line MiniCinwma.java:6

From then in doMain() i called check()
In check() method

B.displayTime();
			System.out.println("You have to pay just " +  B.getTimePrice(input.nextInt())+ " to enjoy this movie"); //here error comes line 63
			B.ticket();

in above line i just get input of int type pass it to getTimePrice(int a){this method also accepting int} then way there is something like input[mismatchexception?
i'm really confuse.

Member Avatar
tong1
Posting Whiz
358 posts since Jul 2010
Reputation Points: 22 [?]
Q&As Helped to Solve: 66 [?]
Skill Endorsements: 0 [?]
 
0
 

1. Robert W. Sevesta (2002, Concepts of programming languages, 5th edition, page 107 ) pointed out that “ a token of a language is a category of its lexemes.”
“Formal descriptions of the syntax of programming languages, for simplicity’s sake, often do not include descriptions of the lowest-level syntactic units. These small units are called lexemes.”
2. When the class Scanner in java.util.* is used, you may create a instance:
Scanner in = new Scanner(System.in);
Then using the instance in :
int n = in.nextInt();
To receive an input in int.
When the input string is not a int string, e.g. “44k4”, an InputMissmatchException is thrown:
(see the thread” How to guard the input effectively”
http://www.daniweb.com/forums/thread299076.html
);

public static void main(String args[]){
int n=0;
Scanner in = new Scanner(System.in);

while(true){
try{
System.out.println("Input a non-negative integer:");
n = in.nextInt();
}catch(InputMismatchException e){
System.out.println("Your input is mismatched.");
in.next();
continue;
}
if (n<0){
System.out.println("You negative value is not valid.");
continue;
}
break;
}

System.out.printf("Your input is %d\n", factorial(n));
}
}

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Using the Scanner methods can be tricky. Scanner does some buffering of the input and can block for some calls.

Your code assumes that there is an int ready to be read. If you used a hasNext method before using the next method, you could be sure the data was an int

What data was entered by the user and read by the nextInt() method?
Can you copy and post the contents of the command prompt window?

1. what is token...

Token is usually described as a string of characters delimited by whitespace or begin/end of a line

Member Avatar
tong1
Posting Whiz
358 posts since Jul 2010
Reputation Points: 22 [?]
Q&As Helped to Solve: 66 [?]
Skill Endorsements: 0 [?]
 
0
 
/* The following program demonstrates how to guard 
 * a non-negative input in int. 
 * If the input is in int and also greater than -1, the valid input
 * will be shown on DOS window and program terminates.
 * If client inputs an value in non-int or an negative value in int, the program will
 * ask the user for re-input.
 **/
import java.util.*;
public class Input {
public static void main(String args[]){
int n=0;
Scanner in = new Scanner(System.in);

while(true){
try{
System.out.println("Input a non-negative integer:");
n = in.nextInt();
}catch(InputMismatchException e){
System.out.println("Your input is mismatched.");
in.next();
continue;
}
if (n<0){
System.out.println("You negative value is not valid.");
continue;
}
break;
}

System.out.printf("Your input is %d\n", n);
}
}

The output in DOS:
Input a non-negative integer:
3r3
Your input is mismatched.
Input a non-negative integer:
3.1415
Your input is mismatched.
Input a non-negative integer:
35
Your input is 35

Member Avatar
usiyalla
Newbie Poster
21 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Using the Scanner methods can be tricky. Scanner does some buffering of the input and can block for some calls.

Your code assumes that there is an int ready to be read. If you used a hasNext method before using the next method, you could be sure the data was an int

What data was entered by the user and read by the nextInt() method?
Can you copy and post the contents of the command prompt window?

Token is usually described as a string of characters delimited by whitespace or begin/end of a line

======================>Welcome to MiniCinema
For Block A( RobinHood ) press 1
For Block B( Kites ) press 2
For Block C( Eclipse ) press 3
For Block D( Go to hell ) press 4
1
You select :  RobinHood
For Morning enter 1 .
For AfterNoon enter 2 .
For Night enter 3 .           // Notice i even not getting time to put something if no input then how it miss matched :-(
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Block.displayTime(Block.java:26)
    at Main.check(Main.java:53)
    at Main.doMain(Main.java:22)
    at MiniCinwma.main(MiniCinwma.java:6)
</code>
<text>
There is a doMain() in main.java who display welcome to miniCinema.. and  other 4 lines then gets input(by scanner functioning  normally)and call a check() from main.java in its scope.
</text>
<code>
check() do if else on input like above i entered 1
        if(firstInput == 1){

            System.out.println("You select :  " + A.getMovieName());
            int b=A.displayTime();  //point to notice here error comes
            System.out.println(b);
            System.out.println("You have to pay just " + A.getTimePrice(b)+ " to enjoy this movie");
            A.ticket();
            A.seatInfo();

        }
//the above commented line int b=A.displayTime(); call displayTime() from Block.java
displayTime() is:
    public int displayTime(){
        System.out.println("For Morning enter 1 ."); 
        System.out.println("For AfterNoon enter 2 ."); 
        System.out.println("For Night enter 3 ."); 
        int a= input.nextInt();   // it give me mismatchedException
        return a;
    }

this int a =input.nextInt() not letting me to enter some thing just giving me error
I hope i explain my problem well please help and tell how i use hasNext() here ...

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
1
 

his int a =input.nextInt() not letting me to enter some thing just giving me error

What was entered before this point? Scanner will buffer/save what was input and try to give it to you on the nextXXXX call you make.

What code is around this line: at Block.displayTime(Block.java:26)

Can you run the program in the command prompt, copy and paste the screen contents here?
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Member Avatar
usiyalla
Newbie Poster
21 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

What was entered before this point? Scanner will buffer/save what was input and try to give it to you on the nextXXXX call you make.

What code is around this line: at Block.displayTime(Block.java:26)

Can you run the program in the command prompt, copy and paste the screen contents here?
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

i'm using eclipse
Block.java :

import java.util.Scanner;
public class Block {
	Scanner input = new Scanner("System.in");

	private int seats=35;
private String ShowName;
Time t = new Time("12:30 PM", "04:30 PM", "11:00 PM");
	
public Block(String moviname){
		ShowName = moviname;
}
	public String getMovieName(){
		return ShowName;
	}
	public void setTimePrice(int val){
		
		t.setMorningPrice(15*val);
		t.setAfterNoonPrice(20*val);
		t.setNightPrice(32*val);
	}
	public int displayTime(){
		System.out.println("For Morning enter 1 ."); 
		System.out.println("For AfterNoon enter 2 ."); 
		System.out.println("For Night enter 3 ."); 
		int a= input.nextInt();
		return a;
	}
	
	public double getTimePrice(int PriceOfWhatTime){
		if(PriceOfWhatTime ==1){
			return t.getMorningPrice();
		}
		else if(PriceOfWhatTime==2){
			return t.getAfterNoonPrice();
		}
		else if(PriceOfWhatTime==3){
			return t.getNightPrice();
		}
		else
		return -1;
		 
	}
	
	public void seatInfo(){
		seats--;
		if(seats<=35)
		{
			System.out.println("Now there are "+seats + "lefts");
			}
		else if(seats<=0)
			{
			System.out.println("Sorry No Seats available");
			}
		else
			System.out.println("Retry please");
			
	}

	public void ticket(){
		System.out.println("\nFor Ticket press 1 or anyother key to cancel");
		int b = input.nextInt();
		String a = (b==1) ? "Thank you here is your ticket." : "Feel sorry to let you go buy ;-(" ; 
		System.out.println(a);
		}



}

Main.java

import java.util.Scanner;

public class Main {
	Scanner input = new Scanner("System.in");
	private static int firstInput ;

	
	
	
	public void doMain(){
	
		Block A =new Block("RobinHood");
		A.setTimePrice(10);
		Block B =new Block("Kites");
		B.setTimePrice(12);
		Block C =new Block("Eclipse");
		C.setTimePrice(14);
		Block D =new Block("Go to hell");
		D.setTimePrice(15);	
	
		display(A,B,C,D);
		check(A, B, C, D);
		
	}
	
	
	public void display(Block objA,Block objB,Block objC,Block objD){
		Scanner input = new Scanner(System.in);	

		System.out.println("======================>Welcome to MiniCinema");
		System.out.println("For Block A( "+ objA.getMovieName()+" ) press 1");
		System.out.println("For Block B( "+ objB.getMovieName()+" ) press 2");
		System.out.println("For Block C( "+ objC.getMovieName()+" ) press 3");
		System.out.println("For Block D( "+ objD.getMovieName()+" ) press 4");
		try{
		firstInput=input.nextInt();
		}//end of try
		catch(Exception e){

			System.out.println("Error : Wrong Input...\n You get one other try ...  ");
			Scanner input1 = new Scanner(System.in);	
		//	firstInput=input1.nextInt();
			
			}//end of catch
		
	}
	

	public void check(Block A, Block B, Block C, Block D){
		if(firstInput == 1){
			
			System.out.println("You select :  " + A.getMovieName());
			int b=A.displayTime();
			System.out.println(b);
			System.out.println("You have to pay just " + A.getTimePrice(b)+ " to enjoy this movie");
			A.ticket();
			A.seatInfo();
			
		}
		else if(firstInput == 2){
			
			System.out.println("You select :  " + B.getMovieName());
			int b=B.displayTime();
			System.out.println("You have to pay just " + B.getTimePrice(b)+ " to enjoy this movie");
			B.ticket();
			B.seatInfo();

		}
		else if(firstInput == 3){
			
			System.out.println("You select :  " + C.getMovieName());
			int b=C.displayTime();
			System.out.println("You have to pay just " + C.getTimePrice(b)+ " to enjoy this movie");
			C.ticket();
			C.seatInfo();

		}
		else if(firstInput == 4){

			System.out.println("You select :  " + D.getMovieName());
			int b=D.displayTime();
			System.out.println("You have to pay just " + D.getTimePrice(b)+ " to enjoy this movie");
			D.ticket();
			D.seatInfo();

		}		
		
	}
}

in miniCinema.java i am just calling doMain()
Is there something is with my ExceptionHandling because there i use nextInt.

Member Avatar
usiyalla
Newbie Poster
21 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

What was entered before this point? Scanner will buffer/save what was input and try to give it to you on the nextXXXX call you make.

What code is around this line: at Block.displayTime(Block.java:26)

Can you run the program in the command prompt, copy and paste the screen contents here?
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

That's all when i run this program

======================>Welcome to MiniCinema
For Block A( RobinHood ) press 1
For Block B( Kites ) press 2
For Block C( Eclipse ) press 3
For Block D( Go to hell ) press 4
1                               //   here i entered 1
You select :  RobinHood
For Morning enter 1 .
For AfterNoon enter 2 .
For Night enter 3 .            
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at Block.displayTime(Block.java:25)
	at Main.check(Main.java:53)
	at Main.doMain(Main.java:22)
	at MiniCinwma.main(MiniCinwma.java:6)
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Try doing some debugging.
Add some statements just before where the error occurs to read in whatever is in Scanner's buffer and display it.
Something like:

String aStr = input.nextLine();  // get the bad stuff
System.out.println("aStr=" + aStr + "<");  // show it

Then you'll know what the nextInt() method is complaining about.

Member Avatar
usiyalla
Newbie Poster
21 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Try doing some debugging.
Add some statements just before where the error occurs to read in whatever is in Scanner's buffer and display it.
Something like:

String aStr = input.nextLine();  // get the bad stuff
System.out.println("aStr=" + aStr + "<");  // show it

Then you'll know what the nextInt() method is complaining about.

well if i use nextLine()
it gives me "system.in" back
and if i print nextInt then same output which i'm receiving earlier.
Main problem is that nextInt() not letting me do anything.

I did some other try but same problem.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
1
 
String aStr = input.nextLine();  // get the bad stuff
System.out.println("aStr=" + aStr + "<");  // show it

What was printed on the console when you did this?
Did you see: "aStr=System.in<"

well if i use nextLine()
it gives me "system.in" back

Got it.

Scanner input = new Scanner("System.in");

Remove the " in the above
Scanner was primed with the String: "System.in"

Member Avatar
usiyalla
Newbie Poster
21 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
String aStr = input.nextLine();  // get the bad stuff
System.out.println("aStr=" + aStr + "<");  // show it

What was printed on the console when you did this?
Did you see: "aStr=System.in<"


Got it.

Scanner input = new Scanner("System.in");

Remove the " in the above
Scanner was primed with the String: "System.in"

yeah .............
really you got it ................
thanks for wasting lot of your precious time on this stupid kinda error
ha ha ha
Now its working fine ....
:->
:P

Member Avatar
usiyalla
Newbie Poster
21 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Result: Scanner input = new Scanner (System.in)
Don't use quotation marks around System.in

Question Answered as of 3 Years Ago by NormR1 and tong1
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

It would help if you posted exactly what was printed on the console and not edit it.
It took me a few minutes to understand what was happening.
If you had posted: aStr=System.in<
I would have known faster.

When using println() for debug tracing it is important to have a unique id string (aStr=) and to have a terminating character "<" so you can know where and what is being printed.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: