logo

Metodo nextInt() dello scanner Java

IL nextInt() Il metodo della classe Java Scanner viene utilizzato per scansionare il token successivo dell'input come int. Esistono due diversi tipi di metodo Java nextInt() che possono essere differenziati in base al relativo parametro. Questi sono:

  • Metodo nextInt() dello scanner Java
  • Metodo nextInt(int radix) dello scanner Java

metodo nextInt()

Questo metodo della classe Java Scanner viene utilizzato per scansionare il token successivo dell'input come int.

metodo nextInt(int radix).

Questo è un metodo integrato della classe Java Scanner che viene utilizzato per scansionare il token successivo dell'input come int nella radice specificata.

Sintassi

Di seguito la dichiarazione di nextInt() metodo:

 public boolean nextInt() public boolean nextInt(int radix) 

Parametro

Tipo di dati Parametro Descrizione Obbligatorio/facoltativo
int radice Viene utilizzato per interpretare il token come valore int. Necessario

ritorna

IL nextInt() Il metodo restituisce il valore int scansionato dall'input.

Eccezioni

InputMismatchException - Questo metodo genera un'eccezione se il token successivo non corrisponde all'espressione regolare intera o è fuori intervallo

IllegalStateException - Genera questa eccezione se l'invocazione viene eseguita dopo che lo scanner è stato chiuso.

IllegalArgumentException - Genera questa eccezione se la radice specificata è fuori intervallo.

Versione compatibile

Java 1.5 e versioni successive

Esempio 1

 import java.util.*; public class ScannerNextIntExample1 { public static void main(String[] args) { String str = 'Facebook.com 13 + 3.0 = 16 true'; Scanner scanner = new Scanner(str); while (scanner.hasNext()) { //If the next is a int, print found and the int if (scanner.hasNextInt()) { System.out.println('Found Int Value: '+scanner.nextInt()); } //If no int is found, print 'Not Found:' and the token System.out.println('Not Found Int value: '+scanner.next()); } scanner.close(); } } 

Produzione:

 Not Found Int value: Facebook.com Found Int Value: 13 Not Found Int value: + Not Found Int value: 3.0 Not Found Int value: = Found Int Value: 16 Not Found Int value: true 

Esempio 2

 import java.util.*; public class ScannerNextIntExample2 { public static void main(String args[]){ int amount; int balance; //Insert amount and balance from console Scanner input = new Scanner (System.in); System.out.print('Enter the amount: '); amount = input.nextInt(); System.out.print('Enter the Total Balance: '); balance = input.nextInt(); //Reduce amount+fee from balance balance = balance-(amount + 500); //Print new balance System.out.print('New Balance is: '+balance); input.close(); } } 

Produzione:

 Enter the amount: 800 Enter the Total Balance: 1500 New Balance is: 200 

Esempio 3

 import java.util.*; public class ScannerNextIntExample3 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print('Number: '); int number = scan.nextInt(); System.out.print('String: '); String str = scan.next(); System.out.println('Output: '+number + ', ' + str); scan.close(); } } 

Produzione:

 Number: 12345 String: JavaTpoint Output: 12345, JavaTpoint 

Esempio 4

 import java.util.*; public class ScannerNextIntExample4 { public static void main(String[] args) { //Initialize the scanner Scanner scan = new Scanner('55 B3 -25 -7 1D'); while(scan.hasNext()){ //Printing int tokens of base 16 System.out.println(scan.nextInt(16)); } scan.close(); } } 

Produzione:

 85 179 -37 -7 29 

Esempio 5

 import java.util.*; public class ScannerNextIntExample5 { public static void main(String[] args) { String str = ' 11 Java 11 + 11 = 22.0'; Scanner scanner = new Scanner(str); while (scanner.hasNext()) { //If the next is int, print found and the int with radix if (scanner.hasNextInt()) { System.out.println('Found :' + scanner.nextLong(598670)); } //If int is not found, print 'Not Found' and the token System.out.println('Not Found :' + scanner.next()); } scanner.close(); } } 

Produzione:

 Exception in thread 'main' java.lang.IllegalArgumentException: radix:598670 at java.base/java.util.Scanner.setRadix(Scanner.java:1368) at java.base/java.util.Scanner.nextLong(Scanner.java:2370) at myPackage.ScannerNextIntExample5.main(ScannerNextIntExample5.java:10)