I have the following code that might throw exceptions:
import java.util.*;
import java.io.*;
class Test {
public static void main (String [] args) {
try {
Scanner keyboard = new Scanner (System.in);
int n1, n2;
System.out.print("Type an int: ");
n1 = keyboard.nextInt();
System.out.print("Type another int: ");
n2 = keyboard.nextInt();
int r = n1/n2;
}
catch (ArithmeticException e) {
System.out.println("Divide by 0");
}
catch (InputMismatchException e) {
System.out.println("Wrong entry");
}
}
}
After an exception is thrown. I want the program to go back to asking the user to enter a new int again instead of exiting.
Wrap your code with a while (true) and add a break at the end of the try block, so that it will be reached only if no exceptions are thrown.
e.g.
while (true) {
try {
// your code goes here ....
break; // this will only be reached if no exceptions thrown
}
catch (...) {
}
}; // close the while loop
// this will be reached after the break only, i.e. no exceptions
while (true) {
try {
// your code
break;
}
catch (Exception e) {
}
};
I would use an infinite loop. You can exit it using a break statement when you are ready.
public static void main(String[] args) {
while (true) {
try {
#SuppressWarnings("resource")
Scanner keyboard = new Scanner(System.in);
int n1, n2;
System.out.print("Type an int: ");
n1 = keyboard.nextInt();
System.out.print("Type another int: ");
n2 = keyboard.nextInt();
int r = n1 / n2;
//Do whatever... use 'break' to exit the loop when you are done
} catch (ArithmeticException e) {
System.out.println("Divide by 0");
} catch (InputMismatchException e) {
System.out.println("Wrong entry");
}
}
}
I don't like the "while (true) with a break" idiom. I find it clearer in this case -- I rarely have rules that apply in all cases -- to introduce an auxiliary variable that controls looping.
boolean repeat;
do {
repeat = false;
try {
...stuff...
}
catch (SomeException ex) {
... error stuff...
repeat = true;
}
while (repeat);
This is (a) makes the loop termination clearer than arbitrary jumps ('break') out of the loop, and (b) makes it trivial when you find you have other reasons to re-do the loop body.
Related
I am trying to write a method that asks a user for a positive integer. If a positive integer is not inputted, a message will be outputted saying "Please enter a positive value". This part is not the issue. The issue is that when I try to implement a try catch statement that catches InputMismatchExceptions (in case user inputs a character or string by accident), the loop runs infinitely and spits out the error message associated with the InputMistmatchException.
Here is my code:
private static int nonNegativeInt(){
boolean properValue = false;
int variable = 0;
do {
try {
while (true) {
variable = scanner.nextInt();
if (variable < 0) {
System.out.println("Please enter a positive value");
} else if (variable >= 0) {
break;
}
}
properValue = true;
} catch (InputMismatchException e){
System.out.println("That is not a valid value.");
}
} while (properValue == false);
return variable;
}
Essentially what is happening is that the scanner runs into an error when the given token isn't valid so it can't advance past that value. When the next iteration starts back up again, scanner.nextInt() tries again to scan the next input value which is still the invalid one, since it never got past there.
What you want to do is add the line
scanner.next();
in your catch clause to basically say skip over that token.
Side note: Your method in general is unnecessarily long. You can shorten it into this.
private static int nonNegativeInt() {
int value = 0;
while (true) {
try {
if ((value = scanner.nextInt()) >= 0)
return value;
System.out.println("Please enter a positive number");
} catch (InputMismatchException e) {
System.out.println("That is not a valid value");
scanner.next();
}
}
}
you are catching the exception but you are not changing the value of variable proper value so the catch statement runs forever. Adding properValue = true; or even a break statement inside the catch statement gives you the required functionality!
I hope I helped!
You can declare the scanner at the start of the do-while-loop, so nextInt() will not throw an exception over and over.
private static int nonNegativeInt(){
boolean properValue = false;
int variable = 0;
do {
scanner = new Scanner(System.in);
try {
while (true) {
variable = scanner.nextInt();
if (variable < 0) {
System.out.println("Please enter a positive value");
} else if (variable >= 0) {
break;
}
}
properValue = true;
} catch (InputMismatchException e){
System.out.println("That is not a valid value.");
}
} while (properValue == false);
return variable;
}
This is indeed nearly identical to SO: Java Scanner exception handling
Two issues:
You need a scanner.next(); in your exception handler
... AND ...
You don't really need two loops. One loop will do just fine:
private static int nonNegativeInt(){
boolean properValue = false;
int variable = 0;
do {
try {
variable = scanner.nextInt();
if (variable < 0) {
System.out.println("Please enter a positive value");
continue;
} else if (variable >= 0) {
properValue = true;
}
} catch (InputMismatchException e){
System.out.println("That is not a valid value.");
scanner.next();
}
} while (properValue == false);
return variable;
}
Just add a break statement inside your catch.
Btw, you can get rid of the while loop by rewriting it like this:
try {
variable = scanner.nextInt();
if (variable < 0) {
System.out.println("Please enter a positive value");
} else {
properValue = true;
}
}
//...
This is the program that I have written & I want to solve this in some other way. I dont want to use the try-catch statement.
I have researched about the other waya but it was not clearly explained.
import java.io.*;
import java.util.*;
public class TPJava {
public static void main(String args[]) throws Exception {
Scanner scan = new Scanner(System.in);
try {
int int_var = scan.nextInt();
System.out.println("It is an Integer.");
}
catch (InputMismatchException e)
{
try
{
String str_var = scan.next();
System.out.println("It is a String");
}
catch (InputMismatchException ie)
{
try
{
Float f = scan.nextFloat();
System.out.println("It is Float");
}
catch (InputMismatchException ime)
{
System.out.println("Wrong Input.");
}
}
}
}
}
if (XXX instanceof int)
{
System.out.println("It is an integer.");
}
else if (XXX instanceof String)
{
System.out.println("It is a string.");
}
....
(and so on)
EDIT:
found a solution that should work for exactly your code.
Scanner scan= new Scanner(System.in);
if(scan.hasNextInt())
{
System.out.println("It is an integer.");
}
else if (scan.hasNextFloat())
{
System.out.println("It is a Float.");
}
....
else
{
System.out.println("It is a String.");
}
this checks if the next variable can be interpreted as a integer/float/ etc.....
i think there is none for "String" so i just used the "else" statement there
BUT: you have to user scan.next() or nextInt or something like that afterwards, else it will continuesly use the same input over and over again (i assume)
This question already has answers here:
What is a NumberFormatException and how can I fix it?
(9 answers)
Closed 6 years ago.
I'm not sure how to set it up, but my method has a user input a number between a minimum and maximum, although I have no idea how to handle the NumberFormatException with a try-catch block. Are there any suggestions in regards to how this can be fixed?
static int promptForInt(String prompt, int min, int max){
System.out.println(prompt);
String input = in.readLine();
int parsedInt = Integer.parseInt(input);
while(!(parsedInt > min && parsedInt < max)){
System.out.println("Your input is invalid. " + prompt);
input = in.readLine();
parsedInt = Integer.parseInt(input);
}
return parsedInt;
}
static int promptForInt(String prompt, int min, int max){
System.out.println(prompt);
String input = in.readLine();
int parsedInt;
boolean exceptionThrown = false;
do {
try {
parsedInt = Integer.parseInt(input);
} catch(NumberFormatException e) {
exceptionThrown = true;
}
if (exceptionThrown || (!(parsedInt > min && parsedInt < max)) {
System.out.println("Your input is invalid. " + prompt);
input = in.readLine();
parsedInt = Integer.parseInt(input);
} else {
return parsedInt;
}
} while(true)
}
From my post about NumberFormatException:
Ad. 4.
Finally we come to the place in which we agree, that we can't avoid situations when it's user typing "abc" as a numeric string. Why? Because he can. In a lucky case, it's because he's a tester or simply a geek. In a bad case it's the attacker.
What can I do now? Well, Java gives us try-catch you can do the following:
try {
i = Integer.parseInt(myString);
} catch (NumberFormatException e) {
e.printStackTrace();
//somehow workout the issue with an improper input. It's up to your business logic.
}
Exceptions
In Java Exceptions are used for marking unexpected situations. For example parsing non-numeric String to a number (NumberFormatException) or calling a method on a null reference (NullPointerException). You can catch them in many ways.
try{
//some code
} catch (NumberFormatException e1) {
e.printStackTrace() //very important - handles the Exception but prints the information!
} catch (NullPointerException e2) {
e.printStackTrace();
}
or using the fact, that they all extend Exception:
try {
//somecode
} catch (Exception e) {
e.printStackTrace;
};
or since Java 7:
try {
//somecode
} catch (NullPointerException | NumberFormatException e) {
e.printStackTrace;
};
Exceptions can be easier to deal with if you think of them as the method "Trying to do something, but it couldn't because of X". X is the exception.
The below code could be one way you modify your code to handle the exception:
static int promptForInt(String prompt, int min, int max) {
Integer parsedInt = null; // use an Integer so null can be used to mean an invalid value
while (parsedInt == null) {
System.out.println(prompt);
String input = in.readLine();
int temp;
try {
temp = Integer.parseInt(input);
} catch(NumberFormatException e) {
System.out.print(input+" is not a number. ");
continue;
}
if (temp < min || temp > max) {
System.out.print("Your number must be between "+min+" and "+max+" (inclusive). ");
} else {
parsedInt = temp;
}
}
return parsedInt;
}
Some things you should notice: Firstly, you have not defined in. You could do that like so:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
If you do that, you'll soon see that you have another Exception to deal with: this can throw UnsupportedEncodingException, and also readLine can throw IOException.
Your method must return a valid integer or it will not exit (you really should supply the user some means of exiting the loop without entering a valid number). Since that's not going to be possible if, for instance, you couldn't read anything from System.in your method needs a reasonable way of telling the caller: "I tried to get an int from the user, except I was stopped by X".
You may actually end up doing something more like:
static int promptForInt(String prompt, int min, int max) throws UserInputException {
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
} catch(UnsupportedEncodingException unsupported) {
// create a new exception and supply the cause as an inner exception
throw new UserInputException("Could not open a reader for System.in", unsupported);
}
Integer parsedInt = null; // use an Integer so null can be used to mean an invalid value
while (parsedInt == null) {
System.out.println(prompt);
String input;
try {
input = in.readLine();
} catch (IOException ioException) {
throw new UserInputException("Could not read a line", ioException);
}
if (input.length() == 0) {
throw new UserInputException("User aborted input");
}
int temp;
try {
temp = Integer.parseInt(input);
} catch(NumberFormatException e) {
System.out.print(input+" is not a number. ");
continue;
}
if (temp < min || temp > max) {
System.out.print("Your number must be between "+min+" and "+max+" (inclusive). ");
} else {
parsedInt = temp;
}
}
return parsedInt;
}
I just learned about the 'try' statement in Java, and what I'm trying to do is to have this input loop until the user's input is both an integer and a positive one.
This is my code so far:
int scanning () {
Scanner scan = new Scanner(System.in);
int input = 0;
boolean loop = false;
do {
try {
System.out.print("Amount: ");
input = scan.nextInt();
if (input < 0) {
System.out.println("Error. Invalid amount entered.");
loop = true;
}
} catch (Exception e) {
System.out.println("Error: Invalid input");
loop = true;
}
} while (loop);
return input;
}
However it goes through an infinite loop when the user inputs an invalid integer, printing the error message over and over. The expected outcome is to keep asking the user for a valid input.
This code will help you to be in infinite loop and also throw a exception when input is a -ve integer.
The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.
Most of the times when we are developing an application in java, we often feel a need to create and throw our own exceptions.So first create a user defined exception AmountException.
public class AmountException extends Exception {
private static final long serialVersionUID = 1L;
public AmountException() {
// TODO Auto-generated constructor stub
System.out.println("Error. Invalid amount entered");
}
}
And now edit your scanning() to this :
int scanning () {
Scanner scan = new Scanner(System.in);
int input = 0;
boolean loop = false;
do {
try {
System.out.print("Amount: ");
input = scan.nextInt();
if (input < 0) {
loop = true;
throw new AmountException();
} else {
loop = false;
}
} catch (AmountException e) {
}
} while (loop);
return input;
}
Reset the value of loop variable in the do-while loop before each time just before checking the condition.
do {
try {
System.out.print("Amount: ");
input = scan.nextInt();
loop = false; // Reset the variable here.
if (input < 0) {
System.out.println("Error. Invalid amount entered.");
loop = true;
}
} catch (Exception e) {
System.out.println("Error: Invalid input");
scan.next(); // This is to consume the new line character from the previous wrong input.
loop = true;
}
} while (loop);
From you code, Change loop to false and when the valid input is given, it will terminate the while loop
boolean loop = false;
do {
try {
loop = false;
System.out.print("Amount: ");
input = scan.nextInt();
if (input < 0) {
System.out.println("Error. Invalid amount entered.");
loop = true;
}
} catch (Exception e) {
System.out.println("Error: Invalid input");
loop = true;
}
Add an else block after if, otherwise, loop will always stay true if the first input is invalid.
if (input < 0) {
System.out.println("Error. Invalid amount entered.");
loop = true;
} else {
loop = false;
}
import java.util.Scanner;
public class Lab4_5 {
public static void main(String[]args) {
Scanner scan= new Scanner(System.in);
int rows=0;
int rowIndex=0, colIndex=0;
boolean choice1= true;
String y="y";
String n="n";
boolean first = true;
while (choice1==true) {
if (first==true) {
first=false;
System.out.println("Do you want to start(Y/N): ");
} else if (first==false) {
System.out.println("Do you want to continue(Y/N): ");
}
String choice2=scan.next();
if (choice2.equals(y)) {
System.out.println("How many rows/columns(5-21)?");
rows=scan.nextInt();
while (rows<5 || rows>21) {
System.out.println("That is either out of range or not an integer, try again! ");
rows=scan.nextInt();
}
System.out.println("What character?");
String choice3=scan.next();
System.out.println(" ");
for (rowIndex=1; rowIndex<=rows; rowIndex++) {
for (colIndex=1; colIndex<=rows; colIndex++) {
if (rowIndex==1 || rowIndex==rows || colIndex==1 || colIndex==rows) {
System.out.print(choice3);
} else {
System.out.print(" ");
}
}
System.out.println();
}
} else if(choice2.equals(n)) {
choice1 = false;
System.out.println("Thank you. Goodbye.");
} else {
System.out.println("Please either enter Y or N.");
}
}
}//end of main
}
The code prints what I need it to print, but I also have to have something in the code when it asks how many rows/columns to catch whether or not i input something other than an integer(in the part below). need some help, we haven't done anything yet with how to catch exceptions and i don't know how to start.
String choice2=scan.next();
if (choice2.equals(y)) {
System.out.println("How many rows/columns(5-21)?");
rows=scan.nextInt();
while (rows<5 || rows>21) {
System.out.println("That is either out of range or not an integer, try again! ");
rows=scan.nextInt();
}
}
You need to understand this please look into it.
Basic understanding is
try {
//Something that can throw an exception.
} catch (Exception e) {
// To do whatever when the exception is caught.
}
There is also an finally block which will always be execute even if there is an error. it is used like this
try {
//Something that can throw an exception.
} catch (Exception e) {
// To do whatever when the exception is caught & the returned.
} finally {
// This will always execute if there is an exception or no exception.
}
In your particular case you can have the following exceptions (link).
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
So you would need to catch exceptions like
try {
rows=scan.nextInt();
} catch (InputMismatchException e) {
// When the InputMismatchException is caught.
System.out.println("The next token does not match the Integer regular expression, or is out of range");
} catch (NoSuchElementException e) {
// When the NoSuchElementException is caught.
System.out.println("Input is exhausted");
} catch (IllegalStateException e) {
// When the IllegalStateException is caught.
System.out.println("Scanner is close");
}
You can create a try-catch block like so:
try {
int num = scan.nextInt();
} catch (InputMismatchException ex) {
// Exception handling here
}
If you want to implement this in your code, I suggest doing this:
while (true) {
try {
rows = scan.nextInt();
if (rows<5||rows>21) {
break;
}
else {
System.out.println("That is either out of range or not an integer, try again! ");
}
} catch (InputMismatchException ex) {
System.out.println("That is either out of range or not an integer, try again! ");
}
}
See here for more details.
String choice2=scan.next();
if(choice2.equals(y)){
System.out.println("How many rows/columns(5-21)?");
try
{
rows=scan.nextInt();
}catch(Exception e)
{
rows = -1;
}
while(rows<5||rows>21){
System.out.println("That is either out of range or not an integer, try again! ");
try
{
rows=scan.nextInt();
}catch(Exception e)
{
rows = -1;
}
}