I am making a calculator and I am stumped on this part. I want to have the value inputted from the user for inputG only to be between numbers 1-100. Then, grab that value and use it in a formula. My logic is off somewhere and I need help finding what I did wrong. I am using Java, not Kotlin.
package com.example.rvbtcal;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView inputMoles = (EditText) findViewById(R.id.txtMoles);
EditText inputML = (EditText) findViewById(R.id.txtmL);
EditText inputG = (EditText) findViewById(R.id.txtG);
TextView outputAnswer = (TextView) findViewById(R.id.txtAnswer);
Button btnCalculate = (Button) findViewById(R.id.btnCalculate);
Spinner spinner = (Spinner) findViewById(R.id.spnN);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arrayN, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
double iM = Double.parseDouble(inputMoles.getText().toString());
double iML = Double.parseDouble(inputML.getText().toString());
double ioA = Double.parseDouble(inputG.getText().toString());
double textN = Double.parseDouble(spinner.getSelectedItem().toString());
if (ioA >= 0 || ioA <= 101){
Toast.makeText(MainActivity.this,"Enter a number between 1-100", Toast.LENGTH_SHORT);
}
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double Answer1 = (iM * iML * textN) / (ioA / 1000);
double FinalAnswer=(Answer1*1000000);
outputAnswer.setText(String.format("%.3f",FinalAnswer));
// String FinalAnswer2=String.valueOf(FinalAnswer);
// outputAnswer.setText(FinalAnswer2);
}
});
}
}
I now have my code like this below, however, the calculations are still made even with the If statement. I tried an else statement but then the answer is blank no matter if the numbers for input g fall within the parameters.
package com.example.rvbtcal;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView inputMoles = (EditText) findViewById(R.id.txtMoles);
EditText inputML = (EditText) findViewById(R.id.txtmL);
EditText inputG = (EditText) findViewById(R.id.txtG);
TextView outputAnswer = (TextView) findViewById(R.id.txtAnswer);
Button btnCalculate = (Button) findViewById(R.id.btnCalculate);
Spinner spinner = (Spinner) findViewById(R.id.spnN);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arrayN, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double iM = Double.parseDouble(inputMoles.getText().toString());
double iML = Double.parseDouble(inputML.getText().toString());
double ioA = Double.parseDouble(inputG.getText().toString());
double textN = Double.parseDouble(spinner.getSelectedItem().toString());
if (ioA < 0 || ioA > 100) {
Toast.makeText(MainActivity.this, "Enter a number between 1-100", Toast.LENGTH_SHORT);
}
double Answer1 = (iM * iML * textN) / (ioA / 1000);
double FinalAnswer = (Answer1 * 1000000);
outputAnswer.setText(String.format("%.3f", FinalAnswer));
}
});
}
}
My final code for others who may struggle with this in the future here is the finished product (for now):
package com.example.rvbtcal;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView inputMoles = (EditText) findViewById(R.id.txtMoles);
EditText inputML = (EditText) findViewById(R.id.txtmL);
EditText inputG = (EditText) findViewById(R.id.txtG);
TextView outputAnswer = (TextView) findViewById(R.id.txtAnswer);
Button btnCalculate = (Button) findViewById(R.id.btnCalculate);
Spinner spinner = (Spinner) findViewById(R.id.spnN);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arrayN, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double iM = Double.parseDouble(inputMoles.getText().toString());
double iML = Double.parseDouble(inputML.getText().toString());
double ioA = Double.parseDouble(inputG.getText().toString());
double textN = Double.parseDouble(spinner.getSelectedItem().toString());
if (ioA <= 0 || ioA > 100) {
Toast.makeText(MainActivity.this, "Enter a number between 1-100", Toast.LENGTH_LONG).show();
outputAnswer.setText("");
return;
}
double Answer1 = (iM * iML * textN) / (ioA / 1000);
double FinalAnswer = (Answer1 * 1000000);
outputAnswer.setText(String.format("%.3f",FinalAnswer));
}
});
}
}
CodePudding user response:
You need to move the following lines into the btnCalculate.setOnClickListener
,
double iM = Double.parseDouble(inputMoles.getText().toString());
double iML = Double.parseDouble(inputML.getText().toString());
double ioA = Double.parseDouble(inputG.getText().toString());
double textN = Double.parseDouble(spinner.getSelectedItem().toString());
if (ioA < 1 || ioA > 100) {
Toast.makeText(MainActivity.this,"Enter a number between 1-100", Toast.LENGTH_SHORT).show();
return;
}
You need to get the user input after the user taps on the btnCalculate
because that's where you can get the latest input of the user.
What you are doing is fetching the value as soon as the EditText view is being created. Hence it returns an empty string. And when you try to pass an empty string to double it should give you an Exception. Unless you have assigned a text value to the EditText in your XML.