logo

Java Scanner nextInt() metoda

The nextInt() metoda klase Java Scanner koristi se za skeniranje sljedećeg tokena ulaza kao int. Postoje dvije različite vrste Java nextInt() metode koje se mogu razlikovati ovisno o parametru. Ovi su:

  • Java Scanner nextInt() metoda
  • Metoda Java Scanner nextInt(int radix).

nextInt() metoda

Ova metoda klase Java Scanner koristi se za skeniranje sljedećeg tokena ulaza kao int.

nextInt(int radix) Metoda

Ovo je ugrađena metoda klase Java Scanner koja se koristi za skeniranje sljedećeg tokena unosa kao int u navedenom radixu.

Sintaksa

Slijedi izjava od nextInt() metoda:

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

Parametar

Tip podataka Parametar Opis Obavezno/opcionalno
int radix Koristi se za tumačenje tokena kao int vrijednosti. Potreban

Povratak

The nextInt() metoda vraća int vrijednost skeniranu iz ulaza.

Iznimke

InputMismatchException - Ova metoda izbacuje iznimku ako se sljedeći token ne podudara s regularnim izrazom Integer ili je izvan raspona

IllegalStateException - Izbacuje ovu iznimku ako se pozivanje izvrši nakon što je skener zatvoren.

IllegalArgumentException - Izbacuje ovu iznimku ako je navedeni radix izvan raspona.

Verzija kompatibilnosti

Java 1.5 i novije

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

Izlaz:

 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 

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

Izlaz:

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

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

Izlaz:

 Number: 12345 String: JavaTpoint Output: 12345, JavaTpoint 

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

Izlaz:

 85 179 -37 -7 29 

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

Izlaz:

 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)