Variable scope in try catch block - java

I am creating a user interface via command line that will ask for options from 1 - 4 and I want error checking. Only an integer between 1 - 4 is allowed. This is the code I have so far. I want the method to return the userInput integer to another method that will do some stuff with it.
package contactmanager;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* #author andyjohnson
*/
public class UserInterface {
public static Integer GetInput() {
Scanner in = new Scanner(System.in);
//Integer userInput;
System.out.println("Welcome to the contact manager\nMake a selection below:");
System.out.println("1)Display Contacts\n2)Add new business contact\n3)Add new personal contact\n4)Quit");
try {
Integer userInput = in.nextInt();
if (userInput < 1 || userInput > 4) {
System.out.println("Please enter a valid selection");
UserInterface.GetInput();
}
} catch (InputMismatchException e) {
e.getMessage();
System.out.println("Please enter a valid selection");
UserInterface.GetInput();
}
return userInput;
}
}
My return statement is underlined in the IDE and is telling me it isn't initialized. I want to initialize it globally but allow the try statement to change the value. I've tried this.userInput = userInput but I can't figure out where my scope is broken. How do I give the try block global scope? I'm new to java, so anything is helpful. Thanks!

You can just declare userInput variable outside the try-catch block:
package contactmanager;
import java.util.InputMismatchException;
import java.util.Scanner;
public class UserInterface {
public static Integer GetInput() {
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the contact manager\nMake a selection below:");
System.out.println("1)Display Contacts\n2)Add new business contact\n3)Add new personal contact\n4)Quit");
Integer userInput = null;
try {
userInput = in.nextInt();
if (userInput < 1 || userInput > 4) {
System.out.println("Please enter a valid selection");
UserInterface.GetInput();
}
} catch (InputMismatchException e) {
e.getMessage();
System.out.println("Please enter a valid selection");
UserInterface.GetInput();
}
return userInput;
}
}

The try-catch block has a scope that is dissimilar to just writing code in-line with the rest of the method.
I think the problem you are having is in this line where the variable userInput is first declared inside the try-catch block:
Integer userInput = in.nextInt();
The reason why this is an issue:
Consider if the try-catch block failed. What would then be returned? The variable userInput hasn't even been defined, so Java would have no idea what to return.
The fix is relatively simple. You want to just move it out of the try-catch block, like so. That should get rid of your return error. I noticed you commented this change out. Why?
But I have an additional suggestion. Why are you calling UserInterface.GetInput()? Why not have the method accept the parameter of the valid input and simply don't call it when the data is not formatted appropriately? Do you use it? This would take away the need of a truly global-scoped variable.
Because of how this method is written, it must return an Integer of some kind, unless you write it should that the method throws an exception that is caught somewhere downstream.
I tried to make some fixes I think would make the most sense:
Scanner in = new Scanner(System.in);
Integer userInput; // Integer representation of user input
try {
Integer a = in.nextInt(); // if a can be assigned to an Integer this line work
if (a < 1 || a > 4) {
// called if the input can be assigned to an Integer and is within the range
userInput = a;
}
} catch (InputMismatchException e) {
// otherwise the catch block is called
System.out.println("Please enter a valid selection");
}
return userInput;
Maybe you want to call UserInterface.GetInput() inside the range check? Hope this helps!
Edit: Using a sentinel flag instead of recalling the method
Scanner input = new Scanner(System.in);
System.out.println("Please enter a valid Integer. When done type DONE ");
// this method will keep cycling until the String DONE is typed
// you could make this condition whatever you want
while (!input.hasNext("DONE")) {
String a = input.next(); // gets the next item from the Scanner
try {
Integer b = Integer.parseInt(a); // tries to 'cast' the String to an Integer
if (b < 1 || b > 4) {
System.out.println("Input is valid!");
} else {
System.out.println("Input is invalid!");
}
} catch (NumberFormatException e) {
System.out.println("Please enter a valid selection!");
}
}

Related

How can I validate user input in Java

I am currently experimenting with Java, trying to get the user to input an integer. If the user doesn't enter an integer I want a message to appear saying "You need to enter an Integer: " with a completely new input field to the original one.
Code:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner inputScanner = new Scanner(System.in);
int counter = 0;
boolean run = true;
int userInput = 0;
while (run) {
System.out.print("Enter an integer: ");
if (inputScanner.hasNextInt()) {
userInput = inputScanner.nextInt();
} else if (!inputScanner.hasNextInt()) {
while (!inputScanner.hasNextInt()) {
System.out.print("You need to enter an Integer: ");
userInput = inputScanner.nextInt();
}
}
System.out.println(userInput);
if (counter == 6) {
run = false;
}
counter++;
}
}
}
At the moment the code above gives an Exception error ("java.util.InputMismatchException"). I have tried to use a try/catch but this doesn't really work because I want the user to see the second message ("You need to enter an Integer") everytime they don't enter an integer and I don't want it to re-loop around the main run loop for the same reason. I'm sure there is a better way to do this, however I am not sure of it. Any help will be massively appreciated, thanks in advance.
In this case it would make more sense for the Scanner to use hasNextLine and then convert the String to an Integer. If that you could do something like this:
try {
new Integer(inputScanner.hasNextLine);
} catch (Exception e) {
System.out.println(“<error message>”)
}
In place of the if(inputScanner.hasNextInt()) due to the fact that the hasNextInt function will error out if there is not an Integer to be read.

Why is this Scanner assigning null to a variable?

For a college assessment I'm having to use a Scanner called sc with a class-level scope, and the entirety of the program has to be contained in a single class. The main method calls a menu() method, which uses the Scanner and a for loop to call one of two methods in response to user input.
One of the two methods uses the Scanner to calculate the factorial of an input integer. Once the method is executed, the for loop in menu() continues. To avoid an InputMismatchException due to the user entering a float, I used try/catch. However when the program returns back to the menu() for loop the Scanner causes an InputMismatchException when assigning to choice. How can I get Scanner to prompt the user for input again? Apologies if I'm missing something obvious, this is the first programming language I've ever learned. This should be the stripped down compilable code:
package summativeassessment;
import java.util.InputMismatchException;
import java.util.Scanner;
public class SummativeAssessment {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
menu();
}
public static void menu(){
String fName;
String sName;
System.out.print("Enter your first name: ");
fName = sc.next();
System.out.print("Enter your last name: ");
sName = sc.next();
try{
for(int choice = 1; choice!=0;){
System.out.print("Option 1 to generate username. Option 2 to calculate factorial. Press 0 to quit: ");
choice = sc.nextInt();
switch(choice){
case 2:
System.out.println(fName+" "+sName+", you have selected option 2");
numberFactorial();
break;
case 0:
break;
default:
System.out.println("Invalid option. Please try again.");
}
}
} catch(InputMismatchException ex){
String msg = ex.getMessage();
System.out.println(msg);
}
}
public static void numberFactorial(){
System.out.print("Enter a number: ");
try{
int numIn = sc.nextInt();
long result = numIn;
if(numIn>0){
for(int factor = 1; factor<numIn; factor++){
result *= factor;
if(factor==numIn-1){
System.out.println("The factorial is "+result);
}
}
}
else{
System.out.println("Enter a positive integer greater than 0");
}
}
catch(InputMismatchException ex){
System.out.println("Input invalid");
}
}
}
I debugged your code and got this result:
If you enter a float as input you trigger the InputMismatchException but there is still something in your buffer. So the next time sc.nextInt() is called, it won't wait until you input a value because something is in the buffer already, so it takes the next value out of the buffer and tries to interpret is as an integer. However, it fails to do so, because it is not an integer, so an InputMismatchException is raised again and caught in your menu's catch, now leading to the exit of the program.
The solution is to draw whatever is left in the buffer after the exception was raised the first time.
So the working code will contain a buffer clearing sc.next() inside the exception:
public static void numberFactorial(){
System.out.print("Enter a number: ");
try{
int numIn = sc.nextInt();
long result = numIn;
if(numIn>0){
for(int factor = 1; factor<numIn; factor++){
result *= factor;
if(factor==numIn-1){
System.out.println("The factorial is "+result);
}
}
}
else{
System.out.println("Enter a positive integer greater than 0");
}
}
catch(InputMismatchException ex){
System.out.println("Input invalid");
sc.next();
}
}

Try-catch only loops once

I have a try-catch that is meant to catch anything that is not an integer. When I enter a non integer (e.g. 5.6) it tells me only integers are allowed and lets me try again (as it should). But if I enter a non-integer again it doesn't say anything and will keep taking inputs, leaving output blank.
if (choicesObjects == b) {
System.out.println("TEST 2");
System.out.println("Object: Right triangle");
System.out.println("\nEnter length of Right triangle: ");
int lengthOfTriangle = 0;
try {
lengthOfTriangle = input.nextInt();
} catch(InputMismatchException e) {
System.out.println("\nError: user input must be an integer greater than 0.\n");
System.out.println("Object: Right triangle");
System.out.println("\nEnter length of Right triangle: ");
input.next();
}
//method stuff
}
The try/catch statement is not a loop. It will always be executed once.
Of course, if there is a loop inside the try block, that block will keep executing until terminated. But such a loop requires an explicit command like while or for to be used.
Apparently what happens when entering a non-integer value (e.g., 5.6), is that the nextInt() statement throws an Exception and goes to the catch block. A better explanation can be given if the full code of the method is provided.
For this you could define a function, something like this should work
private int getNextInt(Scanner input) {
boolean isInt = false;
int userInput;
while(!isInt) {
try {
userInput = Integer.valueOf(input.next());
isInt = true;
} catch(NumberFormatException e) {
// Do nothing with the exception
}
}
return userInput;
}
This should run until an input given was an int and then return said int
You can update your code to something like this -
Scanner in = new Scanner(System.in);
int num = 0;
while(true) {
try{
num = in.nextInt();
break;
}catch(Exception e){
//print statements
System.out.println("Try again");
}
}
System.out.println("Done");
something like this
Boolean check = true;
while (check) {
if choicesObjects == b {
enter code here` System.out.println("TEST 2");
System.out.println("Object: Right triangle");
System.out.println("\nEnter length of Right triangle: ");
int lengthOfTriangle = 0;
try {
lengthOfTriangle = input.nextInt();
} catch(InputMismatchException e) {
System.out.println("\nError: user input must be an integer greater than 0.\n");
check = false;
System.out.println("Object: Right triangle");System.out.println("\nEnter length of Right triangle:");
input.next();
}
//method stuff
}
}
`

Scaner nextInt() - Shall I handle the InputMismatchException thrown by this method when user enters something other than integer?

While using the nextInt() method of Scanner class, if InputMismatchException is being thrown, shall I handle that by catch block ?
It's a runtime exception, but caused by user input and not programmer's mistake.
Here is my code.
package com.sample.programs;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ScannerPractice {
public static void main(String[] args) {
readInteger();
}
private static void readInteger() {
// Created a Scanner object
Scanner input = new Scanner(System.in);
// Display a prompt text
System.out.println("Please enter an integer");
// Accept the input from user
int number;
try {
number = input.nextInt();
// Display the output to user
System.out.println("You entered: " + number);
} catch (InputMismatchException exception) {
System.err.println("You have entered wrong input. Please enter a number");
// Log the stack trace
readInteger();
} finally {
input.close();
}
}
}
Yes. Is better to handle the user wrong input beacouse you cannot control or be sure that the user will aligned data correctly, and you cannot read doubles, or strings with readInteger().
So I will handle the exception.
Regards.
No, you should call hasNextInt() before calling nextInt().
The exception truly means programmer error, since the programmer forgot to check validity before calling the method.
If you then want to prompt the user again, remember to discard the bad input first.
Scanner input = new Scanner(System.in);
int value;
for (;;) {
System.out.println("Enter number between 1 and 10:");
if (! input.hasNextInt()) {
System.out.println("** Not a number");
input.nextLine(); // discard bad input
continue; // prompt again
}
value = input.nextInt();
if (value < 1 || value > 10) {
System.out.println("** Number must be between 1 and 10");
input.nextLine(); // discard any bad input following number
continue; // prompt again
}
if (! input.nextLine().trim().isEmpty()) {
System.out.println("** Bad input found after number");
continue; // prompt again
}
break; // we got good value
}
// use value here
// don't close input

How to make a scanner be able to take in both an INT and String. Java

I had a bit of a hard time figuring this part out for a school project of mine.
So looking for a bit of clarification.
Generally, the user had to input a number (column) to insert a game piece.
However, if the user were to enter "q" the program would close down.
We were pointed into the direction of using "parseInt", however i am looking for a bit of clarification as to how this works?
while(response.equalsIgnoreCase("q"));
{
System.out.println("Do you want to play again?");
response = scan.next();
}
System.out.println("Do you want to play again?");
response = scan.next(); // this revisits the while loop that
// prompted the player to play.
import java.util.Scanner;
public class ScanInteger {
public static void main(String...args)throws Throwable {
int num = 0; String s= null;
System.out.print("Please enter a number : ");
Scanner sc = new Scanner(System.in);
do{
try{
s = sc.next();
num= Integer.parseInt(s);
System.out.println("You have entered: "+num+" enter again : ");
}catch(NumberFormatException e){
if(!s.equalsIgnoreCase("q"))
System.out.println("Please enter q to quit else try again ==> ");
}
}while(!s.equalsIgnoreCase("q"));
sc.close();
}
}
See http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
public static int parseInt(String s)
throws NumberFormatException
You want to apply a try-catch block around the Integer.parseInt(userInput) call to catch a NumberFormatException
In the catch body, you can set a flag that the input was invalid. Put the whole thing in a while loop based on the boolean isInputIsValid.
boolean isValidNumber = false, wantsToQuit = false;
while (!isValidNumber) {
// Ask user for a value any way you want and save it to userInput
String userInput = "";
try {
Integer.parseInt(userInput);
isValidNumber = true;
} catch (NumberFormatException e) {
isValidNumber = false;
if (userInput.equals("q")) {
wantsToQuit = true;
break;
}
}
}
wantsToQuit is not a necessary variable, just showing what the purpose of that section is

Categories