자바 숫자 자르기 - jaba susja jaleugi

자바에서 숫자를 입력하면 숫자 하나씩 하니씩 배열에 어떻게 넣나요? 4

무신론자

14,932

2011-09-14 11:26:22 129.♡.235.231

자바에서

만약 321 이라고 입력한다면

array[0] 에는 3 넣고

array[1] 에는 2 넣고

array[2] 에는 1을 넣게

어떻게 하면 되는지 궁금합니다.

힌트라도 주시면 감사하겠습니다!

아, 그리고 배열을 만약 안쓰고 한다면

321이라는 숫자를 넣었을 때

n번째 자리의 숫자를 알고 싶을땐 어떻게 해야하나요?

SIGNATURE

An atheist is like the child who doesn't believe in Santa Claus. He is not evil, but he is just smarter than others. 무신론자는 산타클로스를 믿지 않는 어린이와 같다. 그 꼬마는 악한 것이 아니라 단지 더 똑똑할 뿐이다.

서명 더 보기 서명 가리기

댓글 • [4] 을 클릭하면 간단한 회원메모를 할 수 있습니다.

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

지난번과 비슷한 일이 벌어졌다.

이번에는 날짜형식으로 된 8자리 데이터를 보내와야 하는데 싹다 무시하고 '0000-00-00' 형식으로 데이터가 날아왔다.

지난번에 작성한 특정 문자열을 제거하는 것으로는 해결이 되지 않는 문제다.

사실 지난번 문제보다 더 간단하다. 숫자만 빼고 모든 문자를 없애버리면 되기 때문이다.

따라서 문자열 메서드 중 replaceAll 을 사용하면 된다.

데이터가 '0000-00-00' 식으로 날아 왔기 때문에 

단순히 

str.replaceAll("-","");

str.replaceAll("-","");

식으로 처리해 줘도 되겠지만

또 어떤 형식으로 날아 올지 알 수 없기 때문에

숫자 이외의 문자는 모두 제거 하기로 한다.

따라서 이번에도 정규식을 사용하기로 한다.

str.replaceAll("[^0-9]","");

str.replaceAll("[^0-9]", "");

0~9 이외의 문자는 모두 제거한다.

"-" 만 제거 하는 것보다 훨씬 범용성이 좋아 졌다.

어제 오늘 내일

IT/Java

[Java] String을 int로, int를 String으로 변환하기 (문자열 숫자 변환)

hi.anna 2021. 4. 21. 08:06

String -> int (문자열을 숫자로)

String 문자열을 int로 변환하기 위해서는

java.lang.Integer 클래스의 parseInt()와 valueOf() 메소드를 사용할 수 있습니다.

 Integer.parseInt() 

static int parseInt​(String s)

java.lang.Integer 클래스의 static 메소드인 parseInt() 메소드는
파라미터로 숫자로 변환할 문자열을 입력받고,
입력받은 문자열을 integer로 변환한 int 값을 리턴합니다.

 코드 

public class StringToInt { public static void main(String[] args) { String str1 = "123"; String str2 = "-123"; int intValue1 = Integer.parseInt(str1); int intValue2 = Integer.parseInt(str2); System.out.println(intValue1); // 123 System.out.println(intValue2); // -123 } }

 결과 

123 -123

 Integer.valueOf() 

static int valueOf(String s)

parseInt() 메소드와 마찬가지로 
valueOf() 메소드는 java.lang.Integer 클래스의 static 메소드이고,
파라미터로 숫자로 변환할 문자열을 입력받습니다.
parseInt() 와 가장 큰 차이점은,
valueOf() 메소드는 문자열을 변환하여 Integer Object를 리턴한다는 것입니다.
parseInt() 메소드는 primitive type인 int를 리턴합니다.
(parseInt()와 valueOf()는 이 외에도 입력받는 파라미터의 타입 등의 차이점이 더 있습니다.)

 코드 

public class StringToInt { public static void main(String[] args) { String str1 = "123"; String str2 = "-123"; int intValue1 = Integer.valueOf(str1).intValue(); int intValue2 = Integer.valueOf(str2).intValue(); System.out.println(intValue1); // 123 System.out.println(intValue2); // -123 } }

 결과 

123 -123

예제를 살펴보면,

int intValue1 = Integer.valueOf(str1).intValue();

Integer.valueOf() 메소드는 Integer Object를 리턴하기 때문에,

이 Integer Object를 primitive type인 int로 변환하기 위해,

Integer 클래스의 intValue() 메소드를 다시 한번 호출하였습니다.

(사실, intValue() 메소드를 따로 호출하지 않아도,

위 케이스의 경우 자동으로 형변환이 일어나지만,

여기에서는 valueOf() 메소드가 Integer를 리턴한다는 사실을 강조하기 위해서,

명시적으로 intValue() 메소드를 호출하여 주었습니다.)

int -> String (숫자를 문자열로)

int를 String으로 변환하기 위해서는

Integer.toString(), String.valueOf() 메소드를 이용할 수 있고,

간단하게는 빈 문자열과 int를 '+'연산자로 연결하여 문자열로 변환할 수 있습니다.

 Integer.toString() 

 코드 

public class IntToString { public static void main(String[] args) { int intValue1 = 123; int intValue2 = -123; String str1 = Integer.toString(intValue1); String str2 = Integer.toString(intValue2); System.out.println(str1); System.out.println(str2); } }

 결과 

123 -123

Integer 클래스의 toString() 메소드를 사용하면 int를 문자열로 바꿀수 있습니다.

 String.valueOf() 

 코드 

public class IntToString { public static void main(String[] args) { int intValue1 = 123; int intValue2 = -123; String str1 = String.valueOf(intValue1); String str2 = String.valueOf(intValue2); System.out.println(str1); System.out.println(str2); } }

 결과 

123 -123

java.lang.String 클래스의 valueOf() 메소드를 사용하여도,

int를 String으로 변경할 수 있습니다.

 int + "" 

 코드 

public class IntToString { public static void main(String[] args) { int intValue1 = 123; int intValue2 = -123; String str1 = intValue1 + ""; String str2 = intValue2 + ""; System.out.println(str1); System.out.println(str2); } }

 결과 

123 -123

문자열에 int를 이어붙이면,

문자열이 리턴되는 속성을 이용한 방법입니다.

Java의 String을 int로, int를 String으로 변환하는 방법을 알아보았습니다.

Toplist

최신 우편물

태그