logo

Java Scanner close() metóda

The Zavrieť() je metóda Java Scanner triedy, ktorá sa používa na zatvorenie tohto skenera.

Syntax

Nasleduje vyhlásenie o Zavrieť() metóda:

 public void close() 

Parameter

Táto metóda neakceptuje žiadny parameter.

Návraty

The Zavrieť() metóda nevracia žiadnu hodnotu.

preťaženie metódy

Výnimky

TO

Verzia kompatibility

Java 1.5 a vyššie

Príklad 1

 import java.util.Scanner; public class ScannerCloseExample1{ public static void main(String args[]){ String s = 'Hi All! This is JavaTpoint.'; //Create a scanner with the specified Object Scanner scanner = new Scanner(s); System.out.println('' + scanner.nextLine()); //Close the scanner System.out.println('Closing Scanner...'); scanner.close(); System.out.println('Scanner Closed.'); } } 

Výkon:

 Hi All! This is JavaTpoint. Closing Scanner... Scanner Closed. 

Príklad 2

 import java.util.Scanner; public class ScannerCloseExample2{ public static void main(String args[]){ System.out.print('Enter Your Name: '); //Create a scanner with the specified Object Scanner scanner = new Scanner(System.in); String name = scanner.next(); System.out.println('Name: '+name); //Close the scanner scanner.close(); System.out.println('Scanner Closed.'); } } 

Výkon:

reťazec na dlhý
 Enter Your Name: JavaTpoint Name: JavaTpoint Scanner Closed. 

Príklad 3

 import java.util.Scanner; public class ScannerCloseExample3 { public static void main(String args[]){ System.out.println('Throws Exception If Number is of Type Long.'); Scanner scanner = new Scanner(System.in); System.out.print('Enter your rollno: '); int rollno = scanner.nextInt(); System.out.println('RollNumber: '+rollno); //Close the scanner scanner.close(); System.out.println('Scanner Closed.'); } } 

Výkon:

 Throws Exception If Number is of Type Long. Enter your rollno: 345643985649356 Exception in thread 'main' java.util.InputMismatchException: For input string: '345643985649356' at java.base/java.util.Scanner.nextInt(Scanner.java:2264) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at myPackage.ScannerCloseExample3.main(ScannerCloseExample3.java:9)