receive a string message from user input in android and parse to an int so can be stored in an array and compared with another array of integers

StackOverflow https://stackoverflow.com/questions/18440853

  •  26-06-2022
  •  | 
  •  

I am having a problem with the above task in my android application. I am accepting user input from the EditText widget in the form of String. I accepting numbers from the user so I have to parse them to integers so they can be compared with another array of integers. I have the line:

String message = editText.getText().toString()

then to try and parse the String to an int I have the code line:

int userNumbers = Integer.parseInt(message).

However when I attempt to compare the array userArray with the array numbers I am getting the error that "Incompatible operand types String and Integer.

Can anyone see where my problem is or how I can solve it? Here's my code: Thanks in advance.

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
有帮助吗?

解决方案

You have this

Integer [] numbers; // numbers is an integer array

You have string array

String [] userArray = new String[SIZE]; // userArray is a string array

You compare like below

if (userArray[loop] == numbers[loopOther])

So you get the error Incompatible operand types String and Integer.

try

if (Integer.parseInt(userArray[loop]) == numbers[loopOther])

Enclosing the above with try catch block

String message = editText.getText().toString();
try{
    int userNumbers = Integer.parseInt(message); 
     //is this right? yes
    }
    catch (NumberFormatException e)
    {
        e.printStacktrace();


    }

其他提示

Change String to Int here:

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

Parse Like this :

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;
                        }


            }
        }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top