ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/JAVA

[JAVA] hasNextInt() ๋ฉ”์†Œ๋“œ

NaNaRin๐Ÿ™ƒ 2021. 1. 7. 22:20

docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/Scanner.html#hasNextInt()

 

Scanner (Java SE 15 & JDK 15)

All Implemented Interfaces: Closeable, AutoCloseable, Iterator public final class Scanner extends Object implements Iterator , Closeable A simple text scanner which can parse primitive types and strings using regular expressions. A Scanner breaks its input

docs.oracle.com


java.util.Scanner.hasNextInt()

 

public boolean hasNextInt()

 

๋ฐ˜ํ™˜๊ฐ’ :

Scanner ๊ฐ์ฒด์— ์ž…๋ ฅ๊ฐ’์ด int๊ฐ’์ผ ๋•Œ๋งŒ true๋ฅผ ๋ฐ˜ํ™˜

 

Scanner.hasNextInt()


์˜ˆ์ œ

import java.util.Scanner;

public class HasNextIntEx {

	public static void main(String[] args) {
				
		Scanner sc = new Scanner(System.in);
			
		while(sc.hasNextInt()) {
			System.out.print("์ž…๋ ฅ๊ฐ’์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•ฉ : ");
			int a = sc.nextInt();
			int b = sc.nextInt();
			System.out.println(a+b);
		}

	}

}

 

์ถœ๋ ฅ

3 34
์ž…๋ ฅ๊ฐ’์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•ฉ : 37
234 1
์ž…๋ ฅ๊ฐ’์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•ฉ : 235

=> ์ž…๋ ฅ๊ฐ’์ด ๋“ค์–ด์˜ฌ ๋•Œ ๊นŒ์ง€ while๋ฌธ ๋‚ด๋ถ€๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋‹ค๊ฐ€

     ์ž…๋ ฅ๊ฐ’์ด ๋“ค์–ด์™€ sc.hasNextInt()๊ฐ€ true๋ฅผ ๋ฆฌํ„ดํ•œ ์ดํ›„์— while๋ฌธ ๋‚ด๋ถ€๊ฐ€ ์‹คํ–‰๋จ

 

 

์‚ฌ์šฉ ์˜ˆ : nanarin.tistory.com/28