Java Input Buffer Garbage - java

Why is good practice to empty the 'Garbage' from the input buffer in a block of code like this? What would happen if I didn't?
try{
age = scanner.nextInt();
// If the exception is thrown, the following line will be skipped over.
// The flow of execution goes directly to the catch statement (Hence, Exception is caught)
finish = true;
} catch(InputMismatchException e) {
System.out.println("Invalid input. age must be a number.");
// The following line empties the "garbage" left in the input buffer
scanner.next();
}

Assuming you are reading from the scanner in a loop, if you don't skip the invalid token, you will keep reading it forever. That's what scanner.next() does: it moves the scanner to the next token. See the below simple example, which outputs:
Found int: 123
Invalid input. age must be a number.
Skipping: abc
Found int: 456
Without the String s = scanner.next() line, it would keep printing "Invalid input" (you can try by commenting out the last 2 lines).
public static void main(String[] args) {
Scanner scanner = new Scanner("123 abc 456");
while (scanner.hasNext()) {
try {
int age = scanner.nextInt();
System.out.println("Found int: " + age);
} catch (InputMismatchException e) {
System.out.println("Invalid input. age must be a number.");
String s = scanner.next();
System.out.println("Skipping: " + s);
}
}
}

Related

NoSuchElementException Problem in User Input Java

I'm confused while using an Java program I created.
public static void main(String[] args) {
Scanner scanner1 = new Scanner(System.in);
int input1 = 0;
boolean Input1Real = false;
System.out.print("Your first input integer? ");
while (!Input1Real) {
String line = scanner1.nextLine();
try {
input1 = Integer.parseInt(line);
Input1Real = true;
}
catch (NumberFormatException e) {
System.out.println("Use an integer! Try again!");
System.out.print("Your first input integer? ");
}
}
System.out.println("Your first input is " + input1);
}
Initially, when a user Ctrl+D during the input, it will promptly end the program and display an error in the form of this,
Your first input integer? ^D
Class transformation time: 0.0073103s for 244 classes or 2.9960245901639343E-5s per class
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651);
at Playground.Test1.main(Test1.java:13)
Doing a bit of research I note that Ctrl+D terminates the input of sort. Therefore, I tried add few more lines to my codes to prevent the error from appearing again and instead printing a simple "Console has been terminated successfully!" and as far as my skills can go.
public static void main(String[] args) {
Scanner scanner1 = new Scanner(System.in);
int input1 = 0;
boolean Input1Real = false;
System.out.print("Your first input integer? ");
while (!Input1Real) {
String line = scanner1.nextLine();
try {
try {
input1 = Integer.parseInt(line);
Input1Real = true;
}
catch (NumberFormatException e) {
System.out.println("Use an integer! Try again!");
System.out.print("Your first input integer? ");
}
}
catch (NoSuchElementException e) {
System.out.println("Console has been terminated successfully!");
}
}
System.out.println("Your first input is " + input1);
}
In the end, I still got the same error.
Got it!, the code hasNext() will ensure that the error will not appear. This method is to check whether there is another line in the input of the scanner and to check if its filled or empty. I am also using null to check my statement after passing the loop so the program stops if the input value is still null while keeping the function of Ctrl+D.
public static void main(String[] args) {
Integer input1 = null;
System.out.println("Your first input integer? ");
Scanner scanner1 = new Scanner(System.in);
while(scanner1.hasNextLine()) {
String line = scanner1.nextLine();
try {
input1 = Integer.parseInt(line);
break;
}
catch (NumberFormatException e) {
System.out.println("Use an integer! Try again!");
System.out.println("Your first input integer? ");
}
}
if (input1 == null) {
System.out.println("Console has been terminated successfully!");
System.exit(0);
}
System.out.println(input1);
}
This solution is not prefect of course but I would appreciate if there were much simpler options.

How to find the number of characters in a string in java

Below code outputs only "Enter a string" and accepts user input however, does not display the number of characters in the string. Kindly help!
System.out.println("Enter a string");
Scanner scan = new Scanner(System.in);
String result = scan.nextLine();
try {
while (a != null) {
count++;
}
} catch (Exception e) {
System.out.println("Invalid string");
}
System.out.println("The number of characters are : " + count);
You can use String.length() to calculate the length of your input string:
System.out.println("Enter a string");
Scanner scan = new Scanner (System.in);
String a = scan.nextLine();
System.out.println("The number of characters are : " + a.length());
oschlueter is right. Also, when using java.util.Scanner to get a string from user input, you won't need a try/catch to look for invalid strings, as anything the user can input can be a String. However, you will need a Try/Catch when asking for a number through Scanner.nextInt.
Also, it's generally recommended to catch a specific error and not just Exception. In this case, you would catch java.util.InputMismatchException.

Do while goes infinite with try catch

I have a problem when trying to execute try-catch statement inside do while loop.I ask user to first enter letter and then a number and if he enters number correctly the program ends.If he enters letter instead of number the program should say "An error occurred please enter number " and ask user to enter number again but every time i type letter instead of number the program goes into an infinite loop and won't allow me to enter new value.
And just goes
"An error occurred you must enter number"
"Please enter number".
public class OmaBrisem {
public static void main(String[] args) {
Scanner tastatura = new Scanner(System.in);
boolean b = true;
int a = 0;
String r = "";
System.out.println("Please enter a letter");
r = tastatura.next();
do {
try {
System.out.println("Please enter numerical value");
a = tastatura.nextInt();
b = true;
} catch (Exception e) {
System.out.println("An error occured you must enter number");
b = false;
}
} while (!b);
}
}
Here's your problem. If the user enters a non-number where you expect a number, your nextInt() will raise an exception but it will not remove the letter from the input stream!
That means when you loop back to get the number again, the letter will still be there and your nextInt() will once again raise an exception. And so on, ad infinitum (or at least until the heat death of the universe, or the machine finally breaks down, whichever comes first).
One way to fix this would be to actually read/skip the next character when nextInt() fails so that it's removed from the input stream. You could basically do this with Scanner.findInLine(".") until Scanner.hasNextInt() returns true.
The following code shows one way to do this:
import java.util.Scanner;
public class MyTestProg {
public static void main(String [] args) {
Scanner inputScanner = new Scanner(System.in);
System.out.print("Enter letter, number: ");
// Get character, handling newlines as needed
String str = inputScanner.findInLine(".");
while (str == null) {
str = inputScanner.nextLine();
str = inputScanner.findInLine(".");
}
// Skip characters (incl. newline) until int available.
while (! inputScanner.hasNextInt()) {
String junk = inputScanner.findInLine(".");
if (junk == null) {
junk = inputScanner.nextLine();
}
System.out.println("Ignoring '" + junk + "'");
}
// Get integer and print both.
int num = inputScanner.nextInt();
System.out.println("Got '" + str + "' and " + num);
}
}
And the following transcript shows it in action:
Enter letter, number: Abcde42
Ignoring 'b'
Ignoring 'c'
Ignoring 'd'
Ignoring 'e'
Got 'A' and 42
I realized that my program ignores the Scanner object tastatura in try block whenever i insert wrong value and do while loop starts again so i created new object of Scanner class and called it tastatura2 and my program works fine.
Scanner tastatura = new Scanner(System.in);
boolean b = true;
int a = 0;
String r = "";
System.out.println("Please enter a letter");
r = tastatura.next();
do {
try {
Scanner tastatura2 = new Scanner(System.in);
System.out.println("Please enter numerical value");
a = tastatura2.nextInt();
b = true;
} catch (Exception e) {
System.out.println("An error occured you must enter number");
b = false;
}
} while (!b);
Just add tastatura.nextLine() in the catch block to discard the last input.
You can get out of the loop by break; when the Exception is caught ... So just add :
break;
on your catch Block :)
The catch clause would look like this :
catch (Exception e) {
System.out.println("An error occured you must enter number");
b = false;
break;
}

InputMismatchException for String input into integer field

I am having trouble with entering non-integers into an integer field. I am only taking precautions so that if another person uses/works on my program they don't get this InputMismatchException.
When I enter a non-digit character into the input variable, I get the above error. Is there any way to compensate for this like one could do for a NullPointerException when it comes to strings?
This code is redacted just to include the relevant portions causing the problem.
import java.util.Scanner;
class MyWorld {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
int input = 0;
System.out.println("What is your age? : ");
input = user_input.nextInt();
System.out.println("You are: " +input+ " years old");
}
}
You can use an if statement to check if user_input hasNextInt(). If the input is an integer, then set input equal to user_input.nextInt(). Otherwise, display a message stating that the input is invalid. This should prevent exceptions.
System.out.println("What is your age? : ");
if(user_input.hasNextInt()) {
input = user_input.nextInt();
}
else {
System.out.println("That is not an integer.");
}
Here is some more information about hasNextInt() from Javadocs.
On a side note, variable names in Java should follow the lowerMixedCase convention. For example, user_input should be changed to userInput.
You can add a try-catch block:
import java.util.Scanner;
class MyWorld {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
int input = 0;
System.out.println("What is your age? : ");
try{
input = user_input.nextInt();
}catch(InputMisMatchException ex)
System.out.println("An error ocurred");
}
System.out.println("You are: " +input+ " years old");
}
}
If you want to provide the user to enter another int you can create a boolean variable and make a do-while loop to repeat it. As follows:
boolean end = false;
//code
do
{
try{
input = user_input.nextInt();
end = true;
}catch(InputMisMatchException ex)
System.out.println("An error ocurred");
end = false;
System.out.println("Try again");
input.nextLine();
}
}while(end == false);
This is a try-catch block. You need to use this if you want to be sure of not making the program-flow stop.
try {
input = user_input.nextInt();
}
catch (InputMismatchException exception) { //here you can catch that exception, so program will not stop
System.out.println("Integers only, please."); //this is a comment
scanner.nextLine(); //gives a possibility to try giving an input again
}
Test using hasNextInt().
Scanner user_input = new Scanner(System.in);
System.out.println("What is your age?");
if (user_input.hasNextInt()) {
int input = user_input.nextInt();
System.out.println("You are " + input + " years old");
} else {
System.out.println("You are a baby");
}
Use Scanner's next() method to get data instead of using nextInt(). Then parse it to integer using int input = Integer.parseInt(inputString);
parseInt() method throws NumberFormatException if it is not int, which you can handle accordingly.

Java scanner adding variable conditions

I'd like to ask how do i exactly condition what my program does if my user types in a character or a string if i want him to type an integer instead? I tried to do it how i showed here in quotes and also tried with "equals". The second method didn't work the first seems to be behaving strangely the IF part works but ELSE is completely ignored.
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter first integer: ");
int number1 = input.nextInt();// prompt
if (number1 == (char)number1){
System.out.println("Ok.");
}
else{
System.out.println("You were supposed to type in an int..");
System.exit(1);
}
System.out.print("Enter second integer: ");
int number2 = input.nextInt();// prompt
int sum =(number1 + number2);
System.out.printf("Your sum is: %d%n", sum);
}
I suggest you to use the regular expression in the hasNext() function as follows to have a finer control, for example use the following pattern if you look for the numbers,
sc.hasNext("[0-9]+")
Here is the documentation for the hasNext(String pattern) function,
public boolean hasNext(Pattern pattern)
Returns true if the next complete token matches the specified pattern. A complete token is prefixed and postfixed by input that matches the delimiter pattern. This method may block while waiting for input. The scanner does not advance past any input.
Here is the simple code to perform the check,
Scanner sc=new Scanner(System.in);
int input = 0;
while(true) {
System.out.println("enter a number");
if(sc.hasNext("[0-9]+")) {
input = sc.nextInt();
break;
} else {
System.out.println("not a number, try again");
sc.next(); // just consume, but ignore as its not a number
}
}
System.out.println("Entered number is : "+input);
You can use a user defined function as shown below and call it
public static boolean isNum(String input)
{
try
{
int d = Integer.parseInt(input);
}
catch(NumberFormatException e)
{
return false;
}
return true;
}
Then you can call this method from your main function.
if(isNum(number1))
I am not sure if I understand your question, but I see this as follows:
Users will always type a sequence of characters from the input, then your program has to check if that String can be converted to Int, if it can not be converted it should prompt back to the user telling the typed data is not an int. In that case your nextInt will throw an InputMismatchException.
Probably a much more elegant solution is to use hasNextInt(10):
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter first integer: ");
if (input.hasNextInt(10)){
System.out.println("Ok. Typed number: " + input.nextInt());
}else{
System.out.println("You were supposed to type in an int..");
System.exit(1);
}
[...]
}
Try this,
try {
int number1 = sc.nextInt();// prompt
System.out.println("Ok.");
} catch (InputMismatchException ex) {
System.out.println("You were supposed to type in an int..");
System.exit(1);
}
Scanner.nextInt(); Scans the next token of the input as an int.
Program won't execute beyond this line if input is not int.
So it will never enter else part. Don't do any int validation.
I would suggest always use try/catch block to handle incorrect input and show useful message. Also don't forget to close Scanner object.

Categories

Resources