Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
A Functional Approach to Java: Augmenting Object-Oriented Java Code with Functional Principles
this week in the
Functional programming
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Liutauras Vilda
Ron McLeod
Jeanne Boyarsky
Paul Clapham
Sheriffs:
Junilu Lacar
Tim Cooke
Saloon Keepers:
Carey Brown
Stephan van Hulst
Tim Holloway
Peter Rooke
Himai Minh
Bartenders:
Piet Souris
Mikalai Zaikin
Forum:
Beginning Java
InputMismatchException not working?
Jake Cirino
Greenhorn
Posts: 17
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here's my code, I wan't the user to enter integers only but this code doesn't seem to be working.
//Modes: //Null = 0 //Quadratic = 1 import java.util.Scanner; import java.util.InputMismatchException; public class Start { public static int mode = 0; private static int nInt; private static Scanner input; public static void main(String args[]){ menu(); } public static void menu(){ System.out.println("\nWelcome to Homework-Buddy."); System.out.println("Please type in a number for the mode you want below:"); System.out.println("\n1.Quadratic Formula"); menuAction(); } public static void menuAction(){ input = new Scanner(System.in); nInt = input.nextInt(); Thread tMenuAction = new Thread(){ public void run(){ try { mode = nInt; //mode1(); }catch(InputMismatchException e){ System.out.println("Unrecognised input or invalid number."); } } }; tMenuAction.start(); } public static void mode1(){ } }
The error is at about line 30.
John Jai
Rancher
Posts: 1776
posted 10 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
If you want to catch the Exception, then you have to surround the code that takes the Scanner input. Below is the code that reads the input. You have misplaced your try-catch block.
nInt = input.nextInt();
Rob Spoor
Sheriff
Posts: 22743
129
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You shouldn't catch an InputMismatchException,
you should
prevent it:
if (input.hasNextInt()) { nInt = input.nextInt(); } else if (input.hasNext()) { // there is something but not an int; take it away // show an error or something input.next(); }
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Initial variables before a try\catch block
Problem with PrintWriter and another Problem with Inputhandling
Looping in exceptions
Problem in looping
try...catch blocks giving <identifier> expected error
More...