Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- Linux
- 백준
- 10844
- 10951
- setParameter
- 숫자야구
- 자료구조
- 디자인패턴
- 쉬운 계단 수
- 점세개
- Spring
- designpattern
- @NotEmpty
- NamedParameterNotBound
- C
- 여러인수
- createQuery
- decorator
- pscp
- springboot
- java
- mycp
- BubbleSorting
- @Spring
- gradle
- 자바
- junit
- @ModelAttribute
- 데코레이터패턴
- 전치행렬 #C
Archives
- Today
- Total
...
[JAVA] Scanner의 next() / nextLine() 차이점 본문
next()
-> '토큰'의 개념으로서 문자열을 입력받는다.
따라서 앞뒤 공백이나 스페이스가 구분자로 인식되어 입력값으로 들어오지 않는다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s;
while(sc.hasNext()) {
s = sc.next();
System.out.println(s);
}
}
}
nextLine()
-> nextLine()은 토큰 단위로 입력값을 받아오는 게 아닌 콘솔창의 한 문장을 간격으로 입력값을 받아온다.
따라서 앞뒤에 공백이 있거나 중간에 space가 있어도 이를 구분자로 사용하지 않고 모두 받아올 수 있다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s;
while(sc.hasNextLine()) {
s = sc.nextLine();
System.out.println(s);
}
}
}
'언어 > JAVA 자바' 카테고리의 다른 글
[JAVA]객체지향 개념을 고려한 숫자야구 게임의 구현 (0) | 2022.06.08 |
---|---|
JAVA 파라미터에 오는 점 세개 문법 ! (0) | 2022.04.16 |
JAVA 접근 제한자 정리 (0) | 2022.04.14 |
[JAVA] Final 이란? (0) | 2022.03.03 |
[JAVA] Static이란? (0) | 2022.02.28 |
Comments