My app keeps crashing and gives me a:
java.lang.NumberFormatException: empty String
Also says this
FATAL EXCEPTION: main
Process: com.example.ayyan.jellybeanestimator, PID: 2966
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ayyan.jellybeanestimator/com.example.ayyan.jellybeanestimator.MainActivity}: java.lang.NumberFormatException: empty String
package com.example.ayyan.jellybeanestimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity {
EditText jellyBeanLength, jellyBeanDiameter, jarSizeVolume;
double jellybeantall, jellybeanfat, jellybeanspace, volumeOfOneJellyBean, volumeofBeans;
final double loadFactor = .698;
TextView answer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jellyBeanLength = (EditText) findViewById (R.id.length);
jellyBeanDiameter = (EditText) findViewById (R.id.diameter);
jarSizeVolume = (EditText) findViewById (R.id.jarsize);
jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
answer = (TextView) findViewById (R.id.answer);
Button calculate = (Button) findViewById (R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
volumeOfOneJellyBean = ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
volumeofBeans = (jellybeanspace*loadFactor)/volumeOfOneJellyBean;
int jellyguess = (int) (volumeofBeans);
answer.setText("You have this amount of beans in your jar" + jellyguess);
}
});
}
}
jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
All your EditTexts start as empty. You can't getText() in onCreate, you have to do it in onClick
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
volumeOfOneJellyBean = ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
volumeofBeans = (jellybeanspace*loadFactor)/volumeOfOneJellyBean;
int jellyguess = (int) (volumeofBeans);
answer.setText("You have this amount of beans in your jar" + jellyguess);
}
});
Note: when volumeOfOneJellyBean == 0, you'll get a division by zero error
Related
"I am getting this error please help - at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setText(java.lang.CharSequence)' on a null object reference
package com.codewithdevesh.cricketapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.google.android.material.textfield.TextInputLayout;
import java.util.HashMap;
public class TeamActivity extends AppCompatActivity {
private TextInputLayout team1,team2,overs;
private RadioGroup radioGroup1,radioGroup2;
private RadioButton rb_toss_t1,rb_toss_t2,rb_opted,rb_tossed;
private Button begin;
String toss_team,opted_team;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_team);
team1 = findViewById(R.id.tv_team1);
team2 = findViewById(R.id.tv_team2);
rb_toss_t1.setText(team1.getEditText().getText().toString());
rb_toss_t2.setText(team2.getEditText().getText().toString());
radioGroup1 = findViewById(R.id.radio_toss);
radioGroup2 = findViewById(R.id.radio_opted);
rb_toss_t1 = findViewById(R.id.rb_toss_t1);
rb_toss_t2 = findViewById(R.id.rb_toss_t2);
overs= findViewById(R.id.tv_overs);
int id_toss = radioGroup1.getCheckedRadioButtonId();
int id_opted = radioGroup2.getCheckedRadioButtonId();
rb_tossed =radioGroup1.findViewById(id_toss);
rb_opted = radioGroup2.findViewById(id_opted);
toss_team = rb_tossed.getText().toString();
opted_team = rb_opted.getText().toString();
begin = findViewById(R.id.bttn_begin);
begin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(TeamActivity.this,SelectPlayersActivity.class);
i.putExtra("opted_t",opted_team);
i.putExtra("toss_t",toss_team);
i.putExtra("team1",team1.getEditText().getText().toString());
i.putExtra("team2",team2.getEditText().getText().toString());
startActivity(i);
finish();
}
});
}
}
init the rb_toss_t1 and rb_toss_t2 before using them
And init rb_opted,rb_tossed with findViewById too
//no change
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_team);
team1 = findViewById(R.id.tv_team1);
team2 = findViewById(R.id.tv_team2);
radioGroup1 = findViewById(R.id.radio_toss);
radioGroup2 = findViewById(R.id.radio_opted);
// findViewById first before using them
rb_toss_t1 = findViewById(R.id.rb_toss_t1);
rb_toss_t2 = findViewById(R.id.rb_toss_t2);
//rb_opted,rb_tossed init
rb_opted = findViewById(R.id.rb_opted);
rb_tossed = findViewById(R.id.rb_tossed );
//then use function setText
rb_toss_t1.setText(team1.getEditText().getText().toString());
rb_toss_t2.setText(team2.getEditText().getText().toString());
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));
}
});
}
}
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.
I'm new to Android. I'm developing an app which gives all the possible combination number based on the user input and separated it by comma.
For example the user input is: 1234
The output should be list all the possible number combination from "1234" like
"1234,1243,1324,1342,1423,1432,2134,etc.."
And there's also another EditText that limit the output by setting it to 1 or 2 or 3 or 4(max).
For example the user input is: 2
The output should be list all the possible number combination from "1234" by "2" digits
"12,13,14,21,23,24,31,32,34,41,42,43"
This is my MainActivity.java
package com.example.androidtest1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
EditText et_input1, et_output1;
Button b_generate, b_clear;
CheckBox checkBox1;
Random r;
int min,output;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText mEdit = (EditText) findViewById(R.id.et_output1);
mEdit.setEnabled(false);
r = new Random();
et_input1 = (EditText) findViewById(R.id.et_input1);
et_output1 = (EditText) findViewById(R.id.et_output1);
b_generate = (Button) findViewById(R.id.b_generate);
b_clear = (Button) findViewById(R.id.b_clear);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
b_generate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (et_input1.length()==0){
et_input1.setError("Insert numbers");
}else if(checkBox1.isChecked()){
min = Integer.parseInt(et_input1.getText().toString());
output = (min);
et_output1.setText("" + output + "*");
et_input1.setError(null);
}
else{
et_input1.setError("Check the checkbox");
}
}
});
b_clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
et_output1.getText().clear();
et_input1.getText().clear();
return;
}
});
}
}
There probably is a really simple answer to this. My goal is to divide the the inputed information x and y and put that into z.
Here is my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
public int x = Integer.parseInt("");
public int y = Integer.parseInt("");
public int z = (y/x);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button
Button btn = (Button) findViewById(R.id.button);
//EditText
EditText nop = (EditText) findViewById(R.id.editText);
EditText cob = (EditText) findViewById(R.id.editText2);
x = Integer.parseInt(nop);
y = Integer.parseInt(cob);
final TextView tv = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(z);
}
});
}
}
Declare z inside your onclick function. Or assign it a value inside your function.
Also nop and cob are editText's get the data in it, use getText().
Because Integer.parseInt() accepts only a String but not an editText, so you must use getText() to get the String value in it.
x = Integer.parseInt(nop.getText().toString());
y = Integer.parseInt(cob.getText().toString());
final TextView tv = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int z = x / y;
tv.setText(z);
}
});
Also z is declared globally and assigned nothing. But it needs to be assigned a value when ever you click the button, so that your editText changes every time you click the button.
Convert first as a string from Edittext then make it as Integer.
String Val1 = nop.getText().toString();
String Val2 =cob.getText().toString();
x = Integer.parseInt(val1);
y = Integer.parseInt(val2);
I'm programming a gas calculator that has two outputs (Total Cost of Gas and Total Gallons); how can I put a "$" in front of totalCost but not in front of totalGas?
Also when I run the program in the emulator, when I click on the reset button it force closes the program and I get a FATAL EXCEPTION: main error.
EDIT: I figured it out. I forgot to import myClickHandler on the reset button.
Here's the java file:
package com.example.gas;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import com.example.gas.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
private EditText totalMiles;
private EditText price;
private EditText mpg;
private TextView totalGas;
private TextView totalCost;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
totalMiles = (EditText) findViewById(R.id.milesTxt);
totalCost = (EditText) findViewById(R.id.costTxt);
totalGas = (EditText) findViewById(R.id.gasTxt);
price = (EditText) findViewById(R.id.priceTxt);
mpg = (EditText) findViewById(R.id.mpgTxt);
}
public void myClickHandler(View view)
{
NumberFormat nf = new DecimalFormat("#,###.00");
switch (view.getId())
{
case R.id.calcBtn:
float inputMiles = Float.parseFloat(totalMiles.getText().toString());
float inputMPG = Float.parseFloat(mpg.getText().toString());
float inputPrice = Float.parseFloat(price.getText().toString());
float result1 = inputMiles/inputMPG;
String output1 = nf.format(result1);
totalGas.setText(output1);
float result = result1 * inputPrice;
String output = nf.format(result);
totalCost.setText(output);
break;
case R.id.resetBtn:
totalMiles.setText("");
totalCost.setText("");
totalGas.setText("");
price.setText("");
mpg.setText("");
break;
}
}
}
You should pre-pend it to the String variable:
totalCost.setText(output);
to
totalCost.setText("$"+output);