Scanner

import java.util.Scanner;

public class Scanner{
		public static void main(String[] args){
				Scanner scanner = new Scanner(System.in);
				
				String str = scanner.nextLine();
				int intValue = scanner.nextInt();
				double doubleValue = scanner.nextDouble();
		}
}

🔄 변수 값 교환

public static void main(String[] args){
		int a = 10, b = 20;
		int temp;
		
		//교환
		temp = a;
		a = b; 
		b = a;
}

<aside> 📌

n**extInt() vs nextLine()**

메서드 설명
nextInt() 정수만 읽고개행 문자(\\n)는 소비하지 않음
nextLine() 한 줄 전체를 읽으며, 개행 문자까지 소비함

import java.util.Scanner;

public class ScannerProblemDemo {
	public static void main(String[] args) {
		Scanner scanner = new Scanner([System.in](<http://system.in/>));
    System.out.print("나이를 입력하세요: ");
    int age = scanner.nextInt(); // 여기서 숫자만 읽고 \\\\n은 버퍼에 남음

    System.out.print("이름을 입력하세요: ");
    String name = scanner.nextLine(); // 버퍼에 남은 \\\\n을 그대로 읽어버림

    System.out.println("나이: " + age + ", 이름: " + name);
	}
}
// 실행 예시
나이를 입력하세요: 25⏎
이름을 입력하세요:
→ 이름 입력 없이 바로 넘어가고 name은 빈 문자열이 됨

✅ 원인

✅ 해결 방법

System.out.print("나이를 입력하세요: ");
int age = scanner.nextInt();
scanner.nextLine(); // ← 버퍼에 남아 있던 \\n 제거

System.out.print("이름을 입력하세요: ");
String name = scanner.nextLine();

</aside>