Check length of a String in Android - android

I want to check whether the entered strings length is between 3 to 8 characters. Previously I used if condition and it worked. However when I introduced some substring from the string , one of the if statements doesnt work. Can some one help me to understand why. Thanks.
My codes is
Working Code:
text = et.getText().toString();
l = text.length();
a = text.substring(0, 1);
if (l >=9) tv.setText("Invalid length!!! Please check your code");
if (l <= 2) tv.setText("Invalid length! Please check your code");
And here, the second if statement doesnt work.
text = et.getText().toString();
l = text.length();
a = text.substring(0, 1);
c = text.substring(1, 2);
d = text.substring(3, 4);
e = text.substring(4);
if (l >=9) tv.setText("Invalid length!!! Please check your code");
if (l <= 2) tv.setText("Invalid length! Please check your code");

You will want to ensure that you handle a null string as well as ensuring your string is within the limits you want. consider:
text = et.getText().toString();
if (text == null || text.length() < 3 || text.length > 8) {
tv.setText("Invalid length, should be from 3 to 8 characters. Please check your code");
} else {
a = text.substring(0,1);
b = text.substring(1,2);
c = text.substring(3,4);
if (text.length() > 3) {
d = text.substring(4);
} else {
d = null;
}
}

You need to check the length before trying to create substrings, since if the length is too short the substring indexes are invalid. Try this:
text = et.getText().toString();
l = text.length();
if (l >= 9 || l <= 2) {
tv.setText("Invalid length!!! Please check your code");
} else {
a = text.substring(0, 1);
c = text.substring(1, 2);
d = text.substring(3, 4);
e = text.substring(4);
}

You can use like this:
editText.getText().toString().length() < 3
EditText etmobile_no;
if (etmobile_no.getText().toString("") ||
etmobile_no.getText().toString().length() <3 ||
etmobile_no.getText().toString().length() >8)
{
tv.setText("Invalid length, should be from 3 to 8 characters. Please check your code");
}

Related

Finding App Architecture using android API

Suppose we can use getPackageInfo in Package Manager in android and get any installed app's versionCode and all.
So can we find the architecture or the app? Like it's arm-v7a or arm64
I'll be very helpful to you.
Thank you
I found your question for "Java custom string encryption from a paragraph or set of strings", and would like to help because I've done something similar in c#.
I noticed that one of the largest reasons for that question being closed is because you were lacking in certain details that would help us determine what you needed.
This is an example of mine:
public static string Trans(string data) {
data = ToBinary(ConvertToByteArray(data));
string[] Adata = data.Split();
string output = "";
foreach(string word in Adata) {
int count = 0;
bool counting1s = false;
count = 0;
output = "";
foreach(var ch in word) {
if (ch == '0' && counting1s) {
counting1s = false;
if (count > 0) {
output += bit1[count - 1];
count = 0;
}
}
if (ch == '1' && !counting1s) {
counting1s = true;
if (count > 0) {
output += bit0[count - 1];
count = 0;
}
}
count++;
}
if (count > 0) {
output += (counting1s ? bit1 : bit0)[count - 1];
}
}
return output;
}
I could help you with making something in Java if youd like.

EditText number validation in Android

I have three EditText controls and I need to make sure that each is the correct number input.
The first has to be a number between 0 and 23
The second has to be a number between 0 and 59
And the third has to be a number between 0 and 1500
I saw someone make a post about an easy EditText validation using setError, example:
EditText firstName = (EditText)findViewById(R.id.first_name);
if (firstName.getText().toString().length() == 0)
firstName.setError("First name is required!");
So is there an easy way to do it like above, but making sure a number isn't > 23, 59, or 1500 (individually)?
EditText firstEditText = (EditText)findViewById(R.id.first_edit_text);
EditText secondEditText = (EditText)findViewById(R.id.second_edit_text);
EditText thirdEditText= (EditText)findViewById(R.id.third_edit_text);
int value;
value = Integer.parseInt(firstEditText .getText().toString());
if (! value > 0 && value < 23)
firstEditText.setError("Error");
value = Integer.parseInt(secondEditText .getText().toString());
if (! value > 0 && value < 59)
secondEditText.setError("Error");
value = Integer.parseInt(thirdEditText.getText().toString());
if (! value > 0 && value < 1500)
thirdEditText.setError("Error");
Integer.parseInt(String s)can convert string to integer.Then, you validate it with if, else.
However, the input may not be an integer, and you have to set android:numeric="integer" in <EditText /> tag in the .xml file.
Be careful about this, if the input string is not integer, Integer.parseInt() will throw an exception, which will cause a crash.
You want to make sure you're performing some error checking since the value is coming from the user. Try something like this:
EditText firstText = (EditText) findViewById(R.id.first);
try
{
long firstVal = Long.parseLong(firstText.getText());
if (firstVal < 0 || firstVal > 23)
firstText.setError("The value must be between 0 and 23!");
}
catch (NumberFormatException e)
{
firstText.setError("Enter an integer value!");
}
// very similar for your remaining cases
String value = firstName.getText().toString();
int int_value = Integer.parseInt(value);
if (firstName.getText().toString().length() != 0)
if (int_value < 1500) {
// do what you want
}
else if (int_value < 59) {
// do what you want
}
else if (int_value < 23) {
// do what you want
}
}else {
firstName.setError("First name is required!");
}

TextWatcher - Increase value of last char by one

What I am trying to do is this: User enters text in an edittext. Example: "ab" then when he enters a character like '<' let's say, that would cause the last char typed before that to be increased by one. In this example, it'd do "ac" ('<' should be deleted too). On onTextChanged method, I use:
if (s.length() > 0 && s.toString().charAt(s.length() - 1) == '<') { //ab<
character = s.toString().charAt(s.length() - 2); //keep b
current_string = s.toString().substring(0, (s.length() - 2)); //a
et.setText(current_string); //set a
next_character_to_integer = (int) character + 1; //b is 98 ascii, need 99
integer_to_character = (char) next_character_to_integer; // 99 = char c
et.setText(s + String.valueOf(integer_to_character)); //set a+c
length = s.length();
et.setSelection(length); //move cursor after c
}
But this gives "abqc"!!! I might be close enough but still can't find the solution. Any ideas? Thanks a lot
Try this:
if (s.length() > 0 && s.toString().charAt(s.length() - 1) == '<') {
character = s.toString().charAt(s.length() - 2);
current_string = s.toString().substring(0, (s.length() - 2));
character += 1;
et.setText(current_string + character);
length = et.getText().toString().length();
et.setSelection(length);
}
This should work.
When you call:
et.setText(s + String.valueOf(integer_to_character)); //set a+c
you are using the original value of s which you have not updated. Change the line to:
et.setText(current_string + String.valueOf(integer_to_character)); //set a+c
and see what you get.

String Multiline - Android

i got this issue and i don't know how to solve it. Here is the problem:
1 - i have a data in my database who i split into a strings[] and then i split this strings[] into another 2 strings[] (even and odd lines). Everything works fine but when i want to join all the lines into a single String i got a multi line string intead of a single line. Someone can help me?
data
abcdef//
123456//
ghijkl//
789012
code:
String text = "";
vec1 = data.split("//"); //split the data
int LE = 0;
for (int a = 0; a < vec1.length; a++) { //verify how many even and odds line the data have
if (a % 2 == 0) { //if 0, LE++
LE++;
}
}
resul1 = new String[LE];
int contA = 0, contB = 0;
for (int c = 0; c < resul1.length; c++) {
if (c % 2 != 0) {
text += " " + resul1[c].toLowerCase().replace("Á","a").replace("Ã","a").replace("ã","a").replace("â","a").replace("á","a").replace("é","e").replace("É","e")
.replace("ê","e").replace("í","i").replace("Í","i").replace("ó","o").replace("Ó","o").replace("õ","o").replace("Õ","o").replace("ô","o").replace("Ô", "o")
.replace("Ú","u").replace("ú","u").replace("ç","c").replace("_","").replace("<","").replace(">","");
contA++;
}
}
And the String looks like
abcdef
ghijkl
instead of
abcdefghijkl
You should use replaceAll() method.
text.replaceAll("\\r\\n|\\r|\\n", ""); // the method removes all newline characters

Parse big xml file in android

In my application there is a search option. If the user enters a search value, I have to get a value from the webservice. I am getting a large webservice value. In my webservice string values are coming. I am getting like <> like xml character entity reference like. I want to replace all characters and parse xml. Can anybody tell me how to do this and give an example?
I tried with StringBuffer for unescapexml character, I am getting out of memory error
public String unescapeXML(String str) {
if (str == null || str.length() == 0)
return "";
StringBuffer buf = new StringBuffer();
int len = str.length();
for (int i = 0; i < len; ++i) {
char c = str.charAt(i);
if (c == '&') {
int pos = str.indexOf(";", i);
if (pos == -1) { // Really evil
buf.append('&');
} else if (str.charAt(i + 1) == '#') {
int val = Integer.parseInt(str.substring(i + 2, pos), 16);
buf.append((char) val);
i = pos;
} else {
String substr = str.substring(i, pos + 1);
if (substr.equals("&"))
buf.append('&');
else if (substr.equals("<"))
buf.append('<');
else if (substr.equals(">"))
buf.append('>');
else if (substr.equals("""))
buf.append('"');
else if (substr.equals("&apos;"))
buf.append('\'');
else if (substr.equals(" "))
buf.append(" ");
else
// ????
buf.append(substr);
i = pos;
}
} else {
buf.append(c);
}
}
return buf.toString();
}
I tried with stream, I am not able to do it. Can anybody give an example how to do this?
You should not parse it on your own. There are better ways - SAX or DOM.
This resource contains a lot of useful inforamtion about these both ways (and code examples too): http://onjava.com/pub/a/onjava/2002/06/26/xml.html
Take a look here in order to get more details about android included parsers :
http://developer.android.com/reference/javax/xml/parsers/package-summary.html
But make your own parser with SAX is probably the best choice in your case ;)

Categories

Resources