import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a =  sc.nextInt();
        //sc.nextLine();
        System.out.println(a);
        String s = sc.nextLine();
        System.out.println(s);
    }

}

 

nextInt는 엔터를 버퍼에 남기기 때문에

다음에 s를 입력하기 전에 엔터가 남아있어서

공백이 출련된다.

 

이것을 수정하기 위해 

sc.nextLine()을 사용하면

엔터의 버퍼를 지워주기 때문에

다음 문자를 입력할 수 있다.

 

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a =  sc.nextInt();
        sc.nextLine();
        System.out.println(a);
        String s = sc.nextLine();
        System.out.println(s);
    }

}

 

 

 

숫자만 입력 두번 할 시

nextLine()이 필요없다.

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a =  sc.nextInt();
        //sc.nextLine();
        System.out.println(a);
        int s = sc.nextInt();
        System.out.println(s);
    }

}

 

 

 

 

StringBuilder는 문자열을 자주 이어붙이거나 수정할 때 사용하는 클래스입니다.
String과 달리 변하지 않는(immutable) 게 아니라 변하는(mutable) 문자열을 다룹니다.

 

 

 

 

 

 

즉 새로운 문자열을 만들 번거로움 없이

즉각적으로 문자열을 수정할 때 용이하다.

 

StringBulider는 기본적으로 문자열이 아닌 배열

 

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String before = sc.next();
        System.out.println(before);
        int after = Integer.parseInt(before.substring(0,2));
        System.out.println(after);
    }

}

 

Integer.parseInt()는 문자열(String) 로 되어 있는 숫자를 정수(int) 로 바꿔주는 함수

 

 

'Study > ' 카테고리의 다른 글

[Java] StringBuilder  (0) 2025.11.20
[Java] charAt을 활용하여 숫자를 문자열로 변환  (0) 2025.11.17
[Java] Array.sort()  (0) 2025.11.17
[Java] Integer.MIN_VALUE  (0) 2025.11.16
[Tip] 맥 SVN 설치 - snailSVN  (0) 2024.07.21
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String T  =  sc.next();
        int sum = 0;
        for(int test_case=0;test_case<T.length();test_case++){
            sum  += T.charAt(test_case) - '0';
        }
        System.out.println(sum);
    }
}

 

각 입력받은 숫자들의 자리수 별로 합을 구한다.

예를들어 5624면 5 + 6 + 2 + 4 인 17이된다.

입력을 숫자가 아닌 문자열로 받으면

'0'은 0의 아스키코드인 48

'1'은 1의 아스키코드인 49

...

이렇게 실제 수보다 48 이 크게나오므로

 

공통된 48인 '0'을 빼주며

각 자리를 반복문을 통해 더해준다.

'Study > ' 카테고리의 다른 글

[Java] StringBuilder  (0) 2025.11.20
[Java] Integer.parseInt(String.substring(num1,num2)) + String.format("%02d", num)  (0) 2025.11.18
[Java] Array.sort()  (0) 2025.11.17
[Java] Integer.MIN_VALUE  (0) 2025.11.16
[Tip] 맥 SVN 설치 - snailSVN  (0) 2024.07.21

Arrays.sort()란?

자바에서 배열을 오름차순(작은 → 큰) 으로 정렬해주는 기능이다.

사용 방법:

 
Arrays.sort(arr);

이 한 줄이면 배열 arr 안에 있던 값들이 자동으로 정렬됨.

 

 

 

 

package SWEA;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] arr = new int[N];

        for(int i = 0; i < N; i++){
            arr[i] = sc.nextInt();
        }

        Arrays.sort(arr);

        int midIndex = N/2;
        System.out.println(arr[midIndex]);

    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.MIN_VALUE);
    }
}

 

 

 

-2147483648

 

 

public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
    }
}

 

 

 

2147483647

 

 

 

Integer.MIN_VALUE와 Integer.MAX_VALUE는

자바에서 int 타입이 가질 수 있는 가장 작은 값을 의미

 


 

 

처음에 테스트 케이스를 받아야하므로 정수형을 입력받아야한다.

그리고 나서 버퍼를 지워야하기 위해 nextLine을 사용

 

 

처음에 charAt을 사용해서 Hello라는 문구를 넣었을 때

숫자로 나왔다.

아스키 코드인 H(72) 와 o(111)의 합이 183이기 때문

 

그래서 찾아보니 문자열을 더하기 위해선 앞에 " " 를 붙여줘야 한다.

 

 

 

 

import java.util.Scanner;

public class num9086 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int T = sc.nextInt();
        sc.nextLine();

        for(int i = 0; i < T; i++){
            String str = sc.nextLine();
            System.out.println("" + str.charAt(0) + str.charAt(str.length() - 1));
        }
    }
}

'Java > 백준을 풀며' 카테고리의 다른 글

repeat(num)  (0) 2025.09.05
BigInteger  (1) 2025.08.28

백준 25314번(브론즈5) 문제를 풀며 문제발생

 

 

 


 

 

문자열을 반복하는 과정에서 메서드를 찾게 되었다.

 

.repeat(num> => num 안에는 숫자를 넣으면 된다.

System.out.println("long ".repeat(N / 4) + "int");

 

 

https://learn.microsoft.com/en-us/dotnet/api/java.lang.string.repeat?view=net-android-34.0

 

String.Repeat(Int32) Method (Java.Lang)

Returns a string whose value is the concatenation of this string repeated count times.

learn.microsoft.com

 

package Bronze5;

import java.util.Scanner;

public class num25314 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        int mul = num / 4;
        String plusNum = "long ";
        if(num <= 4){
            System.out.println("long int");
        }
        else if(num > 4){
            System.out.println(plusNum.repeat(mul) + "int");
        }
    }
}

 

이렇게 짜 보았는데 코드가 지저분한것 같아서

챗GPT에게 AS요청

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        System.out.println("long ".repeat(N / 4) + "int");
    }
}

 

다시 정리

 

package Bronze5;

import java.util.Scanner;

public class num25314 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        System.out.println("long ".repeat(N / 4) + "int");
    }
}

 

출력

 

 

'Java > 백준을 풀며' 카테고리의 다른 글

문자열연결 charAt(index)  (0) 2025.09.15
BigInteger  (1) 2025.08.28

 

백준 브론즈5를 풀다가 숫자를 나누려하는데 나누기가 되지 않는 상황 발생

 


 

그 이유는 사용가능 범위를 초과하였기 때문

 

 

 


 

 

그래서 BigInteger을 사용하여야 한다.

 

https://learn.microsoft.com/ko-kr/dotnet/api/java.math.biginteger?view=net-android-34.0

 

BigInteger 클래스 (Java.Math)

변경할 수 없는 임의 정밀도 정수입니다.

learn.microsoft.com

 

하지만 BigInteger은 산술 연산자를  사용하지 못하는데

그 이유는 객체이기 때문이다.

자바는 연산자 오버로딩을 지원하지 않는다.

 

 

따라서 우리는 이런 식으로 사용해야 한다.

 

 

 


 

 

백준에서는 클래스명을 Main으로 쓰지 않으면 컴파일 에러가 난다.

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        BigInteger n = sc.nextBigInteger();
        BigInteger m = sc.nextBigInteger();

        System.out.println(n.divide(m));
        System.out.println(n.remainder(m));
        sc.close();
    }
}

'Java > 백준을 풀며' 카테고리의 다른 글

문자열연결 charAt(index)  (0) 2025.09.15
repeat(num)  (0) 2025.09.05
package fc.java.test;

public class Human {
    public String name;
    public int age;
    private String phone;

    public Human(String name, int age, String phone) {
        this.name = name;
        this.age = age;
        this.phone = phone;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }

    public String getPhone(){
        return phone;
    }
}

 

package fc.java.test;

public class Test {
    public static void main(String[] args) {

        Human man = new Human("길동", 100, "010-1111-2222");
        System.out.println(man.getName() + "\t" + man.getAge() + "\t" + man.getPhone());
    }
}

'Java' 카테고리의 다른 글

scanner.nextLine()  (0) 2025.01.08

+ Recent posts