how do I split array element into more element [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
this is my array:
[UserID|Name|Password|IC or Passport Number|Gender|Phone, 01234567|Bob|abcderf|MALE|01234567]
and I want to convert into:
[UserID|Name|Password|IC or Passport Number]
[01234567|Bob|abcderf|MALE|01234567]
this is my code: (but it doesn't work]
String[] arr = userList.toArray(new String[userList.size()]);
String[] arrr = arr[0].split(",");
for ( int i=0; i<arr.length;i++)
{
System.out.println(arr[i]);
}

You can simply use method from Arrays if lenght of array is always the same
String[] array1 = Arrays.copyOfRange(array, 0, 4);
String[] array2 = Arrays.copyOfRange(array, 4, 9);

Related

how to remove multiple characters by their indice from a string in java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can I remove multiple characters by their index from a string. I thought to use StringBuilder's deleteCharAt function. But if I delete char one by one, I will not keep track the right index.
For example :
String test = "0123456789"
int[] removingIndice = int[] {2, 4, 0};
// remove character from string
// result = "1356789"
Create a new string builder,
iterate on string, add elements to builder if its index not in the array
StringBuilder sb = new StringBuilder("");
for(int i = 0; i< test.length; i++){
if( !ArrayUtils.contains(removingIndice, i))
{
sb.append(test.charAt(i));
}
}
test = sb.toString();
String is immutable in Java, so you will need to create a new String with characters at positions 0,2,4 removed. As one option, you may use StringBuilder for that, as answered here: How to remove single character from a String
I think, you need to redesign the task:
"new string must contains all characters except ..."
Now, seems weak initial data structure and the goal.

All combinations of ArrayList<ArrayList<String>> Lists in JAVA [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need help with a implementation of a function that gives me all combinations of ArrayList<ArrayList<String>> (not List<List<String>>) List in fix order.
An Example for what I need:
From:
List1:[1,2]
List2:[3,4]
List3:[5,6]
To:
String 1 "1,3,5"
String 2 "1,3,6"
String 3 "1,4,5"
String 4 "1,4,6"
String 5 "2,3,5"
String 6 "2,3,6"
String 7 "2,4,5"
String 8 "2,4,6"
All Strings in ArrayList<String> Object
Please help me, I have no idea how to implement it.
PS: The size of ArrayList<ArrayList<String>> is variable.
PSS: ...and the size from the ArrayList lists are variable.
After little changes from the old rev it works now (ArrayIndexOutOfBoundsException and deleting to much on result):
void generate(ArrayList<ArrayList<String>> lists,
ArrayList<String> result, int index) {
if (index >= lists.size()) {
System.out.println(String.valueOf(result));
return;
}
ArrayList<String> list = lists.get(index);
for (int i = 0; i < list.size(); i++) {
result.add(list.get(i));
generate(lists, result, index + 1);
result.remove(result.size() - 1);
}
}
}

I want to split into `String and `int` arrays Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have text data and I want to split into String and int arrays Java
String text = "one1two20three300four4000";
Desired output:
String str[] = "one","two","three","four";
int num[] = 1,20,300,4000;
This one will work for you in Java :
public static void main(String[] args) {
String text = "one1two20three300four4000";
String arr1[] = text.replaceFirst("^\\d+", "").split("\\d+");
String arr2[] = text.replaceFirst("^\\D+", "").split("\\D+");
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2)); // parse each value as an in using Streams (preferably) or a loop.
}
O/P :
[one, two, three, four] [1, 20, 300, 4000]
PS : In java 8 Use int[] arr2Int = Arrays.stream(arr2).mapToInt(Integer::parseInt).toArray(); to convert your String array to int array.

How to convert ArrayList of Strings to ints, so they can be added? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to convert each String in an ArrayList to int so then I can add them,
asuma is an ArrayList containing String
I need to iterate over this ArrayList to convert String to int
for (int i = 0; i <=asuma.size(); i++) {
and then I need to add the integers. How do I do it?
I suspect that the ArrayList you are referring to is asuma and that it is an ArrayList<String>. In that case, you can do this:
for (String s : asuma) {
int valorFinal = Integer.parseInt(s);
}
I think u try that:
for (int i = 0; i <=asuma.size(); i++) {
int valorFinal = Integer.parseInt(asuma.get(i));
}

String Array to Integer Array Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a string array for an app I am making. It loads an EditText, so I will instead create a dummy one.
String[] s = ["1","2","3"];
How would I change String s into an integer array, such as Int i?
Int[] i = [1,2,3];
I have tried Integer.parseInt on the string array but it does not work. Does anyone know how to convert string arrays into integer arrays?
Something like the following should work:
string[] s = ["1","2","3"];
int[] i = new int[s.length()];
for (int j = 0; j < s.length(); j++)
i[j] = Integer.parseInt(s[j]);

Categories