0

I'm having some difficulties with my android application.

Heres my code:

package activities;

import java.util.Vector;

import coursework.prototype.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class game_calculator extends Activity {

double currentTotal = 0.0; //Total of current calculation
String currentText = "0"; //Text displayed in the TextView
TextView Calculator_Display;
Vector<String> currentCalculations = new Vector<String>(); //Set up a vector to hold calculations

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_calculator);
    
    Calculator_Display = (TextView) findViewById(R.id.display); //Locate the textview
    Calculator_Display.setText( currentText ); //Set the text to default value
}

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

// MY CODE STARTS HERE

//Go Back button on click event
//Sets the current activity to the main menu
public void GoToMenu(View v)
{
    startActivity( new Intent(this, game_selection.class) ); 
}

//Adds the number to the current display
public void CalculatorButton(View v)
{       
    //Get the buttons text
    String buttons_text = ((Button) v).getText().toString();
    
    //If no number has been inputed (screen will show 0)
    if(currentText == "0")
    {
        currentText = buttons_text;
    }
    else //Add the number to the current display (after previous entered numbers)
    {
        currentText = currentText + buttons_text;
    }
    Calculator_Display.setText(currentText); //Set the display to the inputed number
}

//Performs the calculation with the current total and the display number
public void CalculatorFunction(View button)
{
    switch( button.getId() )
    {
    case R.id.calculator_equals: //display the current total
        
        currentCalculations.add( currentText ); //Add current number into the vector
        
        if(currentCalculations.size() > 0) //Only process calculations if there's elements to process
            CalculateTotal();

        currentText = "0"; //Reset display
        break;
    case R.id.calculator_reset:
        currentText = "0"; //Reset stored display
        currentTotal = 0; //Reset stored total
        currentCalculations.clear();
        break;
    case R.id.calculator_backspace:
        //Delete the last entered number
        if (currentText.length() > 0 && currentText.charAt(currentText.length()-1)=='0') {
            currentText = currentText.substring(0, currentText.length()-1);
          }
        break;
    default:
        //Convert the display to an integer to be worked with           
        currentCalculations.add( currentText ); //Add current number into the vector
        currentCalculations.add( ((Button) button).getText().toString() ); //Add the type of calculation to perform
        
        currentText = "0"; //Reset display number
        break;
    }
    
    Calculator_Display.setText( currentText ); //Set the new display
}

public void CalculateTotal()
{
    //Set up variables
    String currentElement; //Temporary string storage
    String currentSign = " "; //Temporary sign storage
    double currentNumber = 0; //Temporary number storage
    Log.i("INFORMATION", "<=================== MY LOGS START HERE ===================>" );
    Log.i("INFORMATION", "Size: " + Integer.toString(currentCalculations.size()) );
    
    //Start processing
    for(int c = 0; c <= currentCalculations.size(); c++) //Loops through elements to process
    {
        currentElement = currentCalculations.get( c ); //Get element from array
        Log.i("INFORMATION", "Element " + Integer.toString(c) + " : " + currentElement );
        
        //Check if the element is a sign
        if(currentElement == "+" || currentElement == "-" || currentElement == "*" || currentElement == "/")
        {
            Log.i("INFORMATION", "Sign found!");
            currentSign = currentElement; //Save into temporary storage to be used next loop
        }
        else // Element is a number
        {
            currentNumber = Double.parseDouble( currentElement ); //Convert number to integer
            
            if(currentSign == "+") 
                currentTotal += currentNumber;              
            else if(currentSign == "-") 
                currentTotal -= currentNumber;
            else if(currentSign == "*") 
                currentTotal = currentTotal * currentNumber;
            else if(currentSign == "/")
                currentTotal = currentTotal / currentNumber;
            else
                Log.i("INFORMATION", "THE PROGRAMS FUCKED UP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        }
            
    }
    Calculator_Display.setText( Double.toString( currentTotal ) ); //Display the currentTotal

    Log.i("INFORMATION", "<=================== MY LOGS END HERE ===================>" );
}
}

The app crashes when I hit the equals button so I know the error is with CalculateTotal but am not sure why its crashing. If I do 7 + 8, the Logcat shows:

11-13 19:10:43.073: I/INFORMATION(1408): <=================== MY LOGS START HERE ===================>
11-13 19:10:43.073: I/INFORMATION(1408): Size: 5
11-13 19:10:43.114: I/INFORMATION(1408): Element 0 : 7
11-13 19:10:43.114: I/INFORMATION(1408): Element 1 : +
11-13 19:10:43.244: W/dalvikvm(1408): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
11-13 19:10:43.476: E/AndroidRuntime(1408): FATAL EXCEPTION: main
11-13 19:10:43.476: E/AndroidRuntime(1408): java.lang.IllegalStateException: Could not execute method of the activity
11-13 19:10:43.476: E/AndroidRuntime(1408):     at android.view.View$1.onClick(View.java:3633)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at android.view.View.performClick(View.java:4240)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at android.view.View$PerformClick.run(View.java:17721)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at android.os.Handler.handleCallback(Handler.java:730)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at android.os.Looper.loop(Looper.java:137)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at java.lang.reflect.Method.invoke(Method.java:525)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at dalvik.system.NativeStart.main(Native Method)
11-13 19:10:43.476: E/AndroidRuntime(1408): Caused by: java.lang.reflect.InvocationTargetException
11-13 19:10:43.476: E/AndroidRuntime(1408):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at java.lang.reflect.Method.invoke(Method.java:525)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at android.view.View$1.onClick(View.java:3628)
11-13 19:10:43.476: E/AndroidRuntime(1408):     ... 11 more
11-13 19:10:43.476: E/AndroidRuntime(1408): Caused by: java.lang.NumberFormatException: Invalid double: "+"
11-13 19:10:43.476: E/AndroidRuntime(1408):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at java.lang.StringToReal.initialParse(StringToReal.java:151)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at java.lang.StringToReal.parseDouble(StringToReal.java:263)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at java.lang.Double.parseDouble(Double.java:295)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at activities.game_calculator.CalculateTotal(game_calculator.java:127)
11-13 19:10:43.476: E/AndroidRuntime(1408):     at activities.game_calculator.CalculatorFunction(game_calculator.java:76)
11-13 19:10:43.476: E/AndroidRuntime(1408):     ... 14 more
11-13 19:14:16.423: I/Process(1408): Sending signal. PID: 1408 SIG: 9
11-13 19:14:18.223: I/dalvikvm-heap(1446): Grow heap (frag case) to 4.082MB for 1536016-byte allocation
11-13 19:20:14.522: I/dalvikvm-heap(1545): Grow heap (frag case) to 4.082MB for 1536016-byte allocation
11-13 19:20:20.124: I/dalvikvm-heap(1545): Grow heap (frag case) to 5.822MB for 1536016-byte allocation
11-13 19:20:29.415: I/dalvikvm-heap(1545): Grow heap (frag case) to 7.856MB for 1536016-byte allocation
11-13 19:20:32.132: I/Choreographer(1545): Skipped 31 frames!  The application may be doing too much work on its main thread.
11-13 19:20:43.032: I/INFORMATION(1545): <=================== MY LOGS START HERE ===================>
11-13 19:20:43.032: I/INFORMATION(1545): Size: 3
11-13 19:20:43.122: I/INFORMATION(1545): Element 0 : 7
11-13 19:20:43.162: I/INFORMATION(1545): THE PROGRAMS FUCKED UP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
11-13 19:20:43.172: I/INFORMATION(1545): Element 1 : +
11-13 19:20:43.224: W/dalvikvm(1545): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
11-13 19:20:43.292: E/AndroidRuntime(1545): FATAL EXCEPTION: main
11-13 19:20:43.292: E/AndroidRuntime(1545): java.lang.IllegalStateException: Could not execute method of the activity
11-13 19:20:43.292: E/AndroidRuntime(1545):     at android.view.View$1.onClick(View.java:3633)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at android.view.View.performClick(View.java:4240)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at android.view.View$PerformClick.run(View.java:17721)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at android.os.Handler.handleCallback(Handler.java:730)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at android.os.Looper.loop(Looper.java:137)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at java.lang.reflect.Method.invoke(Method.java:525)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at dalvik.system.NativeStart.main(Native Method)
11-13 19:20:43.292: E/AndroidRuntime(1545): Caused by: java.lang.reflect.InvocationTargetException
11-13 19:20:43.292: E/AndroidRuntime(1545):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at java.lang.reflect.Method.invoke(Method.java:525)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at android.view.View$1.onClick(View.java:3628)
11-13 19:20:43.292: E/AndroidRuntime(1545):     ... 11 more
11-13 19:20:43.292: E/AndroidRuntime(1545): Caused by: java.lang.NumberFormatException: Invalid double: "+"
11-13 19:20:43.292: E/AndroidRuntime(1545):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at java.lang.StringToReal.initialParse(StringToReal.java:151)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at java.lang.StringToReal.parseDouble(StringToReal.java:263)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at java.lang.Double.parseDouble(Double.java:295)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at activities.game_calculator.CalculateTotal(game_calculator.java:126)
11-13 19:20:43.292: E/AndroidRuntime(1545):     at activities.game_calculator.CalculatorFunction(game_calculator.java:76)
11-13 19:20:43.292: E/AndroidRuntime(1545):     ... 14 more
11-13 19:25:43.513: I/Process(1545): Sending signal. PID: 1545 SIG: 9
11-13 19:25:44.963: I/dalvikvm-heap(1577): Grow heap (frag case) to 4.082MB for 1536016-byte allocation
General Grievance
  • 4,555
  • 31
  • 31
  • 45
user2990037
  • 397
  • 5
  • 12

3 Answers3

3

never ever ever use == to check equality for strings in Java. you must always use

string1.equals(string2)

the line

if (currentString == "+") 

will always return false, so you must use

if (currentString.equals("+")) 
panini
  • 2,026
  • 19
  • 21
0

The problem is that you do Integer.toString(c) before you check to see if c is numeric.

Also, be aware that you shouldn't compare strings with == you must use .equals(), or you will get unexpected results.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
0

I think the problem is that your IF returns "false" in this fragment:

    //Check if the element is a sign
    if(currentElement == "+" || currentElement == "-" || currentElement == "*" || currentElement == "/")
    {
        Log.i("INFORMATION", "Sign found!");
        currentSign = currentElement; //Save into temporary storage to be used next loop
    }
    else // Element is a number
    {
        currentNumber = Double.parseDouble( currentElement ); //Convert number to integer
    }

So you get fatal error when you try to execute Double.parseDouble(currentElement) with currentElement="+".

I would use string comparison instead of "==", like this:

if("+".equals(currentElement) || "-".equals(currentElement) ... etc