Java Integer 문자열 표현

JavaBeginner
지금 연습하기

소개

이 랩에서는 Java 의 Integer 클래스의 toString() 메서드에 대해 배우겠습니다. 이 메서드를 사용하여 Integer 객체의 문자열 표현을 얻는 방법을 살펴볼 것입니다. 또한 사용법을 이해하기 위한 몇 가지 예제를 볼 것입니다.

환경 설정

IntegerToString.java라는 새 Java 파일을 생성합니다.

public class IntegerToString {
    public static void main(String[] args) {
        // code to be added
    }
}

toString() 메서드를 사용하여 Integer 객체를 String 으로 변환

이 단계에서는 정수 객체를 생성한 다음 toString() 메서드를 사용하여 해당 정수 객체를 문자열로 변환합니다.

public class IntegerToString {
    public static void main(String[] args) {
        // create an Integer object
        Integer num = 35;

        // convert integer to string
        String str = num.toString();

        // print the string
        System.out.println("String representation of the Integer: " + str);
    }
}

출력:

String representation of the Integer: 35

toString() 을 사용하여 음수 Integer 를 String 으로 변환

toString() 메서드는 음수 정수에도 작동합니다. 이 단계에서는 음수 정수를 문자열 표현으로 변환합니다.

public class IntegerToString {
    public static void main(String[] args) {
        // create a negative Integer object
        Integer num = -18;

        // convert negative integer to string
        String str = num.toString();

        // print the string
        System.out.println("String representation of the Integer: " + str);
    }
}

출력:

String representation of the Integer: -18

toString() 메서드를 사용하여 입력된 정수를 String 으로 변환

이 단계에서는 Scanner를 사용하여 입력 정수를 생성한 다음 toString() 메서드를 사용하여 문자열 표현을 얻습니다.

import java.util.Scanner;

public class IntegerToString {
    public static void main(String[] args) {
        // create scanner object
        Scanner sc = new Scanner(System.in);

        // take input integer from user
        System.out.print("Enter an integer: ");
        int num = sc.nextInt();

        // convert integer to string
        String str = Integer.toString(num);

        // print the string
        System.out.println("String representation of the entered integer: " + str);
    }
}

출력:

Enter an integer: 56
String representation of the entered integer: 56

잘못된 입력 처리

4 단계에서는 입력이 정수가 아닌 경우를 처리하지 않았습니다. 이 단계에서는 입력이 유효한 정수가 아닌 경우의 예외를 처리합니다.

import java.util.Scanner;

public class IntegerToString {
    public static void main(String[] args) {
        // create scanner object
        Scanner sc = new Scanner(System.in);

        try {
            // take input integer from user
            System.out.print("Enter an integer: ");
            int num = sc.nextInt();

            // convert integer to string
            String str = Integer.toString(num);

            // print the string
            System.out.println("String representation of the entered integer: " + str);
        } catch(Exception e) {
            System.out.println("Invalid input");
        }
    }
}

출력:

Enter an integer: 34
String representation of the entered integer: 34

toBinaryString() 을 사용하여 정수를 이진수로 변환

Java 에서는 Integer 클래스의 toBinaryString() 메서드를 사용하여 정수를 이진 문자열 표현으로 변환할 수 있습니다.

public class IntegerToString {
    public static void main(String[] args) {
        // create an integer
        int num = 10;

        // convert integer to binary string
        String binaryString = Integer.toBinaryString(num);

        // print binary string
        System.out.println("Binary string representation of the Integer: " + binaryString);
    }
}

출력:

Binary string representation of the Integer: 1010

toHexString() 을 사용하여 정수를 16 진수로 변환

Java 에서는 Integer 클래스의 toHexString() 메서드를 사용하여 정수를 16 진수 문자열 표현으로 변환할 수 있습니다.

public class IntegerToString {
    public static void main(String[] args) {
        // create an integer
        int num = 255;

        // convert integer to hex string
        String hexString = Integer.toHexString(num);

        // print hex string
        System.out.println("Hexadecimal string representation of the Integer: " + hexString);
    }
}

출력:

Hexadecimal string representation of the Integer: ff

요약

이 랩에서는 Integer 클래스의 toString() 메서드를 사용하여 정수를 문자열 표현으로 변환하는 방법을 배웠습니다. 또한, 잘못된 입력을 처리하고 정수를 이진수 또는 16 진수 문자열 표현으로 변환하는 방법도 배웠습니다. 이 방법은 다양한 컨텍스트에서 사용할 수 있도록 정수를 문자열로 변환해야 할 때 유용합니다.