Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从android中的用户输入接收字符串消息,并解析为整数,这样就可以存储在一个数组中,并与另一个整数数组进行比较_Android_Arrays_String Parsing - Fatal编程技术网

从android中的用户输入接收字符串消息,并解析为整数,这样就可以存储在一个数组中,并与另一个整数数组进行比较

从android中的用户输入接收字符串消息,并解析为整数,这样就可以存储在一个数组中,并与另一个整数数组进行比较,android,arrays,string-parsing,Android,Arrays,String Parsing,我的android应用程序中的上述任务有问题。我正在接受来自EditText小部件的字符串形式的用户输入。我接受来自用户的数字,因此我必须将它们解析为整数,以便可以将它们与另一个整数数组进行比较。我有一句话: String message = editText.getText().toString() 然后,要尝试将字符串解析为int,我有以下代码行: int userNumbers=Integer.parseInt(消息)。 但是,当我尝试将数组userArray与数组编号进行比较时,我得到

我的android应用程序中的上述任务有问题。我正在接受来自
EditText
小部件的字符串形式的用户输入。我接受来自用户的数字,因此我必须将它们解析为整数,以便可以将它们与另一个整数数组进行比较。我有一句话:

String message = editText.getText().toString()
然后,要尝试将字符串解析为int,我有以下代码行:

int userNumbers=Integer.parseInt(消息)。

但是,当我尝试将数组userArray与数组编号进行比较时,我得到的错误是“不兼容的操作数类型为String和Integer”

有人能看到我的问题在哪里或者我如何解决它吗?这是我的代码: 提前谢谢

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;
    Button check;
    Integer [] numbers;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //link to the intended web site and get the lottery numbers while the app is opening 
        try {
            Document doc = Jsoup.connect("http://www.national-lottery.co.uk/player/p/drawHistory.do").userAgent("Mozilla").get();

            Elements elements = doc.getElementsByClass("drawhistory");
            Element table = elements.first();
            Element tbody = table.getElementsByTag("tbody").first();

            Element firstLottoRow = tbody.getElementsByClass("lottorow").first();

            Element dateElement = firstLottoRow.child(0);
            System.out.println(dateElement.text());

            Element gameElement = firstLottoRow.child(1);
            System.out.println(gameElement.text());

            Element noElement = firstLottoRow.child(2);
            System.out.println(noElement.text());

            String [] split = noElement.text().split(" - ");
            // set up an array to store numbers from the latest draw on the lottery web page
            Integer [] numbers = new Integer [split.length];

            int i = 0;
            for (String strNo : split) {
                numbers [i] = Integer.valueOf(strNo);
                i++;
            }

            for (Integer no : numbers) { 
                System.out.println(no);
            }

            Element bonusElement = firstLottoRow.child(3);
            Integer bonusBall = Integer.valueOf(bonusElement.text());
            System.out.println("Bonus ball: " + bonusBall);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //called when the user clicks the send button
    public void checkNumbers(View view) {
        final int SIZE =6; 
        String [] userArray = new String[SIZE];
        //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);

        //parse string message to an int for user numbers
        try{
        int userNumbers = Integer.parseInt(message); //is this right?
        }//try
        catch (NumberFormatException e)
        {
            System.out.println("Not a number" + e.getMessage());
        }
        Toast.makeText(MainActivity.this, "Here are your numbers", Toast.LENGTH_LONG).show();

        for (int count =0; count < SIZE; count ++)
        {
            if (check.isPressed())
            {
                userArray[count] = editText.getText().toString(); 
            }
        }//for

        //compare the two arrays of integers
        for (int loop = 0; loop < userArray.length; loop++)
        {
            for (int loopOther = 0; loopOther < numbers.length; loopOther++)
            {
                if (userArray[loop] == numbers[loopOther])  //how do I parse this?
                {
                    lottCount++;
                }else if (userArray[loop] == bonus)
                        {
                            bonus = true;
                        }


            }//for
        }//for main
公共类MainActivity扩展活动{
公共最终静态字符串EXTRA_MESSAGE=“.com.example.lotterychecker.MESSAGE”;
静态布尔加值=false;
静态布尔jackpot=false;
静态计数=0;
按钮检查;
整数[]个数;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//链接到预期的网站,并在应用程序打开时获取彩票号码
试一试{
Document doc=Jsoup.connect(“http://www.national-lottery.co.uk/player/p/drawHistory.douserAgent(“Mozilla”).get();
Elements=doc.getElementsByClass(“drawhistory”);
元素表=elements.first();
元素tbody=table.getElementsByTag(“tbody”).first();
元素firstLottoRow=tbody.getElementsByClass(“lottorow”).first();
元素dateElement=firstLottoRow.child(0);
System.out.println(dateElement.text());
元素gameElement=firstLottoRow.child(1);
System.out.println(gameElement.text());
元素noElement=firstLottoRow.child(2);
System.out.println(noElement.text());
String[]split=noElement.text().split(“-”);
//设置一个数组来存储彩票网页上最新抽奖的号码
整数[]个数=新整数[split.length];
int i=0;
for(字符串strNo:split){
数字[i]=整数.valueOf(strNo);
i++;
}
对于(整数编号:数字){
系统输出打印项次(否);
}
元素bonusElement=firstLottoRow.child(3);
Integer-bonusBall=Integer.valueOf(bonusElement.text());
System.out.println(“奖金球:+bonusBall”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
//当用户单击“发送”按钮时调用
公共作废支票号码(查看){
最终整数大小=6;
String[]userArray=新字符串[大小];
//创建显示数字的意图
Intent Intent=新Intent(这是DisplayNumbersActivity.class);
EditText EditText=(EditText)findViewById(R.id.输入_编号);
String message=editText.getText().toString();
intent.putExtra(额外消息,消息);
星触觉(意向);
//将字符串消息解析为用户编号的整数
试一试{
int userNumbers=Integer.parseInt(message);//是否正确?
}//试一试
捕获(数字格式)
{
System.out.println(“不是数字”+e.getMessage());
}
Toast.makeText(MainActivity.this,“这是您的数字”,Toast.LENGTH_LONG.show();
对于(int count=0;count
在此处将字符串更改为Int:

for (int loop = 0; loop < userArray.length; loop++)
        {
            for (int loopOther = 0; loopOther < numbers.length; loopOther++)
            {
                if (Integer.valueOf(userArray[loop]) == numbers[loopOther])  //how do I parse this?
                {
                    lottCount++;
                }else if (Integer.valueOf(userArray[loop]) == bonus)
                        {
                            bonus = true;
                        }


        }//for
}//for main
for(int-loop=0;loop
像这样解析:

for (int loop = 0; loop < userArray.length; loop++)
        {
            for (int loopOther = 0; loopOther < numbers.length; loopOther++)
            {
                if (Integer.parseInt(userArray[loop]) == numbers[loopOther]) 
                {
                    lottCount++;
                }else if (userArray[loop] == bonus)
                        {
                            bonus = true;
                        }


            }
        }
for(int-loop=0;loop
您有这个

Integer [] numbers; // numbers is an integer array
你有一个字符串数组

String [] userArray = new String[SIZE]; // userArray is a string array
你像下面这样比较

if (userArray[loop] == numbers[loopOther])
因此,您得到的错误是操作数类型String和Integer不兼容

试一试

用try-ca封装上述内容
if (Integer.parseInt(userArray[loop]) == numbers[loopOther])
String message = editText.getText().toString();
try{
    int userNumbers = Integer.parseInt(message); 
     //is this right? yes
    }
    catch (NumberFormatException e)
    {
        e.printStacktrace();


    }