Android:将String转换为int [英] Android: converting String to int

查看:1753
本文介绍了Android:将String转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是尝试将从条形码扫描器生成的字符串转换为int,以便我可以通过获取余数来生成一组整数来操纵它。到目前为止,我已经尝试过:

I'm simply trying to convert a string that is generated from a barcode scanner to an int so that I can manipulate it by taking getting the remainder to generate a set number of integers. So far I have tried:

int myNum = 0;

try {
    myNum = Integer.parseInt(myString.getText().toString());
} catch(NumberFormatException nfe) {

} 

Integer.valueOf(mystr);

int value = Integer.parseInt(string); 

第一个给出错误:方法getText()未定义类型String
而后两个没有任何编译错误但是应用程序在调用时会立即崩溃。我认为这与我的条形码扫描意图方法有关,但我把它放入OnCreate但仍然出错。

The first one gives me the error :The method getText() is undefined for the type String while the last two don't have any compile errors but the app crashes immediately when those are called. I thought it had to do with my barcode scanning intent method but I put it into the OnCreate and still got the error.

推荐答案

更改

try {
    myNum = Integer.parseInt(myString.getText().toString());
} catch(NumberFormatException nfe) {

try {
    myNum = Integer.parseInt(myString);
} catch(NumberFormatException nfe) {

这篇关于Android:将String转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆