티스토리 뷰
A user types 'integer value' and 'Enter', but nextInt() does not use the end of the input, which is 'Enter' so that it leaves in the buffer. If nextLine() is used after nextInt(), the remainder causes an error. Because nextLine() is the method that reads data until the 'Enter' input. To remove the last input(Enter), simply use nextLine() to terminate the line.
nextInt()메서드 다음에 nextLine()메서드가 온다면 Enter키가 input으로 들어올 때 까지 데이터를 읽기 때문에 nextInt()의 input data 다음의 enter 까지 읽어들이게 된다. 자연스럽게 그 다음 nextLine() 메서드는 실행이 되어지지 않고 종료되게 된다. 그런 결과를 대비하기 위해 꼭 enter input을 비우기위한 nextLine()을 nextInt()다음에 써주어야한다. nextInt()다음에 다른 nextInt()가 사용된다면 상관없다.
int select = MenuViewer.keyboard.nextInt();
MenuViewer.keyboard.nextLine(); //nextInt 다음에 nextLine 으로 delimiter 지우기.
|
'JAVA' 카테고리의 다른 글
Overriding hasCode() and equals() in Java (0) | 2019.07.11 |
---|---|
Singleton pattern (Java design pattern) (0) | 2019.07.10 |
Write to file in Java - double output using BufferedWriter (0) | 2019.07.08 |
File I/O - Decorator pattern, File.separator (0) | 2019.07.05 |
File I/O - loading data and creating txt file from CSV file. (0) | 2019.07.05 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Collection Framework
- hackerrank javascript
- Javascript Algorithm
- 프로그래머스 알고리즘
- compareTo()
- HackerRank Algorithm
- javascript
- equals()
- java
- string class in java
- spring boot application
- repeat()
- math.abs
- easy javascript algorithm
- substring()
- math.max
- 알고리즘
- hackerrank javascript solution
- algorithm
- code refactoring
- rest parameter
- Object type casting
- easy algorithm
- ... in Javascript
- hackerrank solution
- hackerrank
- 프로그래머스 알고리즘문제
- C++
- HashMap
- 프로그래머스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함