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;
}
Related
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]".
I want use only input japanese language but I don't know how to check between japanese input and anphabe input(sometime user could input a japanese character but missing something)
So what is my missing idea. I using a dictionary with two for loop but the performance so bad.
This is my code:
public List<String> getAnswer(String rawData) {
int length = rawData.length();
List<String> result = new ArrayList<String>();
for (int i = 0; i < length; i++) {
for (int j = 0; j < length - i; j++) {
String subString = rawData.substring(i, i + j);
if (mDict.containsKey(subString)) {
result.add(mDict.get(subString));
break;
}
}
}
return result;
}
And my dictionary:
<string-array name="dict_en">
<item>a</item>
<item>i</item>
<item>u</item>
<item>e</item>
<item>o</item>
</string-array>
<string-array name="dict_ja">
<item>あ</item>
<item>い</item>
<item>う</item>
<item>え</item>
<item>お</item>
</string-array>
My problem is I have around 1000+ records in an Android App
string field1;
string field2;
string field3;
string field4;
//...
I want to search in this set of records and get the best results on two fields (field1 and field2).
Currently I read each record and compare() (string compare) with the text i want to search so that takes a long time.
What is the best method to perform search?
Store each records in SQLite DB and do "select query where like"
Hash-Mapped
? any other suggestions?
Or may be create an Index of the records and do search.
If you want to search for not exact matches, I would try to make an ArrayList of MyAppRecord where
public class MyAppRecord {
private String record;
private int deviance;
}
and get for each record the deviance of the String you want to find with:
public static int getLevenshteinDistance (String s, String t) {
if (s == null || t == null) {
throw new IllegalArgumentException("Strings must not be null");
}
int n = s.length(); // length of s
int m = t.length(); // length of t
if (n == 0) {
return m;
} else if (m == 0) {
return n;
}
int p[] = new int[n+1]; //'previous' cost array, horizontally
int d[] = new int[n+1]; // cost array, horizontally
int _d[]; //placeholder to assist in swapping p and d
// indexes into strings s and t
int i; // iterates through s
int j; // iterates through t
char t_j; // jth character of t
int cost; // cost
for (i = 0; i<=n; i++) {
p[i] = i;
}
for (j = 1; j<=m; j++) {
t_j = t.charAt(j-1);
d[0] = j;
for (i=1; i<=n; i++) {
cost = s.charAt(i-1)==t_j ? 0 : 1;
// minimum of cell to the left+1, to the top+1, diagonally left and up +cost
d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1), p[i-1]+cost);
}
// copy current distance counts to 'previous row' distance counts
_d = p;
p = d;
d = _d;
}
// our last action in the above loop was to switch d and p, so p now
// actually has the most recent cost counts
return p[n];
}
}
save it to your MyAppRecord-object and finally sort your ArrayList by the deviance of its MyAppRecord-objects.
Note that this could take some time, depending on your set of records. And NOTE that there is no way of telling wether dogA or dogB is on a certain position in your list by searching for dog.
Read up on the Levensthein distance to get a feeling on how it works. You may get the idea of sorting out strings that are possibly to long/short to get a distance that is okay for a threshold you may have.
It is also possible to copy "good enough" results to a different ArrayList.
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.
I am trying to check the spelling of a word in code using the api's for ics.
I have used the spell checker sample as a starting point and using this I can get a list of suggested words based on the query word, however I can't see how just to check that the word is spelled correctly.
I have the code below from the example and using this I can see and check the suggestions but not the original word.
#Override
public void onGetSuggestions(final SuggestionsInfo[] arg0) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < arg0.length; ++i) {
// Returned suggestions are contained in SuggestionsInfo
final int len = arg0[i].getSuggestionsCount();
sb.append('\n');
for (int j = 0; j < len; ++j) {
sb.append("," + arg0[i].getSuggestionAt(j));
}
sb.append(" (" + len + ")");
}
runOnUiThread(new Runnable(){
#Override
public void run(){
if(arg0[0].getSuggestionsAttributes()==SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY){
mMainView.append(sb.toString());
}
}
});
}
I have added the if statement that checks for RESULT_ATTR_IN_THE_DICTIONARY but I don't know if this is checking the original word or the first suggestion. (If I enter 'ton' as a query I get 'ten' returned however if I enter 'twn' as a query I get no words returned)
What I really need is for either a correct word or empty string to be returned to a query.
Any help would be appreciated,
Thanks
for (int j = 0; j < len; ++j) {
if(arg0[i].getSuggestionAt(j).toString().contains(string)){
sb.append("," + arg0[i].getSuggestionAt(j));
Log.e("check if", ""+arg0[i].getSuggestionAt(j));
}else{
sb.append("" );
Log.e("check", ""+arg0[i].getSuggestionAt(j));
}
}
Try with this code
and also remove
if(arg0[0].getSuggestionsAttributes()==SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY){
mMainView.append(sb.toString());
}
if condition just put mMainView.append(sb.toString());
hope it's useful to you.