Android Java comparing strings from a buffer [closed] - java

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 9 days ago.
Improve this question
How can you compare a String coming from a Buffer in Android to a certain string variable? My code is something like this. When I use txtVw.setText(strInput), it shows the string variable strInput, but I cannot compare it to a specific string using an if statement. For example at txtVw the word "CLOSED" is shown but at the if statement it doesn't do anything even if I compared it with a string variable that has a word of "CLOSED".
byte[] buffer = new byte[256];
inputStream.read(buffer);
int i = 0;
for (i = 0; i < buffer.length && buffer[i] != 0; i++) {
}
final String strInput = new String(buffer, 0, i);
txtVw.setText(strInput)
final String str1 = "CLOSED";
if (strInput.equals(str1)){
// *do something*
}

Related

How to assign values from array to strings 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 months ago.
Improve this question
Hello i have an array that is filled with values from database and those values i want to assign them to strings i did this code but its making error:
String[] options = s.split(",");
String visible = options[0];
String sms = options[1];
String contact = options[2];
String calllogs = options[3];
I want the values to separate in array and then by index position to delcare them on each string.
You could use this Technique.
String s = "1000";
String[] options = new String[s.length()];
for(int i=0; i< s.length(); i++)
options[i] = Character.toString(s.charAt(i));
But since you are storing chars the array could be declared as char array
You can get the value from your string like this.
String string = "1000";
char[] options = string.toCharArray();
String visible = options[0];
String sms = options[1];
String contact = options[2];
String calllogs = options[3];
or you can directly use charAt() method to get the value at a particular index.
String visible = string.charAt(0);
String sms = string.charAt(1);
String contact = string.charAt(2);
String calllogs = string.charAt(3);

Store all possible substring in String [] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to store all possible substring in String []. I tried this but got an error:
public void sub(String word){
String [] Str=new String[100];
int n=0;
for (int from = 0; from < word.length(); from++) {
for (int to = from + 1; to <= word.length(); to++) {
str[n]=word.substring(from, to);
n++;
System.out.println(str[n]);
}
}
}
What is solution?
error is: cannot find symbol, variable str, loction: class substring
Well, that fairly clearly tells you what the error is: You haven't declared str. You have declared Str, but Java's identifiers are case sensitive, str and Str are not the same identifier.
So change
String [] Str=new String[100];
to
String [] str=new String[100];
// ^--- lower case
Before, when you hadn't said what the error was, there were a couple of other things Pshemo and I (amongst others) noticed:
You have a sequence issue here:
str[n]=word.substring(from, to);
n++;
System.out.println(str[n]);
...since you're incrementing n before outputting the string, you're always going to output null. Just moving the increment fixes that:
str[n]=word.substring(from, to);
System.out.println(str[n]);
n++;
Another possible problem can occur for longer words, where number of substrings can be more then 100. In that case you should avoid creating fixed size array, but try using dynamic size collection like List
List<String> str = new ArrayList<String>();
To put or read elements here just use str.add(substring) and str.get(index).

How can I convert a string(1,234) into a number(1234) using 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 can I convert a string
(1,234)
into a number
(1234)
using java?
Use DecimalFormat
DecimalFormat format = new DecimalFormat ("#,###");
Number aNumber = format.parse("1,234");
System.out.println(aNumber.intValue());
you can use NumberFormat for that
String number = "1,234";
NumberFormat numberFormat = NumberFormat.getInstance();
int i = numberFormat.parse(number).intValue();
Try String.repaceAll()
String value = "1,234";
System.out.println(Integer.parseInt(value.replaceAll(",", "")));
String is immutable, so when you do replaceAll, you need to reassign object to string reference,
String str = new String("1,234");
str = str.replaceAll(",", "");
System.out.println(Integer.parseInt(str));
This works fine when tested.
String str = new String("1,234");
String str1=str.replace(",", "");
Integer.parseInt(str1);
try with the above code
output
1234
If speed was a major concern you may find something like this quite fast. It beat all comers in this post.
int value(String s) {
// Start at zero so first * 10 has no effect.
int v = 0;
// Work from the end of the string backwards.
for ( int i = s.length() - 1; i >= 0; i-- ) {
char c = s.charAt(i);
// Ignore non-digits.
if ( Character.isDigit(c)) {
// Mul curent by 10 and add digit value.
v = (v * 10) + (c - '0');
}
}
return v;
}

Find all substring contain a keyword [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to find all the substring of one String that contains a key word.
Ex: "This is the keyword in the string".
Output: the keyword, this is the keyword, the keyword in the string, is the keyword in ....
I am think of finding all the substrings first then try to filter one by one. But I think that would be very bad solution.
Could you please give me some advice to do that!. Thank you very much.
I have edited to just find the sequence of tokens.
Try this:
String str = "abcdefkeybncv...";
String key = "key";
int index = str.indexOf(key);
ArrayList<String> sub = new ArrayList<String>();
for (int i = 0; i < str.length(); i++) {
for (int j = 0; j <= str.length() - i; j++) {
String s = str.substring(i, i+j);
if(s.indexOf(key) >= 0){
sub.add(s);
}
}
}
System.out.println(sub);
Output for the code above:
[abcdefkey, abcdefkeyb, abcdefkeybn, abcdefkeybnc, abcdefkeybncv, abcdefkeybncv., abcdefkeybncv.., abcdefkeybncv..., bcdefkey, bcdefkeyb, bcdefkeybn, bcdefkeybnc, bcdefkeybncv, bcdefkeybncv., bcdefkeybncv.., bcdefkeybncv..., cdefkey, cdefkeyb, cdefkeybn, cdefkeybnc, cdefkeybncv, cdefkeybncv., cdefkeybncv.., cdefkeybncv..., defkey, defkeyb, defkeybn, defkeybnc, defkeybncv, defkeybncv., defkeybncv.., defkeybncv..., efkey, efkeyb, efkeybn, efkeybnc, efkeybncv, efkeybncv., efkeybncv.., efkeybncv..., fkey, fkeyb, fkeybn, fkeybnc, fkeybncv, fkeybncv., fkeybncv.., fkeybncv..., key, keyb, keybn, keybnc, keybncv, keybncv., keybncv.., keybncv...]
Build suffix array: http://en.wikipedia.org/wiki/Suffix_array
Use binary search to find your substring there
Move up and down from this point in suffix array while suffixes starts with substring

Put each character in a string into its own string - java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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.
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 have a string and I want to have that string get split up so that each individual character is in its own string. The string will vary in length as it is user inputted. Thanks in advance
If you actually want an array of strings from a string you can try this
String[] chars = myString.split("");
String str = /*Your String here*/;
char[] charArray = str.toCharArray();
String[] strArray = new String[charArray.length];
String strChars = "";
for (Character c : charArray){
int i=0;
strChars = c.toString();
strArray[i] = strChars;
System.out.println(strChars);
i++;
}
System.out.println(strArray.length);

Categories

Resources