Split result not usable on android - java

I have a string that I split, it works perfectly until i want to use it : when I use a 'for' to read what a have in my String table I shows exactly what I want, but when I use if(MyStringTable[1] == "a") it isn't true, even though I just saw that MyStringTable[1] was equal to "a".
My string table is "static" declared.
I'm wondering if there is an invisible character or something that has been created with the split.

In terms of Strings, use .equals() in order to check if a String is equal to another. If one of them is a character, cast it previously to a String using .toString() to make it match this approach.

Related

How to test data within a string without using another string

Is it possible to test the data within a string without using another string? For example, if I wanted to test whether String size=("large") is it possible without having to create a second string with the value as large and comparing it using .equals? Is it possible to test case as well (i.e. not using .equalsignorecase)?
In a conditional statement you need to compare using the .equals(....) method or .equalsIgnoreCase(...). As far as testing for upper and lower case of characters, take a look at the Character class in the javadocs. I believe you'll find methods like: isLetter() and isUpperCase() and isLowerCase(). Just use a loop to iterate over the string and evaluate over every character.

regex to find integers in string not matching

I have two tables' contents stored in Stringbuffers. One has data in it; the other is only a header. I converted the Stringbuffers into Strings and removed whitespace.
table1:
ACCOUNT_NUMBER;BRANCH_CODE;RECALC_ACTION_CODE;RECALC_DATE;PROCESS_NO;PRINCIPAL_CHG_AMXX23QRUP120970003;023;E;05.09.2013;1;-522.53
table2:
ACCOUNT_NUMBER;BRANCH_CODE;MSG_TYPE
I only want to proceed with a table if it has data in it, like table1.
To check for data (i.e integers) I used regex: table1.matches("\\d"), but this returns false. I also tried table1.matches("(?s)\\d")), for new line character but even this returns false.
How can I check for integer data in the strings?
Read the documentation on matches. The "match" requires the entire string to match, and so your table1.matches("\\d") fails -- "table1" is not 'one digit only'.
Use table1.matches(".*\\d.*") instead. Note the double backslash! You might not be aware they need escaping in a String constant.

Hidden char StringTokeneizer/split in Java?

I have a text file and each of its line is like that
author-title-kind
I have a Java program parsing this file and it must returns only the books whose author is "example".
I read a line at a time, and then I split the string with StringTokeneizer or split().
So I will get 3 items: author, title, kind.
Then I check if the first item string is equal to "example".
The problem is that I always get false, and never true.
Is there any hidden character so that this comparison ends always with false?
Maybe I should check with "example-", or "-example"...or anything else?
Remember that String.split() takes a regular expression as a separator and not just a string. I would use apache commons StringUtils.split() if you want basic string splitting with a simple string.

Can I add a char to a variable specified position within a string?

OK, this is the line I am working on:
newstring.charAt(w) += p;
trying to add a character/char (p) to the string 'newstring' at a particular position within the string which is defined by int 'w'. Is this possible?
Strings are immutable in Java, so the answer is no. But there are many ways around it. The easiest is to create a StringBuilder and use the setCharAt() method. Or insert() if you want to insert a new character at a given position.
If you make multiple modifications to your string, you can (and indeed should) reuse your StringBuilder.
Well, you can't modify your string, because Strings are immutable in Java. If you try to change the string, you will get a new string object as a result.
Now, you can use String#substring method for that, using which you can get new string which is generated by some concatenation of substring of original string.: -
str = str.substring(0, w) + "p" + str.substring(w);
But, of course, using StringBuilder as specified in #biziclop's answer is the best approach you can follow.

Parse a substring?

I'm trying to convert the first two characters of a String using the parseInt method but I cannot. It's supposed to look like this:
String firstChars = IntMessage.substring(0,2);// firstChars is a String that corresponds to the first two characters of the string.
message=ASCII[(Integer.parseInt(firstChar))-32];//The message variable is a String that is supposed to take a firstChars variable and make it an integer so it can be used by the ASCII array in determining which element of the array is to be concatenated to the message String.
For example if the first two characters are 98, I want to take that substring and convert it into an int.
Well, other than the fact that your string is called firstChars and you're trying to parse firstChar, that should work fine.
But this is an area where you should either be using a debugger with breakpoints so you can figure out what values are being placed in the variables, or just print them out:
IntMessage before doing the substring (and shouldn't this normally start with a lower case letter if it's an object?).
firstChars after doing the substring (make sure it's numeric, for example).
Integer.parseInt(firstChars) after that, making sure it's what you expect.
Then Integer.parseInt(firstChars) - 32.
Finally, ASCII[Integer.parseInt(firstChars) - 32].
Then it will be a simple matter of examining all the outputs to see what the problem is.

Categories