parseInt: invalid int "0" [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Whenever I want to parse the string "0" in Java to 0 (int) it throws a InvalidInt-Error.
However strings like "1", "2" etc. work.
UPDATE: Other numbers don't work as well.
I'm fetching the HTML source code of a PHP-File from my web page and this web page only displays one number.
Code:
String[] result = sourceCode.trim().split("<br>");
for (int i = 0; i < result.length; i++)
{
result[i] = result[i].trim().replace("\n", "").replace("\r", "");
}
if (Integer.parseInt(result[0]) > 0)
{
//Do Something
}

With the information provided in the comments below the question, it turns out that you have the Unicode Character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF) repeatedly at the beginning of your String. You can do the following to remove it:
final String ZERO_WIDTH_NO_BREAK_SPACE = "\uFEFF";
String good = result[0].replace(ZERO_WIDTH_NO_BREAK_SPACE, "");
Also, there is one TAB hidden inbetween all those characters. Get rid of it with
good = good.replaceAll("\\s+", "");

Related

Digital (number) length in Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How to get the length of some number in Java?
Length of string is string.length(), but what is it for an integer?
I tried this:
int lengthNumber = (String.valueOf(maxNumber)).length();
But it always returns 1, for every number.
Try
Integer#toString().length();
For Example
Integer a=222;
int length=a.toString().length();
Output
3
When I ran this:
int Number = 100003;
int lengthNumber = (String.valueOf(Number)).length();
System.out.println(lengthNumber);
My output was 6, indicating that it works correctly.
Just make sure that your variables are declared properly.
This method will work if the above isn't working.
int x = 100003;
String y = "" + x;
Now you can use y.length(). Printing y gives 100003, and printing y.length() gives 6.
System.out.println(y);
System.out.println(y.length());

convert an integer type array that contain values only 0 and 1 into string that should be simple sequence of array values in string form in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I want to convert integer type array that contain only 0 and 1 into string that should be simple sequence of array value not in any other form for example if array is
arr[] = {0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1}
than string should be
str = "0110100001100101"
than i want to divide it into two substrings like
str1 = 01101000(this is bit value of h as i know) and str2= 01100101 (this is bit value of e) and want to convert these substrings into character h from str1 and e from str2.
please help me .
You can use Arrays.toString()
Integer arr[] = {0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1};
String str= Arrays.toString(arr).replaceAll(",|\\[|\\]","").trim();
System.out.println(str);

Adding numbers within a string in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I add two numbers within one string?
I have:
String a = "(x+x)";
String lb = "2";
String b = a.replace("x", lb);
int c = ?
it outputs 2+2, how do I get it to add them together correctly into an integer?
While you can use a Java library to achieve this goal as mentioned in the comments, there is something built into Java since version 6 which may be useful (but maybe a little overkill). I suggest this not because I believe it's particularly efficient but rather as an alternative to using a library.
Use JavaScript:
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
ScriptEngine jsRuntime = new ScriptEngineManager().getEngineByName("javascript");
String expression = "2+2"; //The expression you would to evaluate.
double result = (Double) jsRuntime.eval(expression);
Use the Integer.parseInt method to parse a String into an int, then add the values. Adding strings just concatenates them.
You can use parseInt() method.
It seems your question could be summarised as
How do I convert a String such as "2+2" to the number 4
Here's a solution that uses only the standard Java libraries:
String sumExpression = "2+2";
String[] numbers = sumExpression.split("\\+");
int total = 0;
for (String number: numbers) {
total += Integer.parseInt(number.trim());
}
// check our result
assert total == 4;

Android converting string to array string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string like this:
["477","com.dummybilling","android.test.purchased","inapp:com.dummybilling:android.test.purchased","779"]
How to have a String[] with these 5 element?
Does anyone know a regex for .split() method?
Thank you very much, regular expressions make me crazy! :(
Process it as JSON. Two immediate benifits would be that it would take care of any embedded commas in your data automatically and the other that you would get a String[] with unquoted strings.
String input = "[\"477\",\"com.dummybilling\",\"android.test.purchased\",\"inapp:com.dummybilling:android.test.purchased\",\"779\"]";
JSONArray jsonArray = new JSONArray(input);
String[] strArr = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
strArr[i] = jsonArray.getString(i);
}
System.out.println(Arrays.toString(strArr));
Output :
[477, com.dummybilling, android.test.purchased, inapp:com.dummybilling:android.test.purchased, 779]
You can split your string by separator [" (the beginning) or "," or "] (the ending) like this:
final String[] tokens = yourString.split("\",\"|\\[\"|\"\\]");
Please note that this will only work for your string. It's not a general solution (for example, it does not take care of any escaped quotes). If your string is in JSON format, you should use a JSON parser as proposed by #Ravi Thapliyal .

Why can this not be done? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to shorten my code using a loop. I have for example 5 zombies in my game. So I thought I could do this
Image zombie;
for(int i = 0; i < 5; i++){
if (zombie.getZombieRect().intersects(zombie + i + .getZombieRect())) {
}}
Why can this not be done? adding i to the end of zombie. zombie being an image. The oother variables are zombie1, zombie2 etc.
Thanks for all help.
This is what arrays are for:
Zombie zombies[] = {zombie, zombie1, zombie2, zombie3, zombie4};
for (int i = 0; i < zombies.length; i++) {
if (zombie.getZombieRect().intersects(zombies[i].getZombieRect())) {
}
}
Make an Array of Objects and then u can call them by using zombie[i] etc whatever you want to do.
The thing of adding you are trying to do is suitable in case of strings only
"zombie"+i;
etc.
To answer the question,
zombie + i
Is a compile-time error because java does not allow an Image object to be used in combination with an int in the '+' operator.

Categories