Java_03_입력함수, 출력함수

OngTK·2025년 7월 9일

Java

목록 보기
3/35

1. 입력함수


콘솔화면에서 자료를 입력받는 방법

1) 형태

new Scanner( System.in ); : 시스템으로부터 입력 관련 기능을 제공하는 객체

일반적으로 아래와 같이 작성하여 사용

Scanner scan = new Scanner();

2) 종류

구분설명
.next()입력받은 문자열(String) 반환 (단, 띄어쓰기 불가)
.nextLine()입력받은 문자열(String) 반환, 띄어쓰기 가능
※ 주의점
nextLine() 앞에 다른 next()가 존재하면 enter(개행)를 포함하므로 문제 발생
해결 : nextLine() 앞에 무의미한 nextLine()를 하나 추가
.nextByte()입력받은 정수(byte)를 반환
.nextShort()입력받은 정수(short)를 반환
.nextInt()입력받은 정수(int)를 반환
.nextLong()입력받은 정수(long)를 반환
.nextFloat()입력받은 실수(float)를 반환
.nextDouble()입력받은 실수(double)를 반환
.nextBoolean()입력받은 true/false를 반환

※주의 .nextChar()는 없음 ☆★☆★☆★☆★☆★

char value = scan.next().charAt(0);
//입력받은 문자열 중에 첫번째[index 0] 글자를 반환

3) 예제

public class Example {
    public static void main(String[] args) {
// [2] Scanner 객체를 변수에 저장하여 활용
        Scanner scan = new Scanner(System.in); 
        // 객체타입 : Scanner, 변수명 : scan, 객체 : new Scanner(System.in)

        // [2.1] .next();
        System.out.println("1. next() : ");
        String str1 = scan.next();
        System.out.println("1. next result : " + str1);

        // [2.2] .nextLine();
        System.out.println("2. nextLine() : ");
        scan.nextLine();
        String str2 = scan.nextLine();
        System.out.println("2. next result : " + str2);

        // [2.3] .nextByte();
        System.out.println("3. nextByte() : ");
        byte value1 = scan.nextByte();
        System.out.println("3. nextByte result : " + value1);

        // [2.4] .nextShort();
        System.out.println("4. nextShort() : ");
        short value2 = scan.nextShort();
        System.out.println("4. nextShort result : " + value2);

        // [2.5] .nextInt();
        System.out.println("5. nextInt() : ");
        int value3 = scan.nextInt();
        System.out.println("5. nextInt result : " + value3);

        // [2.6] .nextLong();
        System.out.println("6. nextLong() : ");
        long value4 = scan.nextLong();
        System.out.println("6. nextLong result : " + value4);

        // [2.7] .nextFloat();
        System.out.println("7. nextFloat() : ");
        float value5 = scan.nextFloat();
        System.out.println("7. nextFloat result : " + value5);

        // [2.8] .nextDouble();
        System.out.println("8. nextDouble() : ");
        double value6 = scan.nextDouble();
        System.out.println("8. nextDouble result : " + value6);

        // nextChar는 없음!
        System.out.println("10. next() : ");
        char value8 = scan.next().charAt(0);
        // 입력받은 문자열 중에 첫번째[index 0] 글자를 반환
    }
}

2. 출력함수


(1) System.out.print( );

console에 출력

(2) System.out.println( );

console에 출력 및 줄바꿈

(3) System.out.printf("형식문자1 형식문자2", 값1, 값2);

구분분류설명
형식문자%s문자열
%d정수
%c문자
%f실수
자릿수%자릿수d자릿수만큼 자리 차지, 만일 비어있으면 공백, 오른쪽 정렬
%-자릿수d자릿수만큼 자리 차지, 비어있으면 공백, 왼쪽 정렬
%0자릿수d자릿수만큼 자리 차지, 만일 비어있으면 0을 채움
소수점 자릿수%전체자릿수.소수점자릿수f(소수점 포함) 전체 자릿수 만큼 자리 차지
        //[1]
        System.out.println("자바안녕1");
        System.out.println("자바안녕2");

        System.out.print("자바안녕3"); // 줄바꿈 없음
        System.out.print("자바안녕4");

        System.out.printf("%s" , "자바안녕5\n");

        //[2]
        String name = "유재석";
        int age = 40;
        System.out.printf("저는 %s이고 나이는 %d입니다.\n" , name, age);
        // vs
        System.out.println("저는 " + name + "이고 나이는 " + age + "입니다.\n");
        System.out.printf("저는 %s이고 나이는 %6d 입니다. \n", name , age); //%6d : 6자리 수라는 정수 표현 (오른쪽 정렬)
        System.out.printf("저는 %s이고 나이는 %-6d 입니다. \n" , name , age); // %-6d : 왼쪽 정렬
        System.out.printf("저는 %s이고 나이는 %06d 입니다. \n" , name , age); // 6자리를 표현하며 빈 칸은 0으로 채움
        System.out.printf("저는 %s이고 신장은 %5.2f 입니다. \n" , name, 188.257);
profile
2025.05.~K디지털_풀스택 수업 수강중

0개의 댓글