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++;
}
Related
Let say I have 2 arrays
Array1 = 1,2,3,4,5
Array2 = a,b,c,d,e
String[] array = getResources().getStringArray(R.array.Array1);
That's work fine. But I don't want to use the code above again with another line
String[] array = getResources().getStringArray(R.array.Array2);
How do I get the below lines to work if
I had declared xxx as variable for array name
String xxx = Array1;
String[] array = getResources().getStringArray(R.array.xxx);
You can:
int xxx = R.array.Array1; //change to integer
String[] array = getResources().getStringArray(xxx); //pass the whole id
Instead of passing just the name of the resource, pass the whole ID (as integer).
If you still want to pass the name as string, you can:
String xxx = "Array1";
int resourceId = getResources().getIdentifier(xxx, "array", getPackageName());
String[] array = getResources().getStringArray(resourceId);
reference of the second option: https://stackoverflow.com/a/3476447/9038584
A simple util method in your activity or service:
String[] getArray(int id) {
return getResources().getStringArray(id);
}
Usage:
String[] array1 = getArray(R.array.array1);
I want to compare two JSONArray with the same value with different order how compare it. This code work fine if value place in the same index.
String a = "[\"ABC-110101-056079-0001\",\"CBA-111101-056079-0001\",\"BCD-110101-056079-0011\"]";
String b = "[\"ABC-111101-056079-0001\",\"CBA-110101-056079-0001\",\"BCD-110101-056079-0011\"]";
JSONArray jsonArraya = null;
JSONArray jsonArrayb = null;
try {
jsonArraya = new JSONArray(a);
jsonArrayb = new JSONArray(b);
} catch (JSONException e) {
e.printStackTrace();
}
if (jsonArraya.equals(jsonArrayb)) {
Log.i("TAG",str2 is equal to str1 = " + "true");
}
You could add the elements of each array to a SortedSet instance and compare those:
SortedSet<Object> seta = new TreeSet<>();
jsonArraya.forEach(seta::add);
SortedSet<Object> setb = new TreeSet<>();
jsonArrayb.forEach(setb::add);
Log.i("TAG", "str2 is equal to str1 = " + seta.equals(setb));
The best solution in this situation is to parse values of those arrays first using Gson into POJO files, After that create .equals() method, which will add all strings from array into Set<>.
Then iterate over one set and remove all current item from another set and remove the same elements. Both objects are the same if at the end there will be no elements in the second set.
I got an arrray like this in android the usage of Arrays.asList is different because i made the Array with different class "Person":
people = new Person[]{
new Person("Python"),
new Person("PHP"),
new Person("Pascol"),
new Person("PyCrust"),
new Person("C"),
new Person("C#"),
new Person("C++")
};
and i used Arrays.asList in this way
int index= Arrays.asList(people).indexOf("Pascol");
String tags = Integer.toString(index);
Toast.makeText(getApplication(),tags,Toast.LENGTH_SHORT).show();
But i get the value "-1"in the toast.
i could not find the fault.
The problem is that you have a list of Person objects. When you call .indexOf("Pascol"); you pass in a String. Comparing a Person with a String will return always false.Do this instead
int index = -1;
for (int i = 0; i < people.length; i++) {
if (people[i].getName().equals("Pascol")) {
index = i;
}
}
String tags = Integer.toString(index);
Toast.makeText(getApplication(),tags,Toast.LENGTH_SHORT).show();
int index = Arrays.asList(people).indexOf("Pascol");
Pascol is a String here but objects in your array are Person type objects. So indexOf method can not match a String with a Person. You need to override equals() and hashcode() and pass a Person type parameter to the indexOf which name is Pascol. Of course I assume that equality of your object depends on only name attribute.
You should implement Comparable<> in Person class
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.
how am I going to move the value of a char array to the same char array? Here's a code:
Assuming ctr_r1=1 ,
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++)
{
letters[ctr_x] = letters[ctr_x];
}
sb.append(letters);
char[] lettersr1 = sb.toString().toCharArray();
sb1.append(lettersr1);
append the "letters", then convert it to string, then convert it to char array then make it as "lettersr1" value.
what im trying to accomplish is given the word EUCHARIST, i need to take the word HARIST out and place it on another array and call it region 1 (Porter2 stemming algorithm).
The code "ctr_X = (ctr_r1 + 2)" starts with H until T. The problem is I cannot pass the value directly that's why i'm trying to update the existing char array then append it.
I tried doing this:
char[] lettersr1 = null;
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++)
{
lettersr1[ctr_x] = letters[ctr_x];
}
sb.append(lettersr1);
but my app crashes when i do that. Any help please. Thanks!
I don't understand what you're trying to do, but I can comment on your code:
letters[ctr_x] = letters[ctr_x];
This is a noop: it sets an array element value to the value it already has.
char[] lettersr1 = null;
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++) {
lettersr1[ctr_x] = letters[ctr_x];
This obviously causes a NullPointerException, since you're trying to access an array which is null. You must initialize the array before being able to modify it:
char[] lettersr1 = new char[someLength];
Additional note: you should choose better names for your variables. The names should tell what the variable represents, and they should respect the Java naming conventions (no underscores in variable names, camelCase). ctr_x, ctr_r1 and lettersr1 don't mean anything.
EDIT:
I'm still not sure what you want to do, and why you don't simply use substring(), but here's how to transform EUCHARIST to HARIST:
char[] eucharist = "EUCHARIST".toCharArray();
char[] harist = new char[6];
System.arraycopy(eucharist, 3, harist, 0, 6);
String haristAsString = new String(harist);
System.out.println(haristAsString);
// or
char[] harist2 = new char[6];
for (int i = 0; i < 6; i++) {
harist2[i] = eucharist[i + 3];
}
String harist2AsString = new String(harist2);
System.out.println(harist2AsString);
// or
String harist3AsString = "EUCHARIST".substring(3);
char[] harist3 = harist3AsString.toCharArray();
System.out.println(harist3AsString);
May be so:
String str = "EUCHARIST";
str = str.substring(3);
and after toCharArray() or smth another