I am trying to make Cumulative GPA application. It contains 7 editTexts
and one textView
. One of them is for the number of the courses and the others are for the courses GPA. When I use it as for 6 courses it works but when I use it as example for 3 courses that I have to keep 3 of them empty the application crash.
public class MainActivity extends Activity {
EditText e1, e2, e3, e4, e5, e6, e7;
TextView t1;
Button b1, b2;
double a=0;
double b=0;
double c=0;
double d=0;
double e=0;
double f=0;
double g=0;
double h=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
e3=(EditText)findViewById(R.id.editText3);
e4=(EditText)findViewById(R.id.editText4);
e5=(EditText)findViewById(R.id.editText5);
e6=(EditText)findViewById(R.id.editText6);
e7=(EditText)findViewById(R.id.editText7);
t1=(TextView)findViewById(R.id.textView3);
b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
a=Double.parseDouble(e1.getText().toString());
b=Double.parseDouble(e2.getText().toString());
c=Double.parseDouble(e3.getText().toString());
d=Double.parseDouble(e4.getText().toString());
e=Double.parseDouble(e5.getText().toString());
f=Double.parseDouble(e6.getText().toString());
g=Double.parseDouble(e7.getText().toString());
h= ((a+b+c+d+e+f)/g);
t1.setText(Double.toString(h));
I tried this code:
{
if (g==3)
e4.setText("0");
e5.setText("0");
e6.setText("0");
}
but the same thing happened. Notice : e7 = the number of the courses
when you are keeping any one of edittext is empty your code will crash for eg: If your editext e1 is empty follwing code wont work as getText().toString() dont have any data
a=Double.parseDouble(e1.getText().toString());
you have to do like
if(!e1.getText().toString().equals(""))
{
a=Double.parseDouble(e1.getText().toString());
}
as for all.....
Modify your getText
lines like this:
String e1 = e1.getText().toString();
a= e1.equals("")?0:Double.parseDouble(e1);
That will prevent the NumberFormatException
that you must be getting now.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.