logo

Java-Scanner nextInt()-Methode

Der nextInt() Die Methode der Java-Scanner-Klasse wird verwendet, um das nächste Token der Eingabe als int zu scannen. Es gibt zwei verschiedene Arten von Java-Methoden nextInt(), die je nach Parameter unterschieden werden können. Diese sind:

  • Java-Scanner nextInt()-Methode
  • Java Scanner nextInt(int radix) Methode

nextInt()-Methode

Diese Java-Scanner-Klassenmethode wird verwendet, um das nächste Token der Eingabe als int zu scannen.

nextInt(int radix) Methode

Dies ist eine integrierte Methode der Java-Scanner-Klasse, die verwendet wird, um das nächste Token der Eingabe als int in der angegebenen Basis zu scannen.

Syntax

Es folgt die Erklärung von nextInt() Methode:

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

Parameter

Datentyp Parameter Beschreibung Erforderlich/Optional
int Wurzel Es wird verwendet, um das Token als int-Wert zu interpretieren. Erforderlich

Kehrt zurück

Der nextInt() Die Methode gibt den von der Eingabe gescannten int-Wert zurück.

Ausnahmen

InputMismatchException – Diese Methode löst eine Ausnahme aus, wenn das nächste Token nicht mit dem regulären Ganzzahlausdruck übereinstimmt oder außerhalb des Bereichs liegt

Illegale staatliche Ausnahme – Diese Ausnahme wird ausgelöst, wenn der Aufruf erfolgt, nachdem der Scanner geschlossen wurde.

IllegalArgumentException – Diese Ausnahme wird ausgelöst, wenn die angegebene Basis außerhalb des gültigen Bereichs liegt.

Kompatibilitätsversion

Java 1.5 und höher

Beispiel 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(); } } 

Ausgabe:

 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 

Beispiel 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(); } } 

Ausgabe:

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

Beispiel 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(); } } 

Ausgabe:

 Number: 12345 String: JavaTpoint Output: 12345, JavaTpoint 

Beispiel 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(); } } 

Ausgabe:

 85 179 -37 -7 29 

Beispiel 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(); } } 

Ausgabe:

 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)