Android - count the number of occurrences android - android

I have a code
String a = et1.getText().toString();
int ad = 0;
for(int i =0 ; i<a.length(); i++){
if(a.charAt(i)== 'a'){
ad++;
}
}
Toast.makeText(MainActivity.this,
ad, Toast.LENGTH_LONG).show();
I gettext ok but i can't counts character 'a' in string. Can you help me?
Thanks
Another,
if i have String b = et2.getText().toString(); , b is special character, and i want count b in a string. How i can do?

Because you are sending an int to Toast.makeText() so it's looking for a resource id rather than displaying the int as a string.
String a = "asjasuhuashu";
int ad = 0;
for(int i =0 ; i<a.length(); i++){
if(a.charAt(i)== 'a'){
ad++;
}
}
Toast.makeText(this,
ad + "", Toast.LENGTH_LONG).show();
Edited as per Selvin suggestion.

you are passing Integer ad in as toast parameter which consider integer as resource id, use
Toast.makeText(MainActivity.this,
String.valueOf(ad), Toast.LENGTH_LONG).show();
this is another way of calculating character count.
String s = "hghhaahghaa";
int count = s.length() - s.replaceAll("[aA]", "").length();
replace the character you want count of in above character 'a' and 'A' both will be counted.
if you want only character 'a' then replace "[aA]" with "[a]".

Related

how to start count upto 1 until press comma(,) it should count tow

its may be silly but am confused on that this i want to start count up to one and if press comma(,)then i want to count comma only, here how i am try.
String conCount;
conCount = "1";
int countComma = conCount.length() - conCount.replace(",", "").length();
String lenVar;
lenVar = conCount;
convert = String.valueOf(countComma);
if (conCount.length() == 0) {
lenVar = "0";
} else {
textViewConCount.setText(convert);
}
String editTextString = "abc,efg,pqr,xyz";
if (editTextString.contains(",")) {
int countStringsSeperatedByComma = 0;
countStringsSeperatedByComma = editTextString.split(",").length;
System.out.println("Count of strings seperated by comma : " + countStringsSeperatedByComma);
int commaCount = countStringsSeperatedByComma - 1;
System.out.println("Count of commas : " + commaCount);
} else {
System.out.println("Count of characters in editText string : " + editTextString.length());
}
Output for above condition will be :
Count of strings seperated by comma : 4
Count of commas : 3
Suppose if your string is "abcefgpqrxyz" i.e. without comma then it will execute else part and print characters count as 12 in this case
Count of characters in editText string : 12
Your question statement is so ambiguous. Elaborate it completely and explain your end result with example. It's regarding string functions, I can give you the answer about it if I understand it :) :D
thanks for that i got that answer like that.
String varStr = editextContact.getText().toString();
//int VarCount = editextContact.getText().length();
int countStringsSeperatedByComma = varStr.split(",").length;
String convet=String.valueOf(countStringsSeperatedByComma);
textViewConCount.setText(varStr);
if (varStr.length() == 0){
textViewConCount.setText("0");
}else {
textViewConCount.setText(convet);
}

Android strings.xml character count

I'm struggling to find a way to count the amount of characters in my strings.xml.
If I select all it only tells me the total amount of ALL characters in the strings, but I want to know the real text characters without <string name="example"> and </string>
Try below one which is recommended
int stringLength=getString(R.String.example).trim().length();
or Try below method
private int getCount(String str) {
int counter = 0;
for (int i = 0; i < str.length(); i++) {
if (Character.isLetter(str.charAt(i)))
counter++;
}
System.out.println(counter + " letters.");
return counter;
}

Android arraylist.get getting the wrong value using an index

I'm at my wit's end with this one! Here's some of my code:
ArrayList<Integer> score = new ArrayList<Integer>();
ArrayList<Integer> indices;
int total = 10;
for(int c = 0; c < total; ++c)
{
score.add(c);
}
indices = new ArrayList<Integer>(total);
for(int c = 0; c < total; ++c)
{
indices.add(c);
}
Collections.shuffle(indices);
rando1 = indices.get(0);
int currentScore;
currentScore = score.get(indices.get(rando1));
Toast.makeText(getApplicationContext(),
"score location should be: " + rando1, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),
"score location is: " + currentScore, Toast.LENGTH_SHORT).show();
The toasts were to help me see what's going on. For some reason, no matter what I try, the rando1 and the currentScore will almost always be different numbers.
This is baffling me because I use rando1 on a number of other arrays (string arrays), and it always gets the correct items from the other arrays.
My question is why doesn't this get the same index item (the integer at whatever index) from the integer array as it does from the other string arrays. I've tried isolating just this code. I've tried changing various things. I've done a lot of testing. And the searches don't turn up anything too specific (but I've tried what I found there as well).
Desired output of the toasts: "score location should be: 3", "score location is: 3"
Actual output: "score location should be: 3", "score location is: 5" (replace 5 with any other number, because there's never a set pattern between what it should be and what it is).
Use the following code to get a random element ArrayList.
Random r = new Random();
currentScore = score.get(r.nextInt(score.size()))
If you want to the output, as you have, then change the following line:
currentScore = score.get(rando1);

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

Array integer Android

ok so i create an array that has integers. The array displays five number from the min and max. How can i display all five numbers in a textview or edittext ? I tried:
nameofile.setText(al.get(x).toString());
but it only displays one?
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = minint; i <= maxint; i++)
al.add(i);
Random ran = new Random();
for (int i = 0; i < 5; i++) {
int x = al.remove(ran.nextInt(al.size()));
String myString = TextUtils.join(", ", al);
lottonumbers.setText(myString);
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(0);
al.add(1);
al.add(5);
al.add(4);
al.add(3);
java.util.Collections.sort(al);//for sorting Integer values
String listString = "";
for (int s : al)
{
listString += s + " ";
}
nameofile.setText(listString);
You're currently only printing out one element (the one at index x). If you want to print them all in order, you can just join them using TextUtils.join().
Update: After seeing your edit, I think there's a better way to go about what you're trying to do. Instead of trying to pull the values one at a time, and update the list, why not just shuffle them, then use the above method?
Update 2: Okay, I think I finally understand your problem. Just a simple change, then.
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = minint; i <= maxint; i++)
al.add(i);
Random ran = new Random();
StringBuilder text = new StringBuilder(); // Create a builder
for (int i = 0; i < 5; i++) {
int x = al.remove(ran.nextInt(al.size()));
if (i > 0)
text.append(", "); // Add a comma if we're not at the start
text.append(x);
}
lottonumbers.setText(text);
al.get(x).toString() will only get the value at index "x". If you want to display all values, you need to combine all of the values from the array into a single string then use setText on that string.
You are only showing one number of your array in the TextView, you must to concat the numbers to see the others results like:
for(Integer integer : al) {
nameofile.setText(nameofile.getText() + " " + al.get(x).toString());
}
Then i think you can show all number in one String.

Categories

Resources