Login using scanner class in java

I made a program to reduce given integers to their simplest relation. But an error occurs when entering data through the Scanner class in a sub-method of the program. Here is the code:

package CodeMania; import java.util.Scanner; public class Question5 { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int T=sc.nextInt();// number of test cases sc.close(); if(T<1) { System.out.println("Out of range"); System.exit(0); } for(int i=0;i<T;i++) { ratio();//line 19 } } static void ratio() { Scanner sc1=new Scanner(System.in); int N=sc1.nextInt();//line 26 if((N>500)||(N<1)) { System.out.println("Out of range"); System.exit(0); } int a[]=new int[N]; for(int i=0;i<N;i++) { a[i]=sc1.nextInt(); } int result = a[0]; for(int i = 1; i < a.length; i++) { result = gcd(result, a[i]); } for(int i=0;i<N;i++) { System.out.print((a[i]/result)+" "); } sc1.close(); } static int gcd(int a, int b) { while (b > 0) { int temp = b; b = a % b; a = temp; } return a; } } 

Error -

 Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at CodeMania.Question5.ratio(Question5.java:26) at CodeMania.Question5.main(Question5.java:19) 

Here I used 2 separate sc scanner objects in the main function and the sc1 function in the ratio for inputting input from the console. However, if I declare an open Scanner object of a public static type in the scope of the class, and then using only one Scanner object in the entire program to enter input, the program works as needed without errors.

Why is this happening...?

+5
source share
4 answers

The reason for this error is that calling .close () on the scanner also closes inputStream System.in , but creating a new scanner will not reopen it.

You need to either pass one scanner to the parameters of your method, or make it a static global variable.

+5
source

Since your main() method and your ratio() use scanners, they throw exceptions when an exception occurs, the normal program flow is interrupted, and the program / application is interrupted abnormally, which is not recommended, therefore, these exceptions are handled. An exception can occur for various reasons, the following are some scenarios in which an exception occurs.

 A user has entered invalid data. A file that needs to be opened cannot be found. A network connection has been lost in the middle of communications or the JVM has run out of memory. 

You can handle these exceptions using Try / Catch blocks, or you can handle them with the word throws after defining a method, in your case these two approaches would be:

With Try / Catch :

  public static void main() { try{ Scanner sc=new Scanner(System.in); int T=sc.nextInt();// number of test cases sc.close(); } catch(NoSuchElementException e){ System.out.print("Exception handled" + e); //rest of method } static void ratio(){ try{ Scanner sc1=new Scanner(System.in); int N=sc1.nextInt();} catch(NoSuchElementException e){ System.out.print("Exception handled" + e);} //rest of method } 

With "throws":

  public static void main()throws Exception{ //rest of method } static void ratio()throws Exception { //rest of method } 
0
source

Try it. You can pass the scanner as an argument

 package stack.examples; import java.util.Scanner; public class Question5 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int T = sc.nextInt();// number of test cases if (T < 1) { System.out.println("Out of range"); System.exit(0); } for (int i = 0; i < T; i++) { ratio(sc);// line 19 } sc.close(); } static void ratio(Scanner sc1) { int N = sc1.nextInt();// line 26 //Your Logic } static int gcd(int a, int b) { while (b > 0) { int temp = b; b = a % b; a = temp; } return a; } 

}

0
source
 import java.util.*; public class Understanding_Scanner { public static void main() { Scanner sc= new Scanner(System.in); System.out.println("Please enter your name"); String name=sc.next(); System.out.println("Your name is:"+name); } } 

Now, to explain this, we need to import the scanner class from the Java Utility package, so that this can be achieved using the code on the first line, the second line creates the class. NOTE (CLASS NAME DOES NOT START WITH CAPITAL) now approaches the main theme of the Scanner class, therefore for of this, we must create a scanner class inside the program with the code that was specified on the 4th line ... in this expression, โ€œscโ€ is the object in which the values โ€‹โ€‹of the scanner class are stored, so if you want to perform some operation in scanner class you can do it through the sc object * NOTE (you can name the ur object like something like: poop, bla, etc.) ... then we have this interesting command, which says that System.in now allows users to write any operator through the keyboard or any such input devices at run time ... The name = sc.next () line this line helps us write any line that we want to write at run time, which will be stored in the name variable

So this is the scanner class for u. Hope it's easy to understand.

Hooray!! Continue coding :-)

0
source

Source: https://habr.com/ru/post/1239414/


All Articles