How can I make this line shorter with arrays? - android

if (edittext.getText().toString().toLowerCase().contains("where") &
(edittext.getText().toString().toLowerCase().contains("you")) &
(edittext.getText().toString().toLowerCase().contains("from")))
I tried using an array for this by searching a string array with contains, but that doesn't work, just syntax error.
How can I make this line shorter by using arrays?

String[] array = {"where", "you", "from"};
String value = edittext.getText().toString().toLowerCase();
for (String word : array) {
if (value.contains(word)) {
//Do something
}
}

String lcText = edittext.getText().toString().toLowerCase();
if (lcText.contains("where") && lcText.contains("you") && lcText.contains("from")) {
// Do stuff...
}

String value = edittext.getText().toString().toLowerCase();
if (value.contains("where") & (value.contains("you") & value.contains("from"))

Related

How to remove String null in android

I want to remove String null and want to show like this Item 1, Item2, Item 3 Here is output... nullItem 1Item 2Item 3
String result;
for (Cuisine c : boxAdapter.getBox()) {
if (c.checkbox) {
result += c.name;
}
}
// result = result.substring(0,4);
Initialize your String like this:
String result = "";
instead of just:
String result;
You can do it like this, put a null check.
String result;
for (Cuisine c : boxAdapter.getBox()) {
if (c.checkbox) {
if(c.name != null)
result += c.name;
}
}
OR
You can just initialize your String with empty string like:-
String result = "";
You're appending the three box names to an initially null String (result) , solve this problem by making your String an empty string at the beginning :
String result = "";
Find the solution
String result;
for (Cuisine c : boxAdapter.getBox()) {
if ((c.name!=null)&&(c.checkbox)) {
result += c.name;
}
}
//
Use regular expression to replace non-printable characters.
String string=line.replaceAll("[^\\p{Print}]","");

Tasker variable editing add commas

I need to edit the string value in variable.
So,
00343755932
should be converted to:
0,0,3,4,3,7,5,5,9,3,2
because I must define each number as an variable array for readable one by one.
if I'm right you are trying to create an array from string. Use following code
String val = "00343755932";
int[] numberArray = new int[val.length()];
Matcher match = Pattern.compile("[0-9]").matcher(val);
int i = 0;
while(match.find()) {
System.out.println(match.group());
numberArray[i] = Integer.parseInt(match.group());
i++;
}

How to detect if a string contains a specific Word

I have this code :
if (currentLocation.distanceTo(myModel.getNearest()) < 900) {
if (said != true) {
String seriousWarning = (myModel.getNearest().getProvider());
tts.speak(seriousWarning, TextToSpeech.QUEUE_ADD, null);
said = true;
warningTxt.setTextColor(Color.RED);
}
I would like to check if there a certain word in the seriousWarning string, knowing that (myModel.getNearest().getProvider()) is the title of the nearest GPS point to the device.
Any help would be much appreciated!
try below piece of code:
boolean isPdf = stringValue.matches(".*\\b"STRING_NAME"\\b.*");
You can use contains() method.
if(seriousWarning.contains("certainword"))
{
//Do something
}
You can use regular expressions to check if a string contains a substring.
This code snippet is from the android developer documentation.
// String convenience methods:
boolean sawFailures = s.matches("Failures: \\d+");
String farewell = s.replaceAll("Hello, (\\S+)", "Goodbye, $1");
String[] fields = s.split(":");
// Direct use of Pattern:
Pattern p = Pattern.compile("Hello, (\\S+)");
Matcher m = p.matcher(inputString);
while (m.find()) { // Find each match in turn; String can't do this.
String name = m.group(1); // Access a submatch group; String can't do this.
use indexOf("String to be checkecked");
if(seriousWarning.indexOf("String to be checkecked") > -1)
{
// your code
}

Android - Check content of a String

I have a string (length 3-8) assigned to a variable (text). I want to check whether the 2nd and 3rd characters are NOT numeric (a letter or symbol or space..or anything other than numbers).
Elementary way to do this could be:
if(((text.charAt(1)-'0')>=0)&&(text.charAt(1)-'0')<10))||((text.charAt(2)-'0')>=0)&&(text.charAt(2)-'0')<10)))
{
//do nothing, since this means 2nd and/or 3rd characters in the string are numeric
}
else
{
// Your condition is met
}
You could also use REGEX's , if your checking is still more complicated.
Here is Another way to achieve this:
boolean isNumeric = true;
String test = "testing";
char second = test.charAt(1);
char third = test.charAt(2);
try {
Integer.parseInt(String.valueOf(second));
Integer.parseInt(String.valueOf(third));
} catch(NumberFormatException e) {
isNumeric = false;
}
System.out.println("Contains Number in 2nd and 3rd or both position: " + isNumeric);
You might make use of the String.IndexOf(String) method, like:
String digits = "0123456789";
String s2 = text.substring(2,3);
String s3 = text.substring(3,4);
boolean valid = (digits.indexOf(s2) > -1) && (digits.indexOf(s3) > -1);

if("a" == "a") not working [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beta);
ed_code = (EditText) findViewById(R.id.et_beta_01);
bu_ok = (Button) findViewById(R.id.bu_beta);
bu_ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String code = ed_code.getText().toString();
String target = "vsi8";
Log.v(TAG, code+"="+target);
if(code == target){
Intent intent = new Intent(BetaCheck.this, AppMenu.class);
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(), "wrong", Toast.LENGTH_SHORT).show();
ed_code.setText("");
}
}
});
}
It seems that the the if statement does not understand that the 2 values are equal.
Thanks for the help
Strings, should be compared using .equals and not ==. (== checks for reference equality and not for content equality.)
That is, change
if(code == target)
to
if(code.equals(target))
Related question:
How do I compare strings in Java?
If you want compare string values, you should use the equals() method, as in str.equals(value).
This is a common pitfal in java. Basically what aioobe said. Here's the code...
It can be tricky. If you do:
if( "a" == "a" )
You will get true because the compiler will just see two static strings that are equal just 'reuse' one. The == operator for String compares the REFERENCES, meaning it's testing to see if they are the same object. Even a case of:
String a = "a" ;
if (a == "a") {
You'll still get true because again the string gets recycled when the compiler optimizes that code to reuse the first "a" for the second to save space.
Now in the following case, we generate an empty string, manipulate it by appending "a"(not really, strings are immutable, so we end up generating a 3rd string BUT that is a different one since the JVM isn't going to waste its time looking for an existing string that's the same.
class tmp {
public static void main(String arg[]) {
String a = "" ;
a = a+"a" ;
if( a == "a" ) {
System.out.println("true") ;
}
else {
System.out.println("false") ;
}
if( "a".compareTo("a") == 0 ) {
System.out.println("true") ;
}
else {
System.out.println("false") ;
}
System.out.println("a = '" + a + "'") ;
}
}
Use code.equals(target) instead of code == target
http://bimal4u.wordpress.com/2007/03/29/what-is-the-difference-between-aequalsb-and-a-b/
Replace code == target with code.equals(target)
You should try the following code, because references of two strings, even if the strings are the same, are not the same.
if (something.equals(someOtherThing)) {
// …
}
comparing two strings wont work with == . It is only when u compare a value...
so plz try something like below
if (strcmp(code, target) == 0)
or
if (code.equals(target))

Categories

Resources