I've run into a simple error with the app. How do I get a user input that is an integer to work with editText field. I have the editText input type set as "number".
EditText M1;
EditText M2;
EditText A1;
EditText A2;
Button answer;
TextView Resultant;
TextView Angle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vector_addition);
M1 = (EditText) findViewById(R.id.Direction1);
M2 = (EditText) findViewById(R.id.Direction2);
A1 = (EditText) findViewById(R.id.Theta1);
A2 = (EditText) findViewById(R.id.Theta2);
Resultant = (TextView) findViewById(R.id.MagnitudeAnswer);
double Vector1X;
double Vector1Y;
double Vector2X;
double Vector2Y;
double ResultantX;
double ResultantY;
double ResultantT;
double Magnitude;
//Double.parseDouble(M1.getText().toString());
//Double.parseDouble(String.valueOf(M1));
double D1 = Double.parseDouble(M1.getText().toString());
double D2 = Double.parseDouble(M2.getText().toString());
double angle1 = Double.parseDouble(A1.getText().toString());
double angle2 = Double.parseDouble(A2.getText().toString());
Vector1X = D1 * Math.cos(Math.toRadians(angle1));
Vector1Y = D1 * Math.sin(Math.toRadians(angle1));
Vector2X = D2 * Math.cos(Math.toRadians(angle2));
Vector2Y = D2 * Math.sin(Math.toRadians(angle2));
ResultantX = Vector1X Vector2X;
ResultantY = Vector1Y Vector2Y;
ResultantT = (ResultantX * ResultantX) (ResultantY * ResultantY);
Magnitude = Math.sqrt(ResultantT);
answer = findViewById(R.id.Answer);
answer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Resultant.setText(String.valueOf(Magnitude));
}
});
This is all the code I have made, The error shows up in line 45 which is "double D1 = Double.parseDouble(M1.getText().toString());"
CodePudding user response:
I think you would want to move the calculation part of code inside the onClick()... something like this:
answer = findViewById(R.id.Answer);
answer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double D1 = Double.parseDouble(M1.getText().toString());
double D2 = Double.parseDouble(M2.getText().toString());
double angle1 = Double.parseDouble(A1.getText().toString());
double angle2 = Double.parseDouble(A2.getText().toString());
Vector1X = D1 * Math.cos(Math.toRadians(angle1));
Vector1Y = D1 * Math.sin(Math.toRadians(angle1));
Vector2X = D2 * Math.cos(Math.toRadians(angle2));
Vector2Y = D2 * Math.sin(Math.toRadians(angle2));
ResultantX = Vector1X Vector2X;
ResultantY = Vector1Y Vector2Y;
ResultantT = (ResultantX * ResultantX) (ResultantY * ResultantY);
Magnitude = Math.sqrt(ResultantT);
Resultant.setText(String.valueOf(Magnitude));
}
});
CodePudding user response:
You are trying to read / parse D1
, D2
, etc before the screen has been shown to the user. Naturally, the fields are blank.
CodePudding user response:
Yes. Sujal Kumar's answer works but as mentioned by Stephen.C
in his comment we need to need to follow some precautions. These are the thing you need to follow
- Check if the value is null or empty(It should not be)
- Check if the value entered in a double
- First we are going to check if the value entered in not empty. This code will work
answer = findViewById(R.id.Answer);
answer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(M1.getText().toString().trim().isEmpty() ||
M2.getText().toString().trim().isEmpty() ||
A1.getText().toString().trim().isEmpty() ||
A2.getText().toString().trim().isEmpty()){
Toast.makeText(this, "One of the fields are empty", Toast.LENGHT_SHORT).show();
return;
}
double D1 = Double.parseDouble(M1.getText().toString());
double D2 = Double.parseDouble(M2.getText().toString());
double angle1 = Double.parseDouble(A1.getText().toString());
double angle2 = Double.parseDouble(A2.getText().toString());
Vector1X = D1 * Math.cos(Math.toRadians(angle1));
Vector1Y = D1 * Math.sin(Math.toRadians(angle1));
Vector2X = D2 * Math.cos(Math.toRadians(angle2));
Vector2Y = D2 * Math.sin(Math.toRadians(angle2));
ResultantX = Vector1X Vector2X;
ResultantY = Vector1Y Vector2Y;
ResultantT = (ResultantX * ResultantX) (ResultantY * ResultantY);
Magnitude = Math.sqrt(ResultantT);
Resultant.setText(String.valueOf(Magnitude));
}
});
- Now we need to check if the text is double or not. Try this code:
//add this method in the class
public static boolean isNumeric(String str) {
return str.matches("-?\\d (\\.\\d )?"); //match a number with optional '-' and decimal.
}
// add this code for the on click listener.
answer = findViewById(R.id.Answer);
answer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(M1.getText().toString().trim().isEmpty() ||
M2.getText().toString().trim().isEmpty() ||
A1.getText().toString().trim().isEmpty() ||
A2.getText().toString().trim().isEmpty()){
Toast.makeText(this, "One of the fields are empty", Toast.LENGHT_SHORT).show();
return;
}
if(!isNumeric(M1.getText().toString().trim()) ||
!isNumeric(M2.getText().toString().trim()) ||
!isNumeric(A1.getText().toString().trim()) ||
!isNumeric(A2.getText().toString().trim())){
Toast.makeText(this, "One of the fields are not doubles", Toast.LENGHT_SHORT).show();
return;
}
double D1 = Double.parseDouble(M1.getText().toString());
double D2 = Double.parseDouble(M2.getText().toString());
double angle1 = Double.parseDouble(A1.getText().toString());
double angle2 = Double.parseDouble(A2.getText().toString());
Vector1X = D1 * Math.cos(Math.toRadians(angle1));
Vector1Y = D1 * Math.sin(Math.toRadians(angle1));
Vector2X = D2 * Math.cos(Math.toRadians(angle2));
Vector2Y = D2 * Math.sin(Math.toRadians(angle2));
ResultantX = Vector1X Vector2X;
ResultantY = Vector1Y Vector2Y;
ResultantT = (ResultantX * ResultantX) (ResultantY * ResultantY);
Magnitude = Math.sqrt(ResultantT);
Resultant.setText(String.valueOf(Magnitude));
}
});