자바에서 입력을 받을 때 Scanner방식과 BufferedReader방식이 있다고 한다.
이번에 공부한 방식은 scanner 방식이다.
- import java.util.Scanner; 해주기
java.util은 유용한 클래스들을 모아놓은 패키지이다. Scanner도 이 안에 있기 때문에 import해줘야 한다.
- Scanner 객체 생성해주기
Scanner 객체이름 = new Scanner(System.in);
3 . 객체를 이용해 문자열 입력받기
Scanner sc = new Scanner(System.in);
String str = sc.next();
sc.뒤에 메서드를 통해 다양한 방식으로 입력 받을 수 있다.
next() String 공백이나 탭 엔터를 기준
nextLine() String 엔터를 기준
nextInt()
nextBoolean()
nextByte()
nextShort()
nextLong()
nextFloat()
nextDouble()
나머지는 직관적이라 next랑 nextLine만 잘 구분해주면 될 것 같다.