Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- checked exception
- 오마이포쉬
- 랜덤 테마
- ControllerAdvice
- runtimeexception
- Spring AOP
- undeclaredthrowableexception
- ohmyposh
- Custom Exception
- PowerShell
- Theme
- exception
Link
- Today
- Total
글
[Java] Scanner nextInt() + nextLine() 본문
문제
- 백준 10828번 풀이 중 StirngTokenizer.nextToken 에서 NoSuchElementException 발생
public class P10828 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StackIpl stk = new StackIpl();
int n = sc.nextInt();
sc.nextLine(); // 해결
for (int i = 0; i < n; i++){
StringTokenizer st = new StringTokenizer(sc.nextLine());
String cmd = st.nextToken();
해결
- Scanner.nextInt() 후 버퍼에 개행문자 \n이 남아있어 nextLine()에 입력되어 의도하지 않은 결과가 나타남.
-> 중간에 nextLine()을 추가하여 버퍼를 비워줌
'Troubleshooting' 카테고리의 다른 글
UndeclaredThrowableException?? (2) (0) | 2024.06.02 |
---|---|
UndeclaredThrowableException?? (1) (0) | 2024.06.02 |