unable to convert EditText to int using parseInt - android

The logcat shows that the Integer.Parse() function was not able to convert the string to int or float whereas i have linked the xml file in java. Also, i have set the input Type of the rate and quantity column as number so there is no chance that the user can enter anything other than an int or float. Plz help. I have been trying to get this code working for a long time. But i cant find the solution.
Attached is the code:
public int initializeVars(){
int i;
for(i=0;i<9;i++){
//items[i] is the AutoCompleteTextView array
item[i]=items[i].getText().toString();
iquant[i]=Integer.valueOf(quant[i].getText().toString());
irate[i]=Float.valueOf(rates[i].getText().toString());
if(item[i]=="")
break;
}
return i;
}
}

To avoid these kind of circumstances use these two functions.
For Int
private int getIntFromString(String str){
try{
return Integer.parseInt(str);
}catch(Exception e){
return 0;
}
}
For float
private float getFloatFromString(String str){
try{
return Float.parseFloat(str);
}catch(Exception e){
return 0f;
}
}
Call these methods at required places

Try to use the parseInt method instead of valueOf and also keep that parsing code in try{}...catch{} block of NumberFormateException.
try{
iquant[i]=Integer.parseInt(quant[i].getText().toString());
irate[i]=Float.parseFloat(rates[i].getText().toString());
}
catch(NumberFormateException e){}

INT
try {
myInt = Integer.parseInt(myString);
} catch(NumberFormatException e) {
}
FLOAT
try {
myFloat = Float.parseFloat(myString);
} catch(NumberFormatException e) {
}

Maybe instead of valueOf you can try parsing it directly via
yourInt = Integer.parseInt(yourString);
youtFloat = Float.parseFloat(yourString);
I always do it this way and it works. In the float variable make sure that decimals are marked by "." not ",".
You may also want to try and catch NumberFormatException to avoid problems while parsing.

Related

Empty EditText error (Lots of EditTexts)

The activity contains a lot of EditTexts (about 40) where the user enters numbers, and then they're added. But when one of these EditTexts is empty, it gives an error and the app stops. I've read lots of solutions, but I don't want to write code for each EditText.
Here is some code (Wrote 450 lines, don't gonna show them all, but all of them are similar)
EditText mark1;
EditText mark2;
EditText mark3;
EditText mark4;
EditText mark5;
TextView marksem1;
Button calcsem1;
double mark1calc=0;
double mark2calc=0;
double mark3calc=0;
double mark4calc=0;
double mark5calc=0;
double totalsem1=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.etsia_gia_ata_layout);
initControls();
private void initControls() {
// Course 1 sem 1 calc
mark1=(EditText)findViewById(R.id.mark1);
mark2=(EditText)findViewById(R.id.mark2);
mark3=(EditText)findViewById(R.id.mark3);
mark4=(EditText)findViewById(R.id.mark4);
mark5=(EditText)findViewById(R.id.mark5);
marksem1=(TextView)findViewById(R.id.total1calc);
calcsem1=(Button)findViewById(R.id.total1);
calcsem1.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) {calcsem1();}
private void calcsem1() {
mark1calc=Double.parseDouble(mark1.getText().toString());
mark2calc=Double.parseDouble(mark2.getText().toString());
mark3calc=Double.parseDouble(mark3.getText().toString());
mark4calc=Double.parseDouble(mark4.getText().toString());
mark5calc=Double.parseDouble(mark5.getText().toString());
totalsem1=(9*mark1calc+6*mark2calc+6*mark3calc+6*mark4calc+3*mark5calc)/30;
marksem1.setText(Double.toString(totalsem1));
marksem1.setText(String.format("%.2f", totalsem1));
}});
}
}
Hope there's a solution. Thank you very much!
Your problem comes from the fact that Double.parseDouble(...) on an empty string will throw a NumberFormatException. You could replace each line with somthing like this:
try {
mark1calc = Double.parseDouble(mark1.getText().toString());
} catch (NumberFormatException e) {
mark1calc = 0;
}
This is a lot of extra code, so I'd suggest implementing something like this:
public static Double safeParse(String input) {
try {
return Double.parseDouble(input);
} catch (NumberFormatException e) {
return 0d;
}
}
// ...
mark1calc = safeParse(mark1.getText().toString());
mark2calc = safeParse(mark2.getText().toString());
mark3calc = safeParse(mark3.getText().toString());
// ...
(This assumes you want an empty box to be worth 0; you can adjust the default value to your needs)
I assume your problem is with parseDouble. You can write your own protected parseDouble, which will try-catch to parse the text, and if an exception is thrown, will just return zero.
Something like this:
private static double safeParseDouble(String text)
{
try
{
double result = Double.parseDouble(text);
return result;
}
catch (Exception ex)
{
return 0;
}
}
Double.parseDouble() can throw exceptions so you will need to use try-catch block.
try {
mark1calc=Double.parseDouble(mark1.getText().toString());
mark2calc=Double.parseDouble(mark2.getText().toString());
mark3calc=Double.parseDouble(mark3.getText().toString());
mark4calc=Double.parseDouble(mark4.getText().toString());
mark5calc=Double.parseDouble(mark5.getText().toString());
totalsem1=(9*mark1calc+6*mark2calc+6*mark3calc+6*mark4calc+3*mark5calc)/30;
marksem1.setText(Double.toString(totalsem1));
marksem1.setText(String.format("%.2f", totalsem1));
} catch (Exception e) {
// handle errors...
}
you can replace your parsing statement with terinary statement as below:
mark2calc = mark4.getText().toString().equals("") ? 0 : Double.parseDouble(mark4.getText().toString());

NumberFormatException error - Android

I get NumberFormatException error at this line of my code. What should i do?
String employ = intent.getStringExtra("employeeid").trim();
spinEmployee.setSelection(Integer.parseInt(employ)-1);
You can handle this by adding try catch block.
Create one method to get valid int from string like this
public int getValidIntFromString(String inputstring) {
if (inputstring!= null) {
if (!inputstring.equals("")) {
try {
return Integer.parseInt(inputstring);
} catch (NumberFormatException e) {
// TODO: handle exception
Log.e("Error-- > ", e.toString());
return 0;
}
} else {
return 0;
}
} else {
return 0;
}
}
Now you can convert string to int by calling this
int intemploy = getValidIntFromString(employ);
Here employ will be your String from where you want to parse Int from String.
Note: if you get employ data from EditText then please set inputType="number" in xml file to avoid such kind of Exception.
Just like this
<EditText
android:id="#+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
The exception says the format of EMPLOY is not valid,maybe it's a String like '2d','33a'

Using fractions on android editText

I'd like an inputType in the edit text that i can input fractional number like this: 2/4 (i want print the "/").
The program is about calculating things and i need to type fractional insted of decimal. Thanks. Sorry my bad english.
I think the best way would be to use a string for your input text, then to parse the string to figure out what kind of things the user entered.
When the user has finished their input, you can check the string with something like this:
public float testInputString(String testString) {
boolean goodInput = true;
float result = 0;
if (testString.contains("/")) {
//possible division
String pieces[] = testString.split("/");
if (pieces.length != 2) {
goodInput = false;
} else {
try {
float numerator = Float.parseFloat(pieces[0]);
float denominator = Float.parseFloat(pieces[1]);
result = numerator/denominator;
} catch (Exception e) {
goodInput = false;
}
}
} else if (testString.contains(".")) {
try {
result = Float.parseFloat(testString);
} catch (Exception e) {
goodInput = false;
}
}
//TODO something here if bad input, maybe an alert or something
return result;
}
Also, you can check while they are typing for valid input if you use a keylistener like this. You could modify that to allow only numbers . and /.
put this property in xml node of Edittext and use Double notation instead of "/"
android:digits="1234567890.-"

Android: Getting into right condition using fileinputstream

I'm facing a different problem, see below is the code for my app that can read stored file and have to check condition according to that.
My inputs are "ON" and "OFF"
String val="";
final ToggleButton start = (ToggleButton) findViewById(R.id.startup);
FileInputStream fileos;
try {
fileos = openFileInput("startup");
byte[] input = new byte[fileos.available()];
while(fileos.read(input) != -1){
val += new String(input);
}
if(val.toString() == "ON"){
start.setChecked(true);
}else if(val.toString() == "OFF"){
start.setChecked(false);
}else{
start.setChecked(true);
}
fileos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The above code fetching the output correctly either "ON" or "OFF", But it Always going into else conditionelse
else{ start.setChecked(true); }
I'm stucked here, Please help me some one
android is based in java , in java you can't use "==" to compare two strings , you should replace
val.toString() == "ON"
to
"ON".equals(val.toString())
There are actually two big problems with this code. One is that you must use the equals() method to compare String objects, always -- the == operator is appropriate only in very limited cases.
The second one is more subtle, and won't break all the time. When you read data into input, although you're using a loop, the code will only work if all the data is read at once. This is because you're creating a String out of the entire array, even if the entire array doesn't contain valid data. The correct loop would look like this:
int count;
while((count = fileos.read(input)) != -1){
val += new String(input, 0, count);
}
you have to compare the string using .equals()
if(val.equals("ON")){
start.setChecked(true);
}else if(val.equals("OFF")){
start.setChecked(false);
}else{
start.setChecked(true);
}
Use String.equals() to compare Strings. Do no use ==
val.toString().equals("ON")

Android - parse String longitude to float causes error

I need to convert my CMS data (which are provided as Strings) to float value, but I am getting exception
NumberFormatException: invalid float value: "16.385837"
The code looks like:
Double.valueOf(myString.trim()).doubleValue();
I've also tried like this:
Double.parseDouble(myString).doubleValue();
but i'm getting the same message. Do you have any idea what is wrong ?!
try {
String s = "16.385837";
Double d = Double.parseDouble(s);
System.out.println(d);// which will prints 16.385837
} catch (NumberFormatException e) {
// p did not contain a valid double
}
String s = e1.getText().toString();
Float f= Float.parseFloat(s);
use this code this will helps you
put your value on place of s; then you can parse string to float
Try this,
Double.parseDouble(String.valueOf("16.385837"));
try
Double.parseDouble(myString) not .doubleValue();
Try this
try {
Double d = Double.parseDouble(String.valueOf("16.385837"));
System.out.println(d);
} catch (NumberFormatException e) {
// Handle The Exception During Parsing
}

Categories

Resources