[JAVA]Scanner 클래스

이정원·2025년 2월 12일
0

Scanner란?

  • 사용자에게 입력을 받을 수 있게 해주는 클래스
  • 형식
    - ex)
    Scanner 별칭 = new Scanner();
    별칭.next();(nextInt(),nextLine(),nextDouble() ...)
  • 사용시 주의사항
    - nextLine() 와 next(), nextInt()와 함께 사용시 의도와 다르게 출력
    - nextLine()을 사용 전 next()와 nextInt()를 사용했다면 nextLine()과 next(), nextInt() 사이에 nextLine()을 추가하여 버퍼에 있는 엔터 값을 제거
    - ex)
    public void exam7() {
    			Scanner sc = new Scanner(System.in);
    		
    			System.out.println("이름 입력: ");
    			String name = sc.next();
    		
    			System.out.println("나이 입력: ");
    			int age = sc.nextInt();
    		
    			sc.nextLine();
    		
    			System.out.println("주소 입력: ");
    			String addr = sc.nextLine();
    		
    			System.out.print("이름: " + name + "\n나이: " + age + "\n주소: " + addr);
    		}
profile
Study.log

0개의 댓글