문제풀다가 자바도 파이썬처럼 입출력을 빠르게 하는 클래스가 있는걸 알게되어서 정리!
java.util.Scanner sc = new java.util.Scanner(System.in);short-cut
import java.util.Scanner;
.
.
.
Scanner sc = new Scanner(System.in);
int num = sc.nextInt(); // 정수문자열 입력받아 출력하기
String st = sc.nextLine();
System.out.println(st);
/*
입력: 안녕하세요 선생님
출력: 안녕하세요 선생님
*/
String stn = sc.next();
System.out.println(stn);
/*
입력: 안녕하세요 선생님
출력: 안녕하세요
*/Scanner은 Buffer를 사용하기 때문에,
Scanner sc = new Scanner(System.in);
int num = sc.nextInt(); // 여기서 1 2를 입력하면
int num1 = sc.nextInt();
System.out.println(num+ " "+num1); // 1 2가 출력된다. Buffer때문이다.이러한 문제를 해결하기 위해서
방식1:
Scanner sc = new Scanner(System.in);
int num = sc.nextInt(); // 1 2 입력
sc.nextLine();
int num1 = sc.nextInt(); // 3 4 입력
sc.nextLine();
System.out.println(num+ " "+num1); // 1 3가 출력된다.방식2: 더 많이 쓰이는 방법
int num2 = Integer.parseInt(sc.nextLine());
// 이런식으로 쓰면 위 처럼 긴 코드를 할 필요 없어진다.Random r = new Random();
int num = r.nextInt(); // 임의의 정수(음수&양수) 추출
System.out.println(num);
int num1 = r.nextInt(10); // 0~9사이의 난수(양수) 추출
System.out.println(num1);
int num2 = r.nextInt(10)+1; // 1(0+1)~10(9+1)사이의 난수(양수) 추출
System.out.println(num2);- line feed(\n, 개행문자) && carriage return를 가지고있다.
System.out.println("abc");
System.out.println("123");
/*
abc
123
*/
System.out.print("abc");
System.out.println("123");
/*
abc123
*/
// 그래서 아래의 출력은 같다.
System.out.print("abc\n");
System.out.println("abc");
/*
abc
abc
*/'' 따움표: 문자(Character)
"" 쌍따움표: 문자열(String)
*printf를 사용하면 입출력이 더 빠르다.
기본타입은? (논리값까지 포함하면 5가지이다.)
- 문자(char) 'ㅁ', '!'
- 문자열(string) "hello"
- 정수(byte short int long) 3, 12
- 실수(float, double) 0.1, 12.3
- 논리값(boolean) true false
자바의 기본형: char, string, int, double, boolean
%d
%f
%c
%s
System.out.printf("%d %s", 10, "ABC");
/*
10 ABC
*/tab을 해준다. 일정한 간격으로 띄워져서 출력된다.
System.out.printf("%d\t%s\n", 10 ,"Hello");
System.out.printf("%d\t%s\n", 10 ,"ABC");
System.out.printf("%d\t%s\n", 10 ,"NEW");
/*
10 Hello
10 ABC
10 NEW
*/만약 tab(8자)를 넘어선다면?
10 Hello
10 ABC
10 NEW
10029232 NEW이런식으로 넘어가게되는 것을 볼 수 있다.
*그래서 해당 데이터가 들어갈 자리에 칸을 지정해주는 방식을 사용할 수 있다.
숫자 오른쪽 정렬
System.out.printf("%10d %s\n", 10 ,"Hello");
System.out.printf("%10d %s\n", 10 ,"ABC");
System.out.printf("%10d %s\n", 10 ,"NEW");
System.out.printf("%10d %s\n", 10029232 ,"NEW");
/*
10 Hello
10 ABC
10 NEW
10029232 NEW
*/마이너스(-)숫자 왼쪽 정렬
System.out.printf("%-10d %s\n", 10 ,"Hello");
System.out.printf("%-10d %s\n", 10 ,"ABC");
System.out.printf("%-10d %s\n", 10 ,"NEW");
System.out.printf("%-10d %s\n", 10029232 ,"NEW");
/*
10 Hello
10 ABC
10 NEW
10029232 NEW
*/*기본으로 소수점 8자리까지 찍는데,
%.3f로 하면3자리까지만 찍는다.
System.out.printf("%-10.0f %s\n", 10.929232 ,"NEW");
/*
11 NEW
*/*무조건 절사가 되게 하고싶으면,
-0.05를 뺀다.
System.out.printf("%-10.1f %s\n", 12.43 - 0.05 ,"NEW");
System.out.printf("%-10.1f %s\n", 12.46 - 0.05 ,"NEW");
/*
12.4 NEW
12.4 NEW
*/문자와 문자를 더하면 숫자가 나온다. 왜냐하면 컴퓨터는 문자를 숫자라고 인식하기 때문이다.
System.out.println('A'+'B');
/*
131
*/
System.out.println(65 +'B');
/*
131
*/유니코드 '\u0041'
System.out.println('\u0041');
// A아스키코드 65
문자 'A'
빠르게 입력 받을 수 있는 클래스
해당 클래스로 readLine() 메소드를 사용할 수 있는데, 이렇게 가져온 String을 Integer.parseInt()등 을 사용하여 용도에 맞춰 정제해 사용한다.
빠르게 출력할 수 있는 클래스
해당 클래스로 write() 메소드를 사용할 수 있는데, 다 사용하고 나서 반드시 close()로 스트림을 닫아줘야 한다.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 처음에 br.readLine()으로 받을땐, String으로 들어와서 Integer.parseInt()로 타입변환을 해준다.
int T = Integer.parseInt(br.readLine());
// List<Integer> ans = new ArrayList<>();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));//선언
for (int i=0; i < T; i++) {
str = br.readLine().split(" ");
// ans.add((Integer.parseInt(str[0])+Integer.parseInt(str[1])));
bw.write((Integer.parseInt(str[0])+Integer.parseInt(str[1]))+"\n");//출력
}
// bw.flush();//남아있는 데이터를 모두 출력시킴
bw.close();//스트림을 닫음1 2
3 4
위 처럼 입력되는 입력을 " "을 기준으로 Tokenizing해준다.
그리고, 앞에서 부터 .nextToken() 메소드로 변수에 할당 할 수 있다/순회할 수 있다.
String화 할 타입을 .append()해주면 String형식으로 차곡 차곡 쌓인다. Python에서 String에 +하는 식으로 쌓거나 .join()을 쓰던 방식과 약간 다르다. 이렇게 하면 시간이 많이 줄어든다.
public class Main {
// Stream으로 풀어보자
static int N;
static int X;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer1 = new StringTokenizer(br.readLine());
N = Integer.parseInt(tokenizer1.nextToken());
X = Integer.parseInt(tokenizer1.nextToken());
String[] str = br.readLine().split(" ");
int[] array = Arrays.stream(StringArrayToIntArray(str))
.filter(x -> x < X )
.toArray();
StringBuilder sb = new StringBuilder();
for(int num : array) { sb.append(num).append(" "); }
System.out.println(sb);
}
static int[] StringArrayToIntArray(String[] stringArray)
{
return Stream.of(stringArray).mapToInt(Integer::parseInt).toArray();
}
}