I am creating a calculator app in android. It should calculate anonymously whatever is on textBox. For e.g. I enter 1+2.5-5*8 in textBox. But when I call addition method the app gets crashed. Because the input is in string format and answer I want is in numeric format. I used string buffer. I tried in java that when I enter (1+1+3-1) in stringBuffer and I display using println() method it give correct answer but same does not happen with string buffer when I take that value from editText.
You have to convert string into integer or float.
Use Integer.parseInt("") method to covert to integer and Float.parseFloat("") to convert to float.
First of all welcome to StackOverFlow
It might help if you add code and the error log in the question
So I am gonna cover all the possible things that might be happening wrong in your program:
NumberFormatException- you might not be converting the string "12" into int 12 to do so use Interger.parseInt(your string buffer with number only here);
Try using a stack and a queue for your solution (google it you will get details)
You might be making a mistake in getting string from edittext it sometimes give NullPointerException
if above is not enough comment and add your code asap
Related
I write app for Android such gets data from server in JSON format. Now I get this value in string, but in my application it must look like:
Route:
1)first point
2)secon point
3).....
n) n point
I read that in Android in textView I can do it if string will be with html tags but I think it is not the best variant. After Android I must do it in iPhone now I don't know how to do that there. Send Routes as Array is not good variant too. Can you say what is the best way to decide this problem?
Have a look here you will have to find the good pattern .
Hence you have separated strings just use a list View with an ArrayAdapter.
I am not so good with regex but i think it should like : [1-9][0-9]) [[a-f][0-9]]+
I couldn't comment b/c of rep, sorry. Could you provide an example of returned JSON string. I think JSON format can be parsed with ease.
If this the case you can parse it in a loop (or another way. I'm not that good at it)
String[] parseIt (String JSON){
String[] list=JSON.split("\\d\\)");
String[] rlist=new String[list.length-1];
for(int i=0;i<list.length-1;i++){
rlist[i]=list[i+1].trim();
}
return rlist;
}
This might do trick. But you should edit result. I didn't test yet
Edit: I edited code. It simply return the address now with leading whitespace. You can get rid off them using. String trim() method like;
list[1].trim();
Do it in loop and don't care about first element (index 0).
Edit 2: Now it should work
I FINALLY have the map and points(arrays) working for my app. Quick question: I have a fatal exception with substring(), a "stringIndexOutOfBoundException"
In general, what is that referring to?
An I going past the end of a string using substring()?
Thanks,
testing.substring(1,2);
(I want to parse each character to find specific characters)
I wouldn't use substring() for grabbing 1-length strings (which is just a single character), but rather charAt(int) for specific positions. If you need to go over all characters in the string, you're probably better off with by converting the whole thing to a char[] first (using toCharArray()) and iterate over that.
Yes, you're going past the end of your strings bounds generally.
The Java API even tells you so...
IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.
You should get used to using the API. It tells you what exceptions a method throws and why.
Try printing the Strings length and value before attempting substring. That'll help you see the problem.
For example...
String testing = "Hello StackOverflow";
System.out.println("Length of testing = " + testing.length);
System.out.println("Value of testing = " + testing);
testing.substring(1,2);
Like stated in the official doc here:
public String substring(int beginIndex)
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
Throws: IndexOutOfBoundsException - if beginIndex is negative or
larger than the length of this String object.
Why does this code trigger a force close in Android?
`score.setText(Integer.parseInt((String) score.getText())+1);`
score is a TextView, and I am simply increasing the number by 1. I have predefined a String resource to be the initial number in the score TextView.
I am quite frustrated.
First off you should try breaking down your code so you can actually see what is going on with it.
Instead of
score.setText(Integer.parseInt((String) score.getText())+1);
try
String tmp = score.getText().toString();
int score;
score = Integer.parseInt(tmp) + 1;
score.setText(String.valueOf(score));
EDIT: Upon further reading of the documentation, setText has several overloads, one of which DOES take an int, but it takes the int of a resource ID. My guess is that your score is not a valid resource ID, thus crashing your application.
public final void setText (int resid)
Oh and as far as the frequent FC's when beginning Android Dev, it happens to the best of us. The key is to learn WHY the FC's happen, and have a LOT of patience.
mostly u need to do this
score.setText(Integer.parseInt(score.getText().toString())+1);
coz.. getText() returns a Editable Object which cannot be parsed to Integer. So it give NumberFormat Exception.
AndMake sure to set TextView,s Text to an integer initially..
try this way
score.setText(String.valueOf(Integer.parseInt(score.getText().toString())+1));
as you can pass the integer value that's why getting force the application
TextEdit.setText takes a CharSequence as input.
You are supplying an integer through Integer.parseInt((String) score.getText())+1
See, if converting it back to string and using it in setText helps.
You can convert an integer to string using Integer.toString.
PS: I am new to java myself.
The compiler should have ideally caught this error.
It's possible java uses some implicit type conversions from string to int.
So I have a simple Edit-text with number input.
User enters his phone number and it is saved into DB obviously that is the goal.
now every time i do
int contactNumber=Integer.parseInt(mContactNumber.getText().toString());
I get an error thrown saying NumberFormatException for some reason it doesn't like a string of number. I have made sure that mContactNumber field in the android input field has the
android:input="number"
now i also tried just to be sure
int contactNumber=Integer.parseInt(mContactNumber.getText().toString().trim());
still the same error
Guys any ideas?
Try putting the Type to ' long ' instead of ' int '. Hope that will work for you.
This might be because you are leaving the text field empty. Are you sure you are not parsing an empty string?
You need to put a simple condition:
if(mContactNumber.getText().length()>0)
{
int contactNumber=Integer.parseInt(mContactNumber.getText().toString().trim());
}
For phone Number you can not take it as Integer. try to take it as string only and after getting that string you can do the check for the numbers only by
TextUtils.isDigitsOnly(str);
I'm trying to persistently store time data. I write the time to the preferences as a string passing it the time.toString(), and then restore it from the string by using the time.parse(String) method. However, I find that the parse method is throwing a TimeFormatException, specifically:
android.util.TimeFormatException: Unexpected character 0x41 at pos=15. Expected Z
I use logcat to view the string i am passing to parse, and it looks normal:
20110321T021030America/Detroit(1,79,-14400,1,1300687830)
Can anyone figure out why this is? Does the "expected Z" mean the letter Z specifically, or does it mean any integer, or what? And why is this happening? It seems like parsing a Time's toString() would be the easiest way to ensure that there ISN'T a timeformatexception, and yet I am still getting one.
It probably just doesn't recognise the format. You could use time.getTime() to get the unix time value instead, this might be easier to use.
The problem with parsing the date format which has been passed to time.parse(); function
Please refer the link to rectify you problem
Custom Date and Time Format Strings