How would I write this C++ input function in Java? [duplicate] - java

This question already has answers here:
How to get the user input in Java?
(29 answers)
Closed 2 years ago.
I am trying to figure out how to read input from the console in Java that would behave exactly the same as the following C++ code:
while(cin >> input)
I essentially need to keep reading the console one integer at a time, and only stop when the user enters no more integers.I am able to read integers one at a time, but cannot figure out how to get it to stop executing once the user passes an empty line. Thanks!

Scanner scanner = new Scanner(System.in);
// find the next int token and print it
// loop for the whole scanner
while (scanner.hasNext()) {
// if the next is a int, print found and the int
if (scanner.hasNextInt()) {
System.out.println("Found :" + scanner.nextInt());
}
// if no int is found, print "Not Found:" and the token
System.out.println("Not Found :" + scanner.next());
}

You can use the Scanner nextLine() method and check for integers:
import java.util.*;
class TextNum {
public static void main(String[] args) {
int n;
String s;
Scanner in = new Scanner(System.in);
while (true) {
s = in.nextLine();
//Do something with this
System.out.println("This is :" + s + ":");
try {
n = Integer.parseInt(s);
} catch (Exception e) {
break;
}
}
}
}

Related

Proper way to use Scanner with multiple functions? [closed]

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 2 years ago.
Improve this question
I have a console application with a main method and some methods that I call from main. There, I want to ask the user for some input, for which I use the Scanner class.
Here's my problem:
I find there is no way to use Scanner when reading inputs from outside main without random exceptions or unexpected behaviour. I have tried two approaches:
Having a Scanner global variable in the class containing main. Then
I use this same Scanner in all functions in that same class.
In every function I need to ask for input, I declare a new Scanner
variable, use it, and close it before exiting the function.
1. makes Scanner try to read twice. I mean, I have a sc.readLine in a function and, when I exit that function, I have another sc.readLine in main. I input once and the two readLine lines get executed, the second one reading an empty String.
2. throws Exception (base class Exception) when I call any sc.readLine for a second time during the execution of the program.
I have also noticed that any other method other than readLine is going to read various items on the same line. For example, line "10 20 30 40" would execute 4 sc.nextInt calls.
TL;DR: how do you use Scanner in a console application?
One way:
import java.util.Scanner;
public class Main {
private Scanner scanner = new Scanner(System.in);
public Scanner getScanner() {
return scanner;
}
void fun1() {
Scanner in = getScanner();
System.out.print("Enter a string: ");
System.out.println("You entered: " + in.nextLine());
}
void fun2() {
Scanner in = getScanner();
System.out.print("Enter an integer: ");
int n = 0;
try {
n = Integer.parseInt(in.nextLine());
} catch (NumberFormatException e) {
e.printStackTrace();
}
System.out.println(n + " + 10 = " + (n + 10));
}
public static void main(String[] args) {
Main m = new Main();
m.fun1();
m.fun2();
}
}
A sample run:
Enter a string: Hello world!
You entered: Hello world!
Enter an integer: 25
25 + 10 = 35
Another way:
import java.util.Scanner;
public class Main {
static void fun1(Scanner in) {
System.out.print("Enter a string: ");
System.out.println("You entered: " + in.nextLine());
}
static void fun2(Scanner in) {
System.out.print("Enter an integer: ");
int n = 0;
try {
n = Integer.parseInt(in.nextLine());
} catch (NumberFormatException e) {
e.printStackTrace();
}
System.out.println(n + " + 10 = " + (n + 10));
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
fun1(in);
fun2(in);
}
}
A sample run:
Enter a string: Hello world!
You entered: Hello world!
Enter an integer: 25
25 + 10 = 35
Regarding your problem with next() or nextInt(): Given below is the recommended way for multiple inputs in one go.
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean valid = true;
System.out.print("Enter some intgers: ");
String strNum = in.nextLine();
String[] strNumArr = strNum.split("\\s+");
int[] numArr = new int[strNumArr.length];
for (int i = 0; i < strNumArr.length; i++) {
try {
numArr[i] = Integer.parseInt(strNumArr[i]);
} catch (NumberFormatException e) {
numArr[i] = Integer.MIN_VALUE;
valid = false;
}
}
System.out.println(Arrays.toString(numArr));
if (!valid) {
System.out.println("Note: invalid inputs have been reset to " + Integer.MIN_VALUE);
}
}
}
A sample run:
Enter some intgers: 10 5 20 15
[10, 5, 20, 15]
Another sample run:
Enter some intgers: 4 40 a 20 b 15
[4, 40, -2147483648, 20, -2147483648, 15]
Note: invalid inputs have been reset to -2147483648
Check Scanner is skipping nextLine() after using next() or nextFoo()? for more information about console input using Scanner.
to use a single instance of Scanner (or any other datatype or class), you can use the design pattern "Singleton" which consist to instantiate a single instance of your Object for the whole project.
The Singleton definition (a new class) :
import java.util.Scanner;
public class ScannerSingleton {
private static Scanner sc = null;
private ScannerSingleton() {
sc = new Scanner(System.in);
}
public static Scanner getInstance() {
if (sc == null) {
synchronized(ScannerSingleton.class) {
if (sc == null)
sc = new ScannerSingleton();
}
}
}
}
and each time you want to use your scanner anywhere you only have to call ScannerSingleton.getInstance() which will return your single instance of scanner
example of use:
String test = ScannerSingleton.getInstance().nextLine();
I suggest to pass your Scanner object as another parameter for your functions.
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
String answer = ask(scanner, "what is your name?");
System.out.println("Oh! Hello dear " + answer + "!");
scanner.close();
}
private static String ask(Scanner scanner, String question)
{
System.out.println(question);
return scanner.nextLine();
}

NoSuchElementException, when method is called after code above it is run [duplicate]

This question already has answers here:
Scanner close after use
(2 answers)
Closed 3 years ago.
When I run my code, it will run fine up to the line "scanner.close()".
After than, when I run the "SumTenNumbers()" method... it will run the first line of the while loop once and crash with the "NoSuchElementException"...
When I remove the code above the line calling the method, it runs fine...
Why does this occur, and how can I solve it?
This is the code:
public class Main
{
public static void main(String[] args)
{
// we use the class "scanner" for input data
Scanner scanner = new Scanner(System.in); //System.in allows you to type input data into the console which can be returned into the console
System.out.println("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Your name is " + name);
System.out.println("Enter your year of birth: ");
boolean isInt = scanner.hasNextInt();
if (isInt)
{
int yearOfBirth = scanner.nextInt();
int age = 2019 - yearOfBirth;
if (age >= 0 && age <= 120)
{
System.out.println("You are " + age + " years old");
}
else
{
System.out.println("Invalid year of birth");
}
}
else
{
System.out.println("Unable to parse year of birth");
}
scanner.close(); // we must close scanner
SumTenNumbers();
}
public static void SumTenNumbers()
{
var reader = new Scanner(System.in);
int sum = 0;
int count = 1;
while (count < 11)
{
System.out.println("Enter number " + count + ": ");
boolean valid = reader.hasNextInt();
if (valid)
{
int userNum = reader.nextInt();
sum += userNum;
count++;
}
else
{
reader.next();
System.out.println("INVALID");
}
}
System.out.println(sum);
reader.close();
}
}
This is how it looks when I run the code...
Enter your name:
Siddharth
Your name is Siddharth
Enter your year of birth:
2001
You are 18 years old
Enter number 1:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at com.company.Main.SumTenNumbers(Main.java:64)
at com.company.Main.main(Main.java:39)
Process finished with exit code 1
the line reader.close() closes the Scanner and, with it, System.in.
After that, you cannot read from stdin (System.in) anymore.
In order to prevent this you can:
use only one Scanner object and close it after using it the last time
close System.in after using it the last time (using a Scanner or System.in.close())
close System.in at the end of your Program
never close System.in (Problem: other Processes cannot use the resource, stdin can also be a File, a Network Connection or something else)

Trying to get fresh user input when user's initial input is invalid. But scanner object continues to use first value [duplicate]

This question already has answers here:
How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner
(5 answers)
Closed 6 years ago.
First of all, apologies for the title gore.
The specific thing I am trying to understand here in the following piece of code is why the getNumber function, when called the second time, continues to return the same initial user input and doesn't ask for a new user input.
/*Write an application that inputs one number consisting
of five digits from the user, separates the number into its individual digits and prints the digits
separated from one another by three spaces each.
*/
public class SeparatingDigitsOfInt {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
boolean check =false;
String s=null;
while (check==false)
{
s= Integer.toString(getNumber()); //since user input was a string, getNumber returns 0 and we enter the while loop below
while (s.equals("0"))// here i am trying to get another input from user because first input was invalid
{
System.out.println("Try that again!");
s= Integer.toString(getNumber()); // why getNumber continues to return xyz here and doesn't ask for new user input
check =false;
}
check =true;
}
System.out.println(s);
while(s.length()!= 5){
System.out.println("Input number is not of 5 digits!");
System.out.println("Please enter a 5 digit number");
s = input.next();
}
String result = "";
for (int i = 0; i < s.length(); i++) {
result= result + s.charAt(i) + " ";
}
System.out.println("Result is :" + result);
}
public static int getNumber(){
try {
System.out.println("Enter a 5 digit number");
return input.nextInt(); // user inputs string xyz
}
catch (InputMismatchException e) {
System.out.println("Please enter only numbers");
return 0;// since user inputs xyz, so 0 is returned by getNumber
}
}
}
In documentation stated :
If the translation is successful, the scanner advances past the input
that matched
So that means if it translation was not successful, it won't advance

Enter a string to integer scanner [duplicate]

This question already has answers here:
Validating input using java.util.Scanner [duplicate]
(6 answers)
Closed 6 years ago.
I want to say enter an integer when someone trying to enter a string in this code.
Can you help me?
Here is my code:
import java.util.Scanner;
public class kl {
public static void main(String[] args) {
boolean primen = true;
Scanner input = new Scanner(System.in);
System.out.print("Please enter a positive integer that is prime or not : ");
int ncheck = input.nextInt();
if (ncheck < 2) {
primen = false;
}
for (int i = 2; i < ncheck; i++) {
if (ncheck % i == 0) {
primen = false;
break;
}
}
if (primen == true) {
System.out.println(ncheck + " is a prime number.");
}
else {
System.out.println(ncheck + " is not a prime number.");
}
}
}
You can find your solution here: Exception handling, Or use codes below
Here is your complete code:
while(true){
System.out.print("Please enter a positive integer that is prime or not : ");
try{
int i = input.nextInt();
break;
}catch(InputMismatchException e){
System.out.print("Wrong type input, pls try again!");
input.nextLine(); \\ prevent infinite loop
}
You can see: I use a Exception handle processor to catch the InputMismatchException and print on console the message. You can replace InputMismatchException by Exception. It's largest Exception Handler class in java
There are two approaches you can use with Scanner.
Call nextInt() and then catch and handle the InputMismatchException that you will get if the next input token isn't an integer.
Call hasNextInt(). If that returns true then call nextInt().
In either case, if you expected an integer and the user entered something else, then neither nextInt() or hasNextInt() will "consume" the unexpected characters. So if you want the user to try try again, you need to call nextLine() which will read all remaining characters on the line. You will typically discard them.
For more information on handling exceptions:
https://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html
For more information on using Scanner:
https://docs.oracle.com/javase/tutorial/essential/io/scanning.html
http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

program crashes halfway through, no idea why

heres what i got, take a look and see what you can find. eclipse says everything is good to go, but when i run it, i get to input the 5 numbers, then it asks the two lines about highest to lowest or vice versa, then crash before i can put my answer in. im at a loss.
these are the errorrs i see. Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at monty.intarray.main(intarray.java:25)
import java.util.Arrays;
import java.util.Scanner;
public class intarray {
public static void main(String[] args) {
System.out.println("Enter a number other than zero, then hit enter. Do this five times.");
Scanner input1 = new Scanner(System.in);
int[] array=new int[5];
for (int whatever = 0; whatever < array.length;whatever++)
array[whatever]=input1.nextInt();
input1.close();
System.out.println("Now tell me if you want to see those numbers sorted from lowest to highest, or highest to lowest.");
System.out.println("Use the command 'lowest' without the single quotes or 'highest'.");
Scanner input2 = new Scanner (System.in);
String answer = input2.next();
input2.close();
boolean finish;
finish = loworhigh(answer);
if (finish) {
Arrays.sort(array);
for (int a = array.length - 1; a >= 0; a--) {
System.out.print(array[a] + " ");
}
}
else {
Arrays.sort(array);
for (int b=0; b<=array.length; b++) {
System.out.print(array[b] + " ");
}
}
System.out.print(array[0] + ", ");
System.out.print(array[1] + ", ");
System.out.print(array[2] + ", ");
System.out.print(array[3] + ", ");
System.out.print(array[4] + ".");
}
public static boolean loworhigh(String ans) {
if (ans.equalsIgnoreCase("lowest")) return false;
else return true;
}
}
When you call on for input1.close, it also closes System.In input stream along with the scanner.
To check if Scanner is still available you can try:
System.out.println(System.in.available());
And there is no way to re-open System.In
public void close() throws IOException --> Closes this input stream and releases any system resources associated with this stream. The general contract of close is that it closes the input stream. A closed stream cannot perform input operations and **cannot be reopened.**
And thus it throws NoSuchElementException
To avoid this, don't close input1 and instead use the same Scanner object and accept as many inputs as required and finally close the Scanner.
Hope this helps.

Categories

Resources