내가 사용하고 다음 문자열의 정수를 얻을 수있는 다른 방법이 Java에서 문자열에서 정수를 가져 오는 방법은 무엇입니까?
int i = Integer.parseInt(args[2]);
이 있습니까? 숫자가 정말로 작 으면 Byte와 Char 객체가 비슷한 것을 제공합니까?
내가 사용하고 다음 문자열의 정수를 얻을 수있는 다른 방법이 Java에서 문자열에서 정수를 가져 오는 방법은 무엇입니까?
int i = Integer.parseInt(args[2]);
이 있습니까? 숫자가 정말로 작 으면 Byte와 Char 객체가 비슷한 것을 제공합니까?
예.
Byte.parseByte(s); -- parses a Byte from a String
Short.parseShort(s); -- parses a Short from a String
그리고 더 큰 숫자있다 :있다
Long.parseLong(s);
-- Float is an imprecise representation of a floating point number using 32 bits
Float.parseFloat(s);
-- Double is an imprecise representation of a floating point number using 64 bits
Double.parseDouble(s);
-- BigIntegers is an integer of arbitrary size as is accurate
new BigInteger(s);
-- BigDecimal is a floating point number of arbitrary size as is accurate
new BigDecimal(s);
예, 더 작은 정수 값을 구문 분석하려면 Short.parseShort(String) 및 Byte.parseByte(String) 래퍼 메서드를 사용할 수 있습니다.
다른 방법은 String
의 정수를 얻을 :
String value = "2";
int i = Integer.valueOf(value);
System.out.println("i = " + i);
Scanner scanner = new Scanner(value);
i = scanner.nextInt();
System.out.println("i = " + i);
또한 그렇게 시도의 catch 블록에서 그 포장한다 정수가 아닌 값을 전달하려고하면 코드가 파열되지 않습니다.
다음 오류가 발생합니다. int i4 = new BigInteger (args [3]); 타입 불일치 : BigInteger에서 int로 변환 할 수 없다 –
'BigInteger'에서'int'를 얻으려면'.intValue()'를 사용하십시오 :'i4 = new BigInteger (s) .intValue();'. BigInteger 클래스는 임의로 큰 숫자에 대해 정확한 정수 연산을 수행하기위한 클래스입니다. 그러한 산술을위한 특별한 방법을 가지고 있습니다. – Bohemian