문자열을 정수로 변환하려면 어떻게 하나요?
사용자가 숫자를 입력하도록 하는 텍스트 상자가 있습니다:
EditText et = (EditText) findViewById(R.id.entry1);
String hello = et.getText().toString();
그리고 이 값은 문자열 hello
에 할당됩니다.
나중에 코드에서 사용할 수 있도록 정수로 변환하여 사용자가 입력한 숫자를 얻고 싶습니다.
편집 텍스트`를 정수로 가져올 수 있는 방법이 있나요? 그러면 중개자를 건너뛸 수 있습니다. 그렇지 않다면 문자열을 정수로 변환해도 괜찮습니다.
Integer 클래스와 정적 parseInt()
메서드를 참조하세요:
http://developer.android.com/reference/java/lang/Integer.html
Integer.parseInt(et.getText().toString());
구문 분석 중 문제가 발생할 경우 NumberFormatException
을 잡아야 합니다:
int myNum = 0;
try {
myNum = Integer.parseInt(et.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
String s="your1string2contain3with4number";
int i=Integer.parseInt(s.replaceAll("[\\D]", ""));
출력: i = 1234년;
String s="abc123xyz456";
int i=NumberFormat.getInstance().parse(s).intValue();
출력: i = 123.
이 코드를 시도하시겠습니까 it& # 39 의 상행담관염이라고 작업 중.
int number = 0;
try {
number = Integer.parseInt(YourEditTextName.getText().toString());
} catch(NumberFormatException e) {
System.out.println("parse value is not valid : " + e);
}
정수 다음 문자열 구문 분석을 위해 사용할 수 있습니다.
, int value = 안테거스파르세인트 (테스트비우스게테스트 네스토스트링 () ())
(2) input:* " abdul"; 즉 누마버포매렉세페시옹 어졌다면 예외가 던져질 것이다. 우리는 이 문제를 해결할 수 있도록 as I catch 시도하시겠습니까 언급하십시오 사용할 필요가 있다 (아래 참조).
int tax_amount=20;
EditText edit=(EditText)findViewById(R.id.editText1);
try
{
int value=Integer.parseInt(edit.getText().toString());
value=value+tax_amount;
edit.setText(String.valueOf(value));// to convert integer to string
}catch(NumberFormatException ee){
Log.e(ee.toString());
}
자세한 내용은 아래 링크를 참조할 수도 있습니다. , http://developer.android.com/reference/java/lang/Integer.html
문자열을 플로트로 숨겨야 합니다. 작동 중입니다.
float result = 0;
if (TextUtils.isEmpty(et.getText().toString()) {
return;
}
result = Float.parseFloat(et.getText().toString());
tv.setText(result);
한 줄로 할 수도 있습니다.
int hello = Integer.parseInt(((Button)findViewById(R.id.button1)).getText().toString().replaceAll("[\\D]", ""));
판독값 오더할 에서 실행
구문 분석 방법을 사용할 수 있는데 그 확장명은 쉽게 다른 프리미티브 유형.
[1]: -string/to-boolean.html https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/kotlin [2]: -string/to-float.html https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/kotlin [3]: -string/to-double.html https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/kotlin [4]: -string/to-byte.html https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/kotlin [5]: -string/to-short.html https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/kotlin [6]: -string/to-int.html https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/kotlin [7]: -string/to-long.html https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/kotlin
String num = "10";
Integer.parseInt(num );
5 가지 방법으로 변환할지 첫 번째 방식:
String str = " 123" ;
int i = Integer.parse(str);
output : 123
두 번째 방법은.
String str = "hello123world";
int i = Integer.parse(str.replaceAll("[\\D]" , "" ) );
output : 123
제 3 의 길:
String str"123";
int i = new Integer(str);
output "123
네 번째 방식:
String str"123";
int i = Integer.valueOf(Str);
output "123
다섯 번째 방식:
String str"123";
int i = Integer.decode(str);
output "123
다른 방법이 있을 수 있습니다. 하지만 이제 s what I remember that& # 39.