why i am getting java.util.NoSuchElementException error in Scanner [duplicate] - java

This question already has answers here:
Java Multiple Scanners
(3 answers)
Closed 3 years ago.
I am trying to read input in main function as well as different functions, for that, I am creating two different Scanner and it is giving me an error but if I take input only in main function and pass the value in the different function it does not gives error then, what I am missing here?. I am a beginner in java.
import java.util.Scanner;
public class abc{
public static void fun(){
Scanner read = new Scanner(System.in);
int a,b,c;
a=read.nextInt();
b=read.nextInt();
c=read.nextInt();
// some code
}
public static void main(String[] args){
Scanner read=new Scanner(System.in);
int t=read.nextInt();
while(t>0){
fun();
t--;
}
}
}

Why do you just don't do like this:-
import java.util.Scanner;
public class abc{
public static void fun(int a, int b, int c){
Scanner read = new Scanner(System.in);
// some code
}
public static void main(String[] args){
Scanner read=new Scanner(System.in);
int t=read.nextInt();
Object o = new Object();
int a, b, c;
while(t>0){
a=read.nextInt();
b=read.nextInt();
c=read.nextInt();
ob.fun(a, b, c);
t--;
}
}
}
This will probably give you no error. If you don't want to do this, you can simply declare the Scanner object in the class and not in a method and try to use it.

I do not think it correct to create two Scanner for the same System.in. Why don't use the single one:
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
int t = scan.nextInt();
while (t > 0) {
fun(scan);
t--;
}
}
}
public static void fun(Scanner scan) {
int a, b, c;
a = scan.nextInt();
b = scan.nextInt();
c = scan.nextInt();
// some code
}

Related

Scanner as parameter and convert to int? (JAVA)

I have a trouble about usage of the scanner.
The getUserInput takes as an input the scanner instance and initialise the array of specified size which comes from the scanner. For example: if user puts 3 then the method will create an array of the size 3.
However, it keeps saying that scnr can't converted to int....
Any advice?
public static void main(String[] args)
{
Scanner scnr = new Scanner(System.in);
System.out.println("How many orders were placed Saturday?");
int [] userInput = getUserInput(scnr);
System.out.println("How many orders were placed Sunday?");
int [] userInput = getUserInput(scnr);
System.out.println("How many orders were placed Monday?");
int [] userInput = getUserInput(scnr)
return;
}
}
public static int[] getUserInput(Scanner scnr)
{
int[] userInput = new int[scnr];
return userInput;
}
You have to call a method on Scanner. And because you want an int do it like this:
int[] userInput = new int[scnr.nextInt()];
Here you'll find the API documentation: https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
nextInt() method of Java Scanner class is used to scan the next token of the input as an int
public static int[] getUserInput(Scanner scnr)
{
int[] userInput = new int[scnr.nextInt()];
return userInput;
}
Firstly there are 2 mistakes you have done here.
int [] userInput there are 2 duplicates of this userInput variable.
You are passing an instance of Scanner as a parameter in your getUserInput function.
getUserInput FIX
So in order to create we need to follow a syntax i.e
datatype [] vairable_name = new datatype[size];
Here size can be byte short int but what you did is
datatype [] vairable_name = new datatype[Scanner];
Which is just not the Syntax. You can fix it by taking input as int from the user.
A scanner as a method to do that .nextInt() which converts the input of the user to int if it's in numbers.
Solution is
int[] userInput = new int[scnr.nextInt()];
// This is like the syntax
datatype [] vairable_name = new datatype[Scanner];
Duplicate Variable
You should revise scopes in java, and then you will know why it's an error.
You cannot have the same variable name in the same scope or in the child scope
public static void main(String args[]){
int a = 0;
int a = 1; // <- error cuz a is already difined and is duplicate in the same scope.
}
Similarly
public static void main(String args[]){
int a = 0;
if(true){
int a = 1; // <- error cuz a is already difined and is duplicate in child scope.
}
}
The solution is to change the variable name.
//scnr is now globally available in the class so need to pass it as a parameter
Scanner scnr = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("How many orders were placed Saturday?");
int [] saturdayUserInput = getUserInput();
System.out.println("How many orders were placed Sunday?");
int [] sundayUserInput = getUserInput();
System.out.println("How many orders were placed Monday?");
int [] mondayUserInput = getUserInput()
return;
}
}
public static int[] getUserInput()
{
int[] userInput = new int[scnr.nextInt()];
return userInput;
}
scnr is not an int. It's an isntance of the Scanner class. If you want to read an int from it, you'll need to call:
scnr.nextInt();
It's also better not to pass this as a parameter, but to create the Scanner on class level, so you can use it in all the methods.

Unable to resolve NoSuchElementException

I am getting NoSuchElementException while executing my code on various online IDE. I am taking STDIN using Scanner object.
import java.util.*;
import java.util.Scanner;
class Maze{
public static int numberLength(int k){
int ln = (int) (Math.log10(k) + 1);
return ln;
}
public static void main(String [] args){
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int s = numberLength(n);
System.out.println(s);
}
}
You have to check Custom input and write your input, then compile
You have got to give inputs for your primitives which you have declared in the main.

Java - how to use variables in static methods [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 4 years ago.
I can't figure out how to use variables when methods are static (which "main" has to be?). Here is an example:
import java.util.Scanner;
public class Yatzy {
Scanner input = new Scanner(System.in);
protected static int reply;
public static void main(String[] args){
startGame();
}
public static void startGame(){
System.out.println("How many players? 1 - 3.");
reply = input.nextInt(); // Option 1: class variable
int userinput = input.nextInt(); // Option 2: instance variable
}
}
No matter which way I try, I keep getting this error:
Error: non-static variable input cannot be referenced from a static context
How can I use user inputs (or more generally, variables) within static methods?
The error message is clear, and this is a fundamental as well as a very basic concept in java. You are trying to refer to a variable within the static area, so you should create that variable in the static area.
static Scanner input = new Scanner(System.in);
will do the trick.
Using Static variables and methods
import java.util.Scanner;
public class Yatzy {
static Scanner input = new Scanner(System.in);
static int reply;
public static void main(String[] args){
startGame();
}
public static void startGame(){
System.out.println("How many players? 1 - 3.");
reply = input.nextInt();
int userinput = input.nextInt();
}
}
In the above example, all methods and variables are in the static area.
Using Instance variables and methods
import java.util.Scanner;
public class Yatzy {
Scanner input = new Scanner(System.in);
int reply;
public static void main(String[] args){
new Yatzy().startGame();
}
public void startGame(){
System.out.println("How many players? 1 - 3.");
reply = input.nextInt();
int userinput = input.nextInt();
}
}
Create a new object of your class in the main method and call startGame().

How to console input from a class in java like that of cpp

How to console input data in java class(not main class). The input methods work in cpp but not in java. Is there a way to console from a class method. System.in.read and interger parse methods are not working.
Use Scanner with System.in
Scanner input = new Scanner(System.in);
int x = input.nextInt();
String t = input.next();
like wise
Try following code
import java.util.Scanner;
public class Demo {
public static void main (String [] args){
A a = new A();
a.getValue();
}
}
class A {
public void getValue(){
Scanner input = new Scanner(System.in);
System.out.println("Enter number : ");
int x = input.nextInt();
System.out.println(x);
}
}

error: non-static variable scan cannot be referenced from a static context in Java

import java.util.Scanner;
public class CHP4Ex
{
Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("enter a n: ");
int n = scan.nextInt();
int i=10;
while (i<n)
{
System.out.println(i);
i = i + 10;
}
}
}
Why am I getting this error? I'm basically writing a while loop that prints all positive numbers that are divisible by 10 and less than n. For example, if n is 100, enter 10 ... 90.
Put the Scanner class object inside the main function. Basically the problem is that your code violates the static feature. You cannot use non-static members inside a static function, main being static in your case. So it should be :
import java.util.Scanner;
public class CHP4Ex
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("enter a n: ");
int n = scan.nextInt();
int i=10;
while (i<n)
{
System.out.println(i);
i = i + 10;
}
}
}
You can't refer to non static variable in static context, so change
Scanner scan = new Scanner(System.in);
to
private static Scanner scan = new Scanner(System.in); It should work

Categories