Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
try {
System.out.println("how many times");
rollnumber = scanner.nextInt();
nigh=2;
}catch (Exception e){
System.out.print("invalid. re-enter");
}
}while (nigh==1);
It keeps printing out infinity "invalid re-enterhow many times"
You get into an infinite loop because scanner.nextInt(); does not consume characters from the input on error. Change the catch clause as follows to make it work:
try {
... // Your code
} catch (Exception e){
System.out.print("invalid. re-enter");
scanner.nextLine();
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
The question is duplicate but I can't find a proper solution so please help me out with this,
I have to convert the credit card number(from edittext) to int.
Credit card number like : "3333 3333 3333 3333"
I removed white space using String removeWhiteSpace = cardNumEt.getText().toString().replace(" ", "");
Than converted to int like :
try
{
int nIntFromET = Integer.parseInt(removeWhiteSpace);
}
catch (NumberFormatException e)
{
Log.e("exptn",e.toString());
}
but unfortunately it's giving me exception :
java.lang.NumberFormatException: For input string: "3333333333333333"
The int type in Java can be used to represent any whole number from -2147483648 to 2147483647.
Your value is above the maximum positive integer.
But you can use instead long and BigInteger
class Scratch {
public static void main(String[] args) {
try
{
int nIntFromET = Integer.parseInt("2147483647");
}
catch (NumberFormatException e)
{
System.out.println(e);
}
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is there anyway to handle the exceptions in Java and prevent the program from getting terminated? For instance when the user enters an invalid number into a calculator, zero in this example for division in the denominator, I don't want the program to get terminated and show the handled exception message. I want the program to continue and ask for another input.
Could anyone clarify it with a practical example for me?
Simple: put a loop around the whole try catch block; like:
boolean loop = true;
while (loop) {
try {
fetch input
loop = false;
} catch (SomeException se) {
print some message
}
does the job in general.
Try this:
boolean exceptionOccured;
do {
try {
exceptionOccured = false;
// code to read input and perform mathematical calculation
// eg: a = 10/0;
} catch(Exception e) {
exceptionOccured = true;
System.out.pritnln("Invalid input! Please try again");
} finally {
// some code that has to be executed for sure
}
} while(exceptionOccured);
First the code inside the try block will be executed. When an execption occurs (like division by zero), execution of code jumps from try block to catch block where you can write your logic to loop the try-catch block.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to create a method to introduce an int with NetBeans, but I have a problem when I run the method, the order of console messages is not correct, someone knows what the problem is:
public static void main(String[] args)
{
Scanner teclado = new Scanner(System. in );
int num;
boolean error;
public int introducirDatos()
{
do
{
error = false;
try
{
System.out.println("Introduzca un número entero: ");
num = Integer.valueOf(teclado.nextLine());
}
catch (NumberFormatException e)
{
System.err.println("Debe introducir un número y sin decimales, vuelve a intentarlo.\n");
error = true;
}
} while (error == true);
return num;
}
}
Thanks.
I believe you are running the code in an IDE - IntelliJ IDEA or Eclipse. The line System.out.println("Introduzca un número entero: "); prints to standard OUT, but System.err.println("Debe introdu... prints to standard ERROR. That's why the output gets messed up. Replace System.err with System.out for the messages being printed nicely one after another.
BTW you haven't specified which language you are using. Is it Java? You might want to update the question tags.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having a trouble figuring out how to solve this issue when it just stops when I call this method when I input letters and other types. What is the problem with my code? Can anyone help? Thanks in advance!
Here is my code:
public int selectOption(int maxRange, String sourceType) throws IOException
{
do
{
try
{
userInput = input.nextInt();
}//end try
catch(Exception e)
{
validInput=false;
input.next();
}//end catch
if (userInput<1 || userInput>maxRange)
{
MovieHouse.clearScreen();
System.out.println("Select from the options only!");
loadHeader();
if(sourceType.equals("main"))
MovieHouse.homeMenu();
else if(sourceType.equals("movie menu"))
loadMovieMenu();
}//end if
}while(userInput<1 || userInput>maxRange || validInput==false);
return userInput;
}//end selectOption
"input letters" - Does it mean normally the program take digits and you are providing char as input.
Not clear what is your issue.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am taking input as integer using Scanner class. like this:in.nextInt();
I need to prompt "wrong input" if user has entered any floating point number or character or string.
How can i accomplish this ?
nextInt() can only return an int if the InputStream contains an int as the next readable token.
If you want to validate input, you should use something like nextLine() to read a full String, and use Integer.parseInt(thatString) to check if it is an integer.
The method will throw a
NumberFormatException - if the string does not contain a parsable integer.
As I mentioned in the comments, try using the try-catch statement
int someInteger;
try {
someInteger = Scanner.nextInt();
} catch (Exception e) {
System.out.println("The value you have input is not a valid integer");
}
Put it in a try-catch body.
String input = scanner.next();
int inputInt 0;
try
{
inputInt = Integer.parseInt(input);
} catch (Exception e)
{
System.out.println("Wrong input");
System.exit(-1);
}