How to get counts of characters on every line in TextView? - android

My TextView has multi-line text. And I want to get counts of characters every single line. I have tried String.split("\n"), but it didn't work...

Try to splite by line using below code
String lines[] = String.split("\\r?\\n");
OR
String lines[] = String.split(System.getProperty("line.separator"));
and If you don’t want empty lines:
String lines[] = String.split("[\\r\\n]+")

Try this:
String[] lines = String.split(System.getProperty("line.separator"));
int[] counts = new int[lines.length];
for(int i = 0; i < lines.length; i++ ){
String line= lines[i].replace(" ", "").trim();
counts[i] = line.toCharArray().length;
}
Then you will get every line's counts of character even you just have one line or empty lines.

You should create a function which store each line in different String variable and then count that string length.

Related

how to loop through 2d array and make the result appear in table form in android

HI below is the code which gets the four columns data from the curson and put in the 2d array. basically there are two issues one is that i get the last value as nullnullnullnull means all for columns are fetched as null.
the seconds is that i want to print the array in multitextline or if any other widget availabe so that i get four fields in a row. like
id rule_body rule_con boole
0 abc def 1
0 a f 0
c.moveToFirst();
int i=0;
while(c.moveToNext()) {
String id = c.getString(c.getColumnIndex("id"));
String rb = c.getString(c.getColumnIndex("rule_body"));
String cn = c.getString(c.getColumnIndex("rule_cons"));
String bl = c.getString(c.getColumnIndex("boole"));
table[i][0] = id;
table[i][1] = rb;
table[i][2] = cn;
table[i][3] = bl;
++i;
}
for(int a=0;a<count_row;a++)
for(int b=0;b<count_col;b++) {
obj_ml.append(String.valueOf(table[a][b]));
}
so far i am getting all the result in a single line. any help will be appreciated.
Change your for-loop as below
for (int a=0;a<count_row;a++)
{
for(int b=0;b<count_col;b++)
{
obj_ml.append(String.valueOf(table[a][b]));
}
// add to obj_ml new line character '\n'
obj_ml.append("\n");
}

Print String[] In textView on Android

I've a String[] and i'd like print all positions this String in textview
code:
for (int i = 0; i < something.length; i++) {
myMsg.setText(something[i]);
}
but in textview, only last position is being displayed.
because you are replacing your text in your textview at every iteration by calling setText.
use append instead. like this:
for (int i = 0; i < something.length; i++) {
myMsg.append(something[i]);
}
if you don't want to use append for any reason. You can use Concatenation Operator +.
myMsg = ""; // initialize String to to empty
for (int i =0 ; i< something.length ; i++){
myMsg+= something[i];
}
You can try this too, If you dont want to use loop.
myMsg.setText(Arrays.toString(something));
or without commas and brackets
myMsg.setText(Arrays.toString(something).replaceAll("[,\\[\\]]+", " "));
or without commas and brackets and removed trailing and leading whitespace
myMsg.setText(Arrays.toString(something).replaceAll("[,\\[\\]]+", " ").trim());
Hope it helps :)

How do I grab each letter of a string in android?

I would like to create a small app, and in the app I have the user entering text in an editText. I already know the basics of editText, like how to get the text from it and enter it into a separate string variable. But after I have that string variable, how can I grab each letter of this message separately? I would assume that you need a For loop. Does anyone know of any easy way to do this?
Yes, use can use a For loop like this:
String MyEditText = myEditText.getText().toString();
int len = MyEditText.length();
char chars[] = MyEditText.toCharArray();
for(int i=0;i<len;i++){
//retrieve chars
char newChar = chars[i];
}
for (int i = 0; i < inputString.length(); i++) {
//newChar is equivalent to whatever is at position i
char newChar = inputString.charAt(i); }

Android: Fill array up to a certain length

The situation
I have got different char[] arrays with a different amount of items. To avoid the "OutOfBounds"-Error while processing them I want to standardize them.
For Example:
My Array has following items: "5;9" --> What I want it to look like: "0,0,0,0,5,9" (fill up with "0"s to 6 items)
What I tried:
char[] myarray1 = mystring1.toCharArray();
...
for(int i=0; i<6; i++){
myarray1[i] = 0;
if(i<myarray1.length-1){
myarray1[i] = myarray1[i];
}else{
myarray1[i] = 0;
};
};
My code failed, because it evokes exactly that error...
I hope somebody can help me :)
Thanks!
The reason why your solution doesn't work is that you are using the same array for everything.
After char[] myarray1 = mystring1.toCharArray(); the length of myarray1 is 2, so you cannot simply assign entry 2,3,4 and 5 in your loop. Furthermore if you want the character ´0´ to be in the string, you need to surround your zeros with apostrophes.
You can fix your solution like this:
char[] myNewArray = new char[6];
int myarrayIndex = 0;
for(int i=0; i<6; i++)
{
if(i < (myNewArray.length - myarray1.length)) {
myNewArray[i] = '0';
} else {
myNewArray[i] = myarray1[myarrayIndex++];
};
};
System.out.println(myNewArray); //Will print out: 000059
An easier solution could be this:
myarray1 = String.format("%6s", mystring1).replace(' ', '0').toCharArray();
Firstly trying to fill 0's is not going to fix the error.
Secondly your logic is not right(assuming size as 6), change it to myString.length().
And I don't understand the point of myarray1[i] = myarray1[i];.
Anyways, every array with integer size is initialized by Zero's according to Java specs. On the other hand if you want to fill it with any other value, try Arrays.fill().
I think this function will accomplish what you're trying to do.
private static String formatString(String input)
{
String FORMATTED_STRING = "0,0,0,0,0,0";
int difference = FORMATTED_STRING.length() - input.length();
return FORMATTED_STRING.substring(0, difference) + input;
}

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