logo

Metoda Java Scanner nextInt().

The nextInt() metoda razreda Java Scanner se uporablja za skeniranje naslednjega žetona vnosa kot int. Obstajata dve različni vrsti metode Java nextInt(), ki ju je mogoče razlikovati glede na njen parameter. To so:

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

metoda nextInt().

Ta metoda razreda Java Scanner se uporablja za skeniranje naslednjega žetona vnosa kot int.

nextInt(int radix) Metoda

To je vgrajena metoda razreda Java Scanner, ki se uporablja za skeniranje naslednjega žetona vnosa kot int v podanem korenu.

Sintaksa

Sledi izjava nextInt() metoda:

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

Parameter

DataType Parameter Opis Obvezno/izbirno
int radix Uporablja se za interpretacijo žetona kot int vrednosti. Obvezno

Vračila

The nextInt() metoda vrne vrednost int, skenirano iz vnosa.

Izjeme

InputMismatchException - Ta metoda vrže izjemo, če se naslednji žeton ne ujema z regularnim izrazom Integer ali je izven obsega

IllegalStateException - To izjemo sproži, če se priklic izvede po zaprtju optičnega bralnika.

IllegalArgumentException - To izjemo sproži, če je podani radiks izven obsega.

Združljivostna različica

Java 1.5 in novejši

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

Izhod:

 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 

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

Izhod:

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

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

Izhod:

 Number: 12345 String: JavaTpoint Output: 12345, JavaTpoint 

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

Izhod:

 85 179 -37 -7 29 

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

Izhod:

 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)