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)