0

I am trying to retrieve user input, store it in the persistent data. Then i want to use the data and calculate a loan amount. I know it has something to do with my double data types and i have searched and tried to no avail. What am i doing wrong?

Here is the code:

package com.example.s35505109;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {
String strID;
String strSurname;
String strAddress; 
String strGuardName;
String strGuardID; 
String strGender; 
String strOutcome;
String douLoanAmt;
String doumonInc;

double dou_monInc = 0;

double douRepayments = 0;

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


 // get inputed data from user


 //code the submit button 

  Button but1 = (Button)findViewById(R.id.buttSubmit);
  final SharedPreferences sharedPref = 
PreferenceManager.getDefaultSharedPreferences(this);
  but1.setOnClickListener(new OnClickListener(){

     public void onClick(View v){

     //This is where your code will go
    // retrieve data from input
         EditText ID = (EditText)findViewById(R.id.editTextID);
         EditText surname = (EditText)findViewById(R.id.editTextSurname);
         Spinner gender = (Spinner)findViewById(R.id.txtGender);
         EditText loanAmt = (EditText)findViewById(R.id.editTextLoanAmt);
         EditText address = (EditText)findViewById(R.id.editTextAddress);
         EditText guardname = (EditText)findViewById(R.id.editTextGuarName);
         EditText guardID = (EditText)findViewById(R.id.editTextGuarID);
         EditText monInc = (EditText)findViewById(R.id.editTextMoInc);          
         TextView repayments = (TextView)findViewById(R.id.textViewRepayments);
         TextView outcome = (TextView)findViewById(R.id.textView1);

         strID = ID.getText().toString();
         strSurname = surname.getText().toString();
         strGender = gender.getSelectedItem().toString();
         douLoanAmt = loanAmt.getText().toString();
         if (!TextUtils.isEmpty(douLoanAmt)) {
             // call parseDouble in here
             double dou_LoanAmt = Double.parseDouble(douLoanAmt);
         }           
         strAddress = address.getText().toString();
         strGuardName = guardname.getText().toString();
         strGuardID = guardID.getText().toString();
         doumonInc = monInc.getText().toString();
         if (!TextUtils.isEmpty(doumonInc)) {
             // call parseDouble in here
             double dou_monInc = Double.parseDouble(doumonInc);
         }           


        if (dou_monInc < 5000 ){
             outcome.setText("You do not qualify for a loan");
         ;}
        else if (dou_monInc>5000 && dou_monInc<10000 ){
             outcome.setText("You qualify for a maximum loan of R40,000 at 15% interest");}
        else if (dou_monInc>10000 && dou_monInc<30000 ){
             outcome.setText("You qualify for a maximum loan of R70,000 at 16.5% interest");}
        else if (dou_monInc>30000 ){
             outcome.setText("You qualify for a maximum loan of R70,000 at 16.5% interest");}
        else {outcome.setText("");}     


         douRepayments = Float.parseFloat(repayments.getText().toString());
         SharedPreferences.Editor editor = sharedPref.edit();
         editor.putString("key1", strID);
         editor.putString("key2", strSurname);
         editor.putString("key3", strGender);
         editor.putString("key4", strAddress);
         editor.putString("key5", strGuardName);
         editor.putString("key6", strGuardID);
         editor.putString("key7", strOutcome);
         //editor.putFloat("key8", douLoanAmt);
         //editor.putFloat("key9", doumonInc);
         //editor.putFloat("key10", douRepayments);
         editor.commit();
         }
   });  //end but1

}



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

}

Here is the stack trace:

04-23 08:17:10.178: E/AndroidRuntime(2690): in writeCrashedAppName, pkgName 
:com.example.s35505109
04-23 08:17:10.228: E/AndroidRuntime(2690): FATAL EXCEPTION: main
04-23 08:17:10.228: E/AndroidRuntime(2690): Process: com.example.s35505109, 
PID: 2690
04-23 08:17:10.228: E/AndroidRuntime(2690): java.lang.NumberFormatException: 
Invalid float: "Repayments:"
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
java.lang.StringToReal.invalidReal(StringToReal.java:63)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
java.lang.StringToReal.initialParse(StringToReal.java:114)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
java.lang.StringToReal.parseFloat(StringToReal.java:304)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
java.lang.Float.parseFloat(Float.java:300)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
com.example.s35505109.MainActivity$1.onClick(MainActivity.java:91)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
android.view.View.performClick(View.java:4560)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
android.view.View$PerformClick.run(View.java:18636)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
android.os.Handler.handleCallback(Handler.java:733)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
android.os.Handler.dispatchMessage(Handler.java:95)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
android.os.Looper.loop(Looper.java:136)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
android.app.ActivityThread.main(ActivityThread.java:5021)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
java.lang.reflect.Method.invokeNative(Native Method)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
java.lang.reflect.Method.invoke(Method.java:515)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:827)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
dalvik.system.NativeStart.main(Native Method)
  • Do you know where it crashes and with what error – harold Apr 22 '17 at 19:44
  • When i click submit it crashes. i have debugged and commented out the doubles and it works. but then when they are put back it crashes. – clint archer Apr 22 '17 at 20:01
  • Check [the stack trace](http://stackoverflow.com/questions/23353173) to determine the cause of the crash. If you need assistance in reading and interpreting it, [edit] your question to post that. – Mike M. Apr 23 '17 at 01:25
  • I added my stack trace. I know it must have something to do with null values but i have searched and tried various methods, none of which have stopped the app from crashing. – clint archer Apr 23 '17 at 06:50

1 Answers1

0

App crashes because of entered value can not be parsed into double.

04-23 08:17:10.228: E/AndroidRuntime(2690): java.lang.NumberFormatException: 
Invalid float: "Repayments:"

Stack trace is showing the error, you have entered word "repayments" and obviously it can't be parsed into double.

You should use a EditText with type numberDecimal|numberSigned as follows, then user is presented with numeric keypad and then it becomes less error prone.

<EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal|numberSigned"
        android:text="Name"
        tools:layout_editor_absoluteX="84dp"
        tools:layout_editor_absoluteY="103dp" />

Then you need to handle the NumberFormatException as Follows.

if (!TextUtils.isEmpty(douLoanAmt)) {
            try {
                // call parseDouble in here
                double dou_LoanAmt = Double.parseDouble(douLoanAmt);
                // it means it is double, process
            } catch (Exception e1) {
                // this means it is not double 
                // show error message
                e1.printStackTrace();
            }
        }
Ruwanka De Silva
  • 3,555
  • 6
  • 35
  • 51