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

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.

Related

how do I split array element into more element [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 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);

How to remove leading zero's in between characters of a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have dates like this in string that need leading zero's removed.
Here is what I need:
Remove leading zero's in between [- and :
Below are some of the examples and expected results
20200831000000.000[-05:EST] -> 20200831000000.000[-5:EST]
20200831000000.000[-08:30:EST] -> 20200831000000.000[-8:30:EST]
20200831000000.000[-10:EST] -> 20200831000000.000[-10:EST]
I can't figure this out as I'm new to regular expressions. I have tried the below but not working
"0+(?!$)"
this will work like you went :
public static void main(String[] args) {
String arr[] = {"20200831000000.000[-05:EST]","20200831000000.000[-08:30:EST]","20200831000000.000[-10:EST]"};
for(int i=0; i < arr.length ; i++){
arr[i] = arr[i].replaceAll("(?<=\\[-?)(0*)(?=.*\\])", "");
}
for(String s : arr){
System.out.println(s);
}
}
output :
20200831000000.000[-5:EST]
20200831000000.000[-8:30:EST]
20200831000000.000[-10:EST]

How to convert Stream of Characters into a String in Java 8 [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 4 years ago.
Improve this question
How to convert Stream<Character> into a String in Java 8? Collectors.joining() expects CharSequence hence it is giving compilation error.
Refer to #jubobs solution link. That is, you could do it this way in your case:
Stream<Character> testStream = Stream.of('a', 'b', 'c');
String result = testStream.collect(Collector.of(
StringBuilder::new,
StringBuilder::append,
StringBuilder::append,
StringBuilder::toString));
This is more performant then map/castping each character to a String first and then joining, as StringBuilder#append(char c) will cut out that intermediate step.
Convert Character to String
Stream<Character> st = Stream.of('C','h','t');
String result = st.map(c->c.toString()).collect(Collectors.joining());
System.out.println(result); //Cht
Or by using method reference
st.map(Object::toString).collect(Collectors.joining())
And Collectors.joining internally uses StringBuilder here
Or just by using forEach
StringBuilder builder = new StringBuilder();
Stream<Character> st = Stream.of('C','h','t');
st.forEach(ch->builder.append(ch));

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.

String and imag array random in NetBeans? [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 8 years ago.
Improve this question
How can I generate a String and an image array in NetBeans? I try to make the arrays like
string{"lion","cat","dog","bird"};
How can I use the Random class to get s[i] where i is random?
String [] animals = {"lion","cat","dog","bird"};
int rndindex = (int)(Math.random()*number_of_element);
String rndAnimal = animals[rndIndex];
Make the array, get a random number between 0 and n inclusively where n is the number of elements in the array. Then retrieve that element.
java.util.Random rand = new java.util.Random();
int index = rand.nextInt(4);
System.out.println(string[index]);
If I understand your question, one solution is to use Collections.shuffle(List). I know, you wanted to do it with a String[] bute we can use Arrays.asList(T...) and use the fact that this List<String> is backed by a String[] like so,
public static void main(String[] args) {
String[] animals = { "lion", "cat", "dog", "bird" };
System.out.println(Arrays.toString(animals));
Collections.shuffle(Arrays.asList(animals));
System.out.println(Arrays.toString(animals));
}
Output is
[lion, cat, dog, bird]
[cat, lion, dog, bird]
Edit
If you just want one element at a random position in the array, that would be
String[] animals = { "lion", "cat", "dog", "bird" };
System.out.println(animals[rand.nextInt(animals.length)]);

Categories