1

I am an enthusiastic newbie when it comes to coding so would really appreciate any help with my problem. I am designing a little math game where it generates a random number less than 11 and the user then puts a number in the edit text field presses the button. The idea is if the random number and the user input number equal 10 the user gets a point and if they get it marked wrong, then another randomly generated number appears for the user to continue the process.

My problem is that it seems to work the first time, then when the user enters a number for the second time and presses the button it crashes. Please excuse what must seem like messy coding to you professionals but a lot of tutorials and trial and error to get this far.

    package com.example.mathtesttwo;



    public class MainActivity extends Activity {

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



    final TextView textOne = (TextView) findViewById(R.id.textView3);
    textOne.setText("4");
    final TextView guessText = (TextView) findViewById(R.id.textView2);
    final EditText userGuess = (EditText) findViewById(R.id.editText1);

    final TextView correctScore = (TextView) findViewById(R.id.textView1);

    final TextView wrongScore = (TextView) findViewById(R.id.textView4);
    wrongScore.setText("0");

    ////////////////////////


    String randText = "";

    Random randGen = new Random();
    int rando = randGen.nextInt(11);


    randText = Integer.toString(rando);
    textOne.setText(randText);
    /////////////////

    Button pushMe = (Button) findViewById(R.id.button1);

    pushMe.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


    int userNumber = Integer.parseInt(userGuess.getText().toString());
    int textNumber = Integer.parseInt(textOne.getText().toString());
    int highScore = Integer.parseInt(correctScore.getText().toString());
    int lowScore = Integer.parseInt(wrongScore.getText().toString());


            if ( userNumber== 10 - textNumber) {

                guessText.setText("you got it correct");
            correctScore.setText("correct:" +       highScore+1));




            } else {
                guessText.setText("WRONG");
                wrongScore.setText("wrong:" + (lowScore+1));

            }



            String randText = "";

            // TODO Auto-generated method stub
            Random randGen = new Random();
            int rando = randGen.nextInt(11);


            randText = Integer.toString(rando);
            textOne.setText(randText);
        }
    });


}

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

    }

LOGCAT

03-28 15:24:19.314: D/AndroidRuntime(959): Shutting down VM

03-28 15:24:19.314: W/dalvikvm(959): threadid=1: thread exiting with uncaught exception (group=0x414c4700)

03-28 15:24:19.484: E/AndroidRuntime(959): FATAL EXCEPTION: main

03-28 15:24:19.484: E/AndroidRuntime(959): java.lang.NumberFormatException: Invalid int: "correct:1"

03-28 15:24:19.484: E/AndroidRuntime(959):  at java.lang.Integer.invalidInt(Integer.java:138)

03-28 15:24:19.484: E/AndroidRuntime(959):  at java.lang.Integer.invalidInt(Integer.java:138)

03-28 15:24:19.484: E/AndroidRuntime(959):  at java.lang.Integer.parseInt(Integer.java:366)

03-28 15:24:19.484: E/AndroidRuntime(959):  at java.lang.Integer.parseInt(Integer.java:332)

03-28 15:24:19.484: E/AndroidRuntime(959):  at com.example.mathtesttwo.MainActivity$1.onClick(MainActivity.java:55)

03-28 15:24:19.484: E/AndroidRuntime(959):  at android.view.View.performClick(View.java:4240)

03-28 15:24:19.484: E/AndroidRuntime(959):  at android.view.View$PerformClick.run(View.java:17721)

03-28 15:24:19.484: E/AndroidRuntime(959):  at android.os.Handler.handleCallback(Handler.java:730)

03-28 15:24:19.484: E/AndroidRuntime(959):  at android.os.Handler.dispatchMessage(Handler.java:92)

03-28 15:24:19.484: E/AndroidRuntime(959):  at android.os.Looper.loop(Looper.java:137)

03-28 15:24:19.484: E/AndroidRuntime(959):  at android.app.ActivityThread.main(ActivityThread.java:5103)

03-28 15:24:19.484: E/AndroidRuntime(959):  at java.lang.reflect.Method.invokeNative(Native Method)

03-28 15:24:19.484: E/AndroidRuntime(959):  at java.lang.reflect.Method.invoke(Method.java:525)

03-28 15:24:19.484: E/AndroidRuntime(959):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

03-28 15:24:19.484: E/AndroidRuntime(959):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

03-28 15:24:19.484: E/AndroidRuntime(959):  at dalvik.system.NativeStart.main(Native Method)
3
  • 1
    Please post the stacktrace (crash report, also known as, logcat).
    – Emmanuel
    Mar 28, 2014 at 15:04
  • If you know how to read a logcat then do read it. Your error will be found else paste it here
    – Maverick
    Mar 28, 2014 at 15:27
  • I have added the Logcat to the post - Thanks
    – James
    Mar 28, 2014 at 15:37

1 Answer 1

0

Try this..

Remove below lines from OnClick

int highScore = Integer.parseInt(correctScore.getText().toString());
int lowScore = Integer.parseInt(wrongScore.getText().toString());

EDIT:

int highScore = 0;
if(correctScore.getText().toString().contains("correct") || correctScore.getText().toString().contains("wrong")){
       String[] separated = correctScore.getText().toString().split(":");
       highScore = Integer.parseInt(separated[1]);
}else{
       highScore = Integer.parseInt(correctScore.getText().toString());
}

int lowScore = 0;
if(wrongScore.getText().toString().contains("correct") || wrongScore.getText().toString().contains("wrong")){
       String[] separated = wrongScore.getText().toString().split(":");
       lowScore = Integer.parseInt(separated[1]);
}else{
       lowScore = Integer.parseInt(wrongScore.getText().toString());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.