System.out 을 통해서 출력을 했듯이, System.in 을 통해서 사용자의 입력을 받을 수 있다.
System.in 을 통해서 사용자 입력을 받으려면 여러 과정을 거쳐야해서 복잡하고 어렵다.
자바는 이런 문제를 해결하기 위해 Scanner 라는 클래스를 제공한다.
<Scanner1.java>
package scanner;
import java.util.Scanner;
public class Scanner1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("문자열을 입력하세요 : ");
String str = scanner.nextLine();
System.out.println("입력한 문자열 : "+str);
System.out.println("정수를 입력하세요 : ");
int intV = scanner.nextInt();
System.out.println("입력한 정수 : "+intV);
System.out.println("실수를 입력하세요 : ");
double doubleV = scanner.nextDouble();
System.out.println("입력한 실수 : "+doubleV);
}
}
- scanner.nextLine()
엔터( \n )을 입력할 때 까지 문자를 가져온다.
<결과>
타입이 다르면 오류가 발생한다. 예제와 같이 숫자에 문자를 입력하면 오류가 발생한다
print() 출력하고 다음라인으로 넘기지 않는다.
println() 출력하고 다음라인으로 넘긴다.
<Scanner2.java>
package scanner;
import java.util.Scanner;
public class Scanner2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("첫 번째 숫자를 입력하세요:");
int num1 = scanner.nextInt();
System.out.print("두 번째 숫자를 입력하세요:");
int num2 = scanner.nextInt();
int sum = num1 + num2;
System.out.println("두 숫자의 합: " + sum);
}
}
<Scanner3.java>
package scanner;
import java.util.Scanner;
public class Scanner3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
if(num1 > num2){
System.out.println("num1이 더 크다.");
}
else if(num2 > num1){
System.out.println("num2이 더 크다.");
}else if(num2 == num1){
System.out.println(" 두수가 같다.");
}
}
}
<ScannerWhile1.java>
package scanner;
import java.util.Scanner;
public class ScannerWhile1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("문자열을 입력하세요(exit: 종료):");
String str = scanner.nextLine();
if(str.equals("exit")){
System.out.println("종료");
break;
}
}
}
}
<결과>
<Scanner3.java>
package scanner;
import java.util.Scanner;
public class ScannerWhile2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true){
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
if(num1==0 && num2==0){
break;
}else {
int sum = num1+num2;
System.out.println(sum);
}
}
}
}
<ScannerWhile3.java>
package scanner;
import java.util.Scanner;
public class ScannerWhile3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sum =0;
while(true){
int num = scanner.nextInt();
if(num==0){
break;
}else {
sum = sum + num;
}
}
System.out.println(sum);
}
}
<ScannerEx1.java>
package scanner.ex;
import java.util.Scanner;
public class ScannerEx1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int age = scanner.nextInt();
System.out.println("이름 : "+name+ " 나이 : "+age);
}
}
<ScannerEx2.java>
package scanner.ex;
import java.util.Scanner;
public class ScannerEx2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
if(num %2 ==0){
System.out.println("짝수입니다");
}else {
System.out.println("홀수입니다");
}
}
}
<ScannerEx3.java>
package scanner.ex;
import java.util.Scanner;
public class ScannerEx3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String foodName = scanner.nextLine();
int foodPrice = scanner.nextInt();
int foodQuantity = scanner.nextInt();
int sum = foodPrice * foodQuantity;
System.out.println(foodName + " " + foodQuantity+"개를 주문하셨습니다.");
System.out.println("총 가격은 "+ sum +"원 입니다.");
}
}
<ScannerEx4.java>
package scanner.ex;
import java.util.Scanner;
public class ScannerEx4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
for (int i=1; i<=9 ; i++){
System.out.println(num + " * " + i +" = " + num+i);
}
}
}
<ChangeVarEx.java>
package scanner.ex;
public class ChangeVarEx {
public static void main(String[] args) {
int a=10;
int b=20;
int temp;
temp =a;
a=b;
b=temp;
System.out.println("a : " + a + " b : "+b);
}
}
<ScannerEx5.java>
package scanner.ex;
import java.util.Scanner;
public class ScannerEx5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
if(num1 > num2){
int temp;
temp = num1;
num1 =num2;
num2 = temp;
}
for( int i = num1; i<=num2; i++){
System.out.print(i);
if(i!=num2){
System.out.print(",");
}
}
}
}
<ScannerWhileEx1.java>
package scanner.ex;
import java.util.Scanner;
public class ScannerWhileEx1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true){
String name = scanner.nextLine();
if(name.equals("종료")){
System.out.println("프로그램을 종료합니다.");
break;
}
int age = scanner.nextInt();
scanner.nextLine();
System.out.println("이름 : "+ name + " 나이 : "+ age);
}
}
}
scanner.nextLine();
숫자 입력 후 줄바꿈 처리
<ScannerWhileEx2.java>
package scanner.ex;
import java.util.Scanner;
public class ScannerWhileEx2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true){
int price = scanner.nextInt();
if(price==-1){
System.out.println("프로그램을 종료합니다.");
break;
}
int quantity = scanner.nextInt();
int sum = price*quantity;
System.out.println("총비용 : "+ sum );
}
}
}
<ScannerWhileEx3.java>
package scanner.ex;
import java.util.Scanner;
public class ScannerWhileEx3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sum =0;
double average=0;
int count =0;
while(true){
int num = scanner.nextInt();
if(num==-1) {
System.out.println("프로그램을 종료합니다.");
break;
}
sum += num;
count ++;
}
average = (double) sum / count;
System.out.println("sum : "+sum +" average : "+average);
}
}
<ScannerWhileEx4.java>
package scanner.ex;
import java.util.Scanner;
public class ScannerWhileEx4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int total=0;
while(true){
int option = scanner.nextInt();
if (option == 1) {
scanner.nextLine();
System.out.print("상품명을 입력하세요: ");
String product = scanner.nextLine();
System.out.print("상품의 가격을 입력하세요: ");
int price = scanner.nextInt();
System.out.print("구매 수량을 입력하세요: ");
int quantity = scanner.nextInt();
total = price * quantity;
System.out.println("상품명:" + product + " 가격:" + price + " 수량:" + quantity + " 합계:" + total);
} else if (option == 2) {
System.out.println("총 비용 : " + total);
total = 0;
} else if (option == 3) {
System.out.println("프로그램을 종료합니다.");
break;
} else {
System.out.println("올바른 옵션 값을 입력해주세요.");
}
}
}
}