This question already has answers here:
java.lang.numberformatexception: invalid double: " "
(6 answers)
Closed 8 years ago.
When I try running my Android App, I get an error saying: java.lang.NumberFormatException: Invalid double: "". This is my code:
public class MainActivity extends Activity {
double score;
EditText gpa;
EditText sat;
EditText act;
Button calc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gpa = (EditText) findViewById(R.id.gpa);
String gpaString = gpa.getText().toString();
final double gpaDouble = Double.parseDouble(gpaString);
sat = (EditText) findViewById(R.id.sat);
String satString = sat.getText().toString();
final int satInt = Integer.parseInt(satString);
act = (EditText) findViewById(R.id.act);
String actString = act.getText().toString();
final int actInt = Integer.parseInt(actString);
calc = (Button) findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(actInt/36>satInt/2400){
score = (0.6*gpaDouble*25)+(0.4*(actInt/36)*100);
}else{
score = (0.6*gpaDouble*25)+(0.4*(satInt/2400)*100);
}
}
});
}
}
I essentially want to get numbers from three EditTexts, make one of them into a double and the other two into ints. Then I would use these variables to set the value for another double variable. I am not getting errors before I run the app. I feel that when the EditText field is blank, it will not parse correctly, but I am unsure how to solve this. What is the problem?
Invalid double: "".
you are parsing onCreate() value without putting any default value so the exception
final double gpaDouble = Double.parseDouble(gpaString);
because "" (empty String is not Double)
Related
This question already has answers here:
How to convert TextView value to Integer
(2 answers)
Closed 2 years ago.
For the if statements part, it has a red mark. and it says, operator '+' can't be applied to 'TextView', 'int'. And I have a no clue what to do with that. Am I not able to use < kind of symbol for if statement?
public class result extends AppCompatActivity {
Button button3;
TextView message;
TextView SMM;
TextView BFM;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
SMM = (TextView) findViewById(R.id.SMM);
BFM = (TextView) findViewById(R.id.BFM);
button3 = (Button) findViewById(R.id.button3);
button3.setClickable(true);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String musclemass = SMM.getText().toString();
String fatmass = BFM.getText().toString();
Intent intent = new Intent();
intent.putExtra("musclemass", musclemass);
intent.putExtra("fatmass", fatmass);
if(musclemass < fatmass + 50){
message.setText("kid");
}
if(musclemass > fatmass + 50){
message.setText("adult");
}
}
});
}
}
Change
if(SMM < BFM + 50){
to
if(Integer.valueOf(SMM.getText().toString())< Integer.valueOf(BFM.getText().toString())+ 50){
and similar at other locations as TextView and integer are two different datatypes. You have to first get Integer value and then compare them
Seem you don't understand what is the TextView, SMM in your case for example. You already get the string musclemass from SMM(TextView), right?
How would you describe this, SMM.getText().toString ?
Assume that you know you got a string.
Why don't you (try to) convert this string to int (Integer). You have to screen and make sure that the input is really an Integer, not other characters.
Since TextView (BFM in this case) is not an integer number, it can't be added (+) with 50.
You would better use EditText instead of TextView.
I want to give a number from edit text and convert to Long and payment this number with a bank gate.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.otherpay);
pardakht = (Button) findViewById(R.id.pardakhtbutton);
mablagh = (TextView) findViewById(R.id.mablagh);
varizi = (EditText) findViewById(R.id.ed4);
description = (TextView) findViewById(R.id.description);
descriptionvariz = (EditText) findViewById(R.id.ed5);
mablaghs = varizi.getText().toString();
//convert number to long
mandeh = Long.parseLong(mablaghs);
//online pay formoney
pardakht.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myPeyment(mandeh);
}
});
and I have an eroor:
java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.atzgroup.atzsoft/ir.atzgroup.atzsoft.OtherpayActivity}: java.lang.NumberFormatException: For input string: ""
I have to convert my string that given from edit text to Long to payment
please help me
Read the input only when user clicks on the Button because at the time of initialization value will be "".
Code:
//online pay formoney
pardakht.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
mandeh = Long.parseLong(varizi.getText().toString());
myPeyment(mandeh);
}
catch(Exception e){
//Invalid input entered.
e.printStackTrace();
}
}
});
Error says For input string: ""
You cannot convert empty String to Number. So you need to handle this at the code level.
if(!mablaghs.isEmpty())
mandeh = Long.parseLong(mablaghs);
Before parsing by Long.parseLong(mablaghs), you should check entered text is a number by using the below method.
boolean digitsOnly = TextUtils.isDigitsOnly(editText.getText());
if (digitsOnly) {
mandeh = Long.parseLong(mablaghs);
}
I have quite a simple application. However, after I clik on the button, app crashes. Tried to debug it and the problem seems to be in first 3 row of the onClick method. Once I tried to get there values manually, not via those edit boxes, everything went smoothly. Any ideas please?
public class MainActivity extends AppCompatActivity {
EditText editText_pocetKM;
EditText editText_spotreba;
EditText editText_cenaPHM;
TextView textView_spotrebaO;
TextView textView_cenaO;
DecimalFormat df = new DecimalFormat("0.00##");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText_pocetKM = (EditText) findViewById(R.id.editText1_pocetKM);
editText_spotreba = (EditText) findViewById(R.id.editText_Spotreba);
editText_cenaPHM = (EditText) findViewById(R.id.editText1_cenaPHM);
textView_spotrebaO = (TextView) findViewById(R.id.textView_spotrebaO);
textView_cenaO = (TextView) findViewById(R.id.textView_cenaO);
}
public void onClick(View v) {
Double pocetKm = Double.parseDouble(editText_pocetKM.getText().toString());
Double spotreba = Double.parseDouble(editText_spotreba.getText().toString());
Double cenaPHM = Double.parseDouble(editText_cenaPHM.getText().toString());
Double spotrebaO = spotreba * pocetKm / 100;
Double cenaO = spotrebaO * cenaPHM;
textView_cenaO.setText("Cena za spotřebované palivo bude "+ df.format(cenaO) + " Kč");
textView_spotrebaO.setText("Celkem bude spotřebováno "+ df.format(spotrebaO) + " litrů paliva");
}
}
You didn't provide the logcat of your crash report. If the logcat was provided we could be certain of your exact problem. But anyway, as you've got rid of your crash by removing the first three lines of your onClick function, I suppose you're setting invalid inputs in your EditText.
You're parsing the text entered in the EditText to double which will fail if the input is not a valid double string. For example, it'll parse 11.01 fine when it'll throw an exception while parsing Hello.
So to check if the application is crashing for a parsing error, you might consider surrounding them with a try/catch block like this.
try {
Double pocetKm = Double.parseDouble(editText_pocetKM.getText().toString());
Double spotreba = Double.parseDouble(editText_spotreba.getText().toString());
Double cenaPHM = Double.parseDouble(editText_cenaPHM.getText().toString());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Parsing error", Toast.LENGTH_LONG).show();
}
This question already has an answer here:
How to receive an int through an Intent
(1 answer)
Closed 4 years ago.
I'm trying to pass an Integer (from an edittext) to another activity through an intent.
When the user clicks a button, the text in the edittext will transform into a string and then into an int, then the int will be sent through an intent to another activity, but i have to use the int after that.
Here the activity sending the intent:
public class HomeActivityPro extends ActionBarActivity {
private InterstitialAd interstitial;
EditText conttext = (EditText) findViewById ( R.id.texthome );
Button buttone = (Button) findViewById(R.id.buttone);
String maxom = conttext.getText().toString();
int maxam = Integer.parseInt(maxom);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
View.OnClickListener maxim = new View.OnClickListener() {
#Override
public void onClick (View view) {
Intent wall = new Intent(HomeActivityPro.this, GuessOne.class);
wall.putExtra("maxPressed", maxam);
startActivity(wall);
}
};
buttone.setOnClickListener(maxim);
Here the activity receiving it:
public class GuessOne extends ActionBarActivity {
int randone;
int contone;
int wall = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_guess_one);
wall = getIntent().getIntExtra("maxPressed", -1);
randone = (int) (Math.random()*10+1);
contone = 0;
}
Here i'm using it:
public void guessone (View view){
contone++;
textcontone.setText(getString(R.string.attempts) + "" + contone);
if (contone >= wall ){
resultaone.setText("You Failed" + " " + wall);
Toast.makeText(this, "You Failed", Toast.LENGTH_LONG).show();
}
When i use the app, the value of the int is always -1. Where i am wrong.
You can't use findViewById without setting the xml to the activity. That means you need to use findViewById method only after you have called setContentView.
Also you need to read the EditText text value once you click on the button otherwise it always will be null/empty.
Do this
public class HomeActivityPro extends ActionBarActivity {
private InterstitialAd interstitial;
EditText conttext;
Button buttone;
String maxom;
int maxam = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
conttext = (EditText) findViewById ( R.id.texthome );
buttone = (Button) findViewById(R.id.buttone);
View.OnClickListener maxim = new View.OnClickListener() {
#Override
public void onClick (View view) {
maxom = conttext.getText().toString();
maxam = Integer.parseInt(maxom);
Intent wall = new Intent(HomeActivityPro.this, GuessOne.class);
wall.putExtra("maxPressed", maxam);
startActivity(wall);
}
};
buttone.setOnClickListener(maxim);
Problem 1
Put this in the on click listener instead:
String maxom = conttext.getText().toString();
int maxam = Integer.parseInt(maxom);
You want the values to be read at the time you click the button not when you open the activity, correct?
Problem 2
The following needs to be after setContentView in onCreate:
conttext = (EditText) findViewById ( R.id.texthome );
buttone = (Button) findViewById(R.id.buttone);
Keep the declarations where they are. Just the declarations:
EditText conttext;
Button buttone;
Note
Follow the same pattern in all your activities. Declare views as field variables, assign them in onCreate after setContentLayout. Get the values at the time they're needed.
public int getIntExtra (String name, int defaultValue)
Added in API level 1 Retrieve extended data from the intent.
Parameters name The name of the desired item. defaultValue the value
to be returned if no value of the desired type is stored with the
given name. Returns the value of an item that previously added with
putExtra() or the default value if none was found. See Also
putExtra(String, int)
This means that no int was found when you called getIntExtra(valueName, defaultValue); so the default value was chosen.
You should check to see what your maxam value is before you call the new activity.
In the activity you receive it:
wall = getIntent().getIntExtra("maxPressed");
SOLVED: by getInt and String.valueOf
private static final String IMGID = "ImgID";
if (getIntent().getExtras().containsKey(IMGID)) {
//Picasso.with(this).load(getIntent().getExtras().getString(IMG)).into(mImg);
Picasso.with(this).load(getIntent().getExtras().getInt(String.valueOf(IMGID))).into(mImg);
}
This question already has answers here:
Reverse a string in Java
(36 answers)
Closed 9 years ago.
I just want to reverse a inputted String by using for loop.
I have tried the following code below. [ its full of mistake i think.. cause i dont know how to convert things to array or to string in this problem ]. So anyone please help me the coding here...
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.textView1);
EditText input_string =(EditText) findViewById(R.id.editText1);
final String orig = input_string.getText().toString();
Button rev = (Button) findViewById(R.id.button1);
rev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int limit = orig.length();
for(int i=limit;i<=limit;i--)
{
String[] neww = orig[i].;
}
tv.setText(neww);
}}) }}
Something like this is what you're looking for.
String x = "A string";
String y = "";
for(int i = x.length()-1; i >= 0; i--){
y=y + x.charAt(i);
}
Your new string will be stored in the variable y.