본문 바로가기

Study/Java

Java 기초_기본 입출력

0. 기본 출력

 

출력을 위한 메소드

System.out.println : 출력 후 위치를 다음 행으로 이동

    * ln == \r\n , \r : 줄의 맨 앞으로 이동하기, \n : 한 줄 띄우기 (자바에서는 \r을 생략해도 된다.)

      \t = 일정한 거리 띄어쓰기

System.out.print : 출력 후 다음 print내용이 뒤에 바로 붙어서 출력

System.out.printf : 형식을 지정하여 출력

 

형식지정자(%)와 영문자 사이에 부호로써 위치 조절 가능

%d : 정수형

%f : 실수형

%s : 문자열

%c : 한문자

 

 

class DataTest {
	public static void main(String[] arg) {
    	String str = "출력 예제"
        byte a = 5;
        short b = 10;
        int c = 15;	// 정수 기본 자료형
        long d = 20l;	// l을 안 쓸 경우 int로 이해/출력되긴 하나 시간이 더 오래 걸림
	float e = 25.67f;
        double f = 26.789;	// 실수 기본 자료형
        char g = 'G';	// 한 글자는 ' ' / 한 글자 이상은 " " 쌍따옴표로
        boolean h = true;
        
        System.out.print(str);
	System.out.print(a);
	System.out.print(b);
	System.out.print(c);
	System.out.print(d);
	System.out.print(e);
	System.out.print(f);
	System.out.print(g);
	System.out.print(h);
	}
}

/*
[결과]

출력예제
5
10
15
20
25.67
26.789
G
true


*/

 

class DataTypeTest {
	public static void main(String[] arg) {
        String str1 = "안녕,";
    	String str2 = new String("자바!");
        byte a1 = 5;
        short b2 = 10;
        int c3 = 15;	// 정수 기본 자료형
        long d4 = 20l;	// l을 안 쓸 경우 int로 이해/출력되긴 하나 시간이 더 오래 걸림
	float e5 = 25.67f;
        double f6 = 26.789;	// 실수 기본 자료형
        char g7 = 'G';	// 한 글자는 ' ' / 한 글자 이상은 " " 쌍따옴표로
        boolean h8 = true;
        
        System.out.print("String 형1:" + str1);
        System.out.print("String 형2:" + str2);
	System.out.print("byte 형:" + a1);
	System.out.print("char 형:" + b2);
	System.out.print("short 형:" + c3);
	System.out.print("int 형:" + d4);
	System.out.print("long 형:" + e5);
	System.out.print("float 형:" + f6);
	System.out.print("double 형:" + g7);
	System.out.print("boolean 형:" + h8);
	}
}

/*
[결과]

String 형1:안녕,
String 형2:자바!
byte 형:5
char 형:10
short 형:15
int 형:20
long 형:25.67
float 형:26.789
double 형:G
boolean 형:true


*/

 

1. 한글자 입력

System.in.read() : 한글자 입력을 받고자 할때는 read()메소드를 이용, 예외처리 반드시 해 주어야 함

throws IOException : 입력받는 값이 있을때 예외처리, 익셉션(예외)가 발생하면 해당 클래스에서 벗어나게 된다는 뜻

import java.io.*; // throws IOException을 사용하기 위해 꼭 필요

public class Test_01 {
	public static void main(String[] arg) throws IOException {
    System.out.print("아무키나 입력하세요 : ");
    int a = System.in.read();			// 여기서 한글자 입력을 받는다. 받은 값을 ASC코드값으로 받는다.
    // 만일 숫자 한 글자를 입력받고 싶다면 -48을 해준다
    // ex. int a = System.in.read() - 48;
    System.out.println("a = " + a);
    System.out.println("a = " + (char)a);	// 문자로 변환시에는 (char)로 형변환
    }
}

 


2. 문자열 입력

 

문자를 입력받을때 사용되는 클래스

  1. BufferedReader : 대문자로 시작 = 클래스 = import java.io.*; 상단 호출 필수
  2. Scanner
import java.io.*; // throws IOException을 사용하기 위해 꼭 필요

public class Test_02 {
	public static void main(String[] arg) throws IOException {

		BufferReader in = new BufferReader(New InputStreamReader(System.in));
        // in을 통해서 문자열을 입력 받겠다고 선언하는 것
        System.out.print("이름을 입력하세요 : ");
        String name = in.readLine();
        // in.readLine()에서 입력을 받음 : Enter키가 들어오면 끝! 굳이 Enter값을 넣어줄 필요 x
        System.out.println(name+"님 반갑습니다.");
    }
}
import java.io.*; // throws IOException을 사용하기 위해 꼭 필요

public class Test_03 {
	public static void main(String[] arg) throws IOException {

		BufferReader in = new BufferReader(New InputStreamReader(System.in));
        // in을 통해서 문자열을 입력 받겠다고 선언하는 것
        System.out.print("수학점수를 입력하세요 : ");
        int math = Integer.parselnt(in.readLiine());
        /* in.readLine()은 String밖에 입력받지 못해 Integer.parseInt를 사용해 int를 받겠다는 뜻
        => 파싱작업 이라고 함(형변환)
        byte를 받겠다 == byte.parseByte(in.readLine()); */
        System.out.println("입력하신 수학 점수는" + math + "점 입니다.");
    }
}

 

Scanner 가 BufferedReader에 비해 더 수월하다!

 

import java.util.*;	// Scanner 를 사용하기 위해 import

public class Test_04 {
	public static void main(String[] arg){
    // Scanner를 사용할 때에는 throws IOException 예외처리 해줄 필요 x

	Scanner sc = new Scanner(System.in);
        // sc를 통해서 문자열을 입력 받겠다고 선언하는 것
        System.out.print("이름 : ");
        String name = sc.next();
        System.out.print("수학점수 : ");
        int math = sc.nextInt()
        System.out.println(name + "님의 수학 점수는" + math + "점 입니다.");
    }
}

 


03. 직접 해보기