Reputation: 718
I want to get a number from editText and use in a calculation, there is no any error in my code but I didn't get what I looking for, the value of r is alwayas 1, here is the code:
public class Counter extends Activity {
Button add, sub;
TextView result;
EditText range;
double r;
public double c=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_counter);
add = (Button) this.findViewById(R.id.add);
sub = (Button) this.findViewById(R.id.sub);
result = (TextView) this.findViewById(R.id.result);
range = (EditText) this.findViewById(R.id.range);
try {
r = new Double(range.getText().toString());
} catch (NumberFormatException e) {
r = 1; // your default value
}
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
c=c+r;
result.setText(""+c);
}
});
sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
c=c-r;
result.setText(""+c);
}
});
}
any help ??
Upvotes: 0
Views: 444
Reputation: 454
you can make your editText in the XML file so that it accepts only numbers
android:inputType="number"
and in your java code
r = Double.parseDouble(range.getText().toString());
Upvotes: 0
Reputation: 27702
Double class provides a static method which parses a string to double.
So try to change:
r = new Double(range.getText().toString());
To:
r = Double.parseDouble(range.getText().toString());
Additionally, you may want to parse other strings to other types of numbers such as integers:
int x = Integer.parseInt(yourString);
Upvotes: 2
Reputation: 622
Try this one: Just in case there is the possibility that the String from the EditText cannot be parsed to a double I have put it inside a try-catch block.
double r;
try {
r = Double.parseDouble(range.getText().toString());
catch (NumberFormatException ex) {
r = 1;
}
Hope this helps :)
Upvotes: 0
Reputation: 157457
try {
r = new Double(range.getText().toString());
} catch (NumberFormatException e) {
r = 1; // your default value
}
the snippet of code you posted is correct. Double
has a constructor that takes a String
. The problem is that you are executing it after your findViewById and probably the EditText does not contain nothing. For instance when you press on add
, you can retrieve the content of the EditText
again and parse it:
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
r = new Double(range.getText().toString());
} catch (NumberFormatException e) {
r = 1; // your default value
}
c=c+r;
result.setText(""+c);
}
});
Upvotes: 3
Reputation: 7494
You just need to do:
r = Double.parseDouble(range.getText().toString()); // Surround with try/catch for fancy functionality
Upvotes: 0
Reputation: 1617
Try this (parsing string to double):
r = Double.parseDouble(range.getText().toString());
you should get value from EditText when user will click on button when you will get value only in onCreate() it will be take data from EditText when it will be created
Upvotes: 0