我试图以字符串消息的形式接收用户输入,并将其转换(解析)为整数,以存储在Android中的数组中
在我的android彩票应用程序中,我正在尝试以表示数字的字符串形式接收用户输入。然后我将这个用户输入解析为整数,并存储在一个数组中,与另一个整数数组进行比较。但是,我遇到了以下问题,因为我得到了一个类型不匹配:无法从int转换为字符串。我已经创建了一个显示用户消息的活动,但我希望它存储在数组中,这将是他们的彩票号码。我有一个按钮链接到这个新的“显示数字活动”。我创建了一个全局变量“static String[]userNumbers=new String[SIZE],并将常量SIZE设置为=6 我在将字符串解析为int的代码部分遇到不匹配错误,在设置数组的for循环中遇到错误。希望有人能帮助我解决此问题。提前感谢 我的代码如下:我试图以字符串消息的形式接收用户输入,并将其转换(解析)为整数,以存储在Android中的数组中,android,Android,在我的android彩票应用程序中,我正在尝试以表示数字的字符串形式接收用户输入。然后我将这个用户输入解析为整数,并存储在一个数组中,与另一个整数数组进行比较。但是,我遇到了以下问题,因为我得到了一个类型不匹配:无法从int转换为字符串。我已经创建了一个显示用户消息的活动,但我希望它存储在数组中,这将是他们的彩票号码。我有一个按钮链接到这个新的“显示数字活动”。我创建了一个全局变量“static String[]userNumbers=new String[SIZE],并将常量SIZE设置为=6
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = ".com.example.lotterychecker.MESSAGE";
static boolean bonus = false;
static boolean jackpot = false;
static int lottCount = 0;
final static int SIZE =6;
static String [] userNumbers = new String[SIZE];
Button check;
//...some code for parsing html......
public void checkNumbers(View view) {
//create an intent to display the numbers
Intent intent = new Intent(this, DisplayNumbersActivity.class);
EditText editText = (EditText) findViewById(R.id.enter_numbers);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message );
startActivity(intent);
String userNumbers = editText.getText().toString();
userNumbers = Integer.parseInt(message); //mismatch error here
Toast.makeText(MainActivity.this, "Here are your numbers", Toast.LENGTH_LONG).show();
for (int count =0; count < SIZE; count ++)
{
if (check.isPressed())
{
userNumbers[count] = editText.getText().toString(); //error "The type of the expression must be an array type
//but it resolved to String" in the userNumbers[count] syntax
}
}//for
}
public class DisplayNumbersActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_numbers);
// Show the Up button in the action bar.
setupActionBar();
//get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
//set the text view as the activity layout
setContentView(textView);
}
公共类MainActivity扩展活动{
公共最终静态字符串EXTRA_MESSAGE=“.com.example.lotterychecker.MESSAGE”;
静态布尔加值=false;
静态布尔jackpot=false;
静态计数=0;
最终静态整数大小=6;
静态字符串[]userNumbers=新字符串[大小];
按钮检查;
//…一些解析html的代码。。。。。。
公共作废支票号码(查看){
//创建显示数字的意图
Intent Intent=新Intent(这是DisplayNumbersActivity.class);
EditText EditText=(EditText)findViewById(R.id.输入_编号);
String message=editText.getText().toString();
intent.putExtra(额外消息,消息);
星触觉(意向);
字符串userNumbers=editText.getText().toString();
userNumbers=Integer.parseInt(message);//此处出现不匹配错误
Toast.makeText(MainActivity.this,“这是您的数字”,Toast.LENGTH_LONG.show();
对于(int count=0;count
在这行中
String userNumbers = editText.getText().toString();
您定义了一个字符串类型的局部变量userNumbers
。此定义使类字段userNumbers
成为不可访问的数组
您还尝试在字符串变量中存储int。这是类型不匹配。在出现不匹配错误之前,您是否查看了
消息
的实际内容?请发布StackTrace他是对的。Srting userNumbers=…not”下的所有内容都可以看到“不再是数组了。所以你真正要做的是尝试将字符串写入一个char变量——这是不可能的。你所要做的就是更改两个变量之一的名称。除此之外,@SuppressLint(“NewApi”)通常也是一个坏主意。它还应该是int numbervalue=Integer.parseInt(message)
并应包含一个try-catch块