我试图在我的彩票安卓应用程序中接收用户输入作为代表数字的字符串。然后,我将此用户输入解析为整数并存储在一个数组中,以便与另一个整数数组进行比较。但是,我遇到了以下问题,因为我遇到了类型不匹配:无法从 int 转换为 String。我创建了一个显示用户消息的活动,但我希望将其存储在数组中,这将是他们的彩票号码。我有一个链接到这个新的“显示数字活动”的按钮。我创建了一个全局变量“static String [] userNumbers = new String[SIZE] 并将常量大小设置为 = 6。
我在将 String 解析为 int 的代码部分中遇到不匹配错误,在用于设置数组的 for 循环中遇到错误。希望有人能帮我解决这个问题。提前致谢!
我的代码如下:
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);
}