[Java] Scanner 정리
by youngjun._.Java Scanner
공백 분리 / 공백 나누기
// 입력 1 2 // 출력 3 // A+B public class p1000 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a, b; a = sc.nextInt(); b = sc.nextInt(); System.out.println(a + b); } }
// 입력 1 2 3 4 5 // 출력 15 //더하기3 public class p11023 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int sum = 0; while (sc.hasNextInt()) { sum += sc.nextInt(); } System.out.println(sum); } }
개행 분리 / 개행 나누기
// 입력 1 // 2 // 출력 3 // A+B - 2 public class p2558 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); //String s = sc.nextLine(); int b = sc.nextInt(); //String ss = sc.nextLine(); System.out.println(a + b); } }
증감 출력
// 입력 5 // 출력 1 // 2 // 3 // 4 // 5 //N찍기 public class p2741 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); for (int i = 1; i < num+1; i ++){ System.out.println(i); } } }
public class p2741_1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); StringBuilder sb = new StringBuilder(); for(int i = 1; i<=n; i++){ sb.append(i+"\n"); } System.out.println(sb); } }
문자열 분리 / 문자열 나누기
// 입력 5 // 54321 // 출력 15 //숫자의 합 public class p11720 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int sum = 0; int cnt = sc.nextInt(); String str = sc.next(); for(int i = 0; i<cnt; i++){ sum += str.charAt(i) - '0'; } System.out.println(sum); } }
char -> int : str.charAt(i) - '0';
char -> string : str.charAt(i) + " ";
알파벳에 해당하는 숫자: str.charAt(i) – ‘A’;
콤마 분리 / 콤마 나누기
// 입력 10,20,30,40,50,100 // 출력 210 //더하기 public class p10822 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String line = sc.nextLine(); String[] str = line.split(","); int sum = 0; for (int i = 0; i < str.length; i++) { sum += Integer.parseInt(str[i].trim()); } System.out.println(sum); } }
// 입력 5 // 1,1 // 2,3 // 3,4 // 출력 2 // 5 // 7 //A+B - 6 public class p10953 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = sc.nextInt(); for (int i = 0; i < count; i++) { String s = sc.next(); String temp[] = s.split(","); int a = Integer.parseInt(temp[0]); int b = Integer.parseInt(temp[1]); System.out.println(a + b); } } }
// 입력 5 // 1,1 // 2,3 // 3,4 // 출력 2 // 5 // 7 //A+B - 6 public class p10953 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = sc.nextInt(); for (int i = 0; i < count; i++) { String s = sc.next(); String temp[] = s.split(","); int a = Integer.parseInt(temp[0]); int b = Integer.parseInt(temp[1]); System.out.println(a + b); } } }
공백 분리 / 공백 나누기
// 입력 10 20 30 40 // 출력 4060 // 네수 public class p10824 { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] line = bf.readLine().split(" "); String a = line[0] + line[1]; String b = line[2] + line[3]; long ans = Long.valueOf(a) + Long.valueOf(b); System.out.println(ans); } }
Reference
공백, 개행, 콤마 등 여러가지 경우 마다 나누는 방법을 정리한 글
hasNextInt()
메소드로 원하는 만큼 입력을 받을 수 있음
블로그의 정보
개발하는만두
youngjun._.활동하기
개발하는만두공부한 내용을 정리합니다 :)