0

I need help creating a loop which splits strings. So far I have the code below.

    System.out.println("*FILE HAS BEEN LOCATED*");
    System.out.println("*File contains: \n");
    List<String> lines = new ArrayList<String>();
    while (scan.hasNextLine()) 
    {
      lines.add(scan.nextLine());
    }

    String[] arr = lines.toArray(new String[0]);

    String str_array = Arrays.toString(arr);

    String[] arraysplit;
    arraysplit = str_array.split(":");

    for (int i=0; i<arraysplit.length; i++)
    {
        arraysplit[i] = arr[i].trim();
        System.out.println(arr[i]);
    }

an example of one of the strings would be

        Firstname : Lastname : age 

I want it to split the string into another array that looks like:

        Firstname
        Lastname
        age

I still encounter an error when running the code, it seems as though when I convert the array to a string, it puts commas in the string and therefore it causes problems as I'm trying to split the string on : not ,

image: IMAGE, I've circled where the commas are within the converted array string

3
  • And what's the question? Commented Nov 16, 2017 at 13:40
  • Could it be that you are making things too complex? "Firstname : Lastname : age".split("\\s*:\\s*") should give you the result you want. Commented Nov 16, 2017 at 13:41
  • 1
    This question still makes no sense at all. You call Arrays.toString(arr) which clearly and obviously writes each array item into one String using a , as the separator and yet you expect it doesn't behave like it is documented. Why? Commented Nov 16, 2017 at 14:16

2 Answers 2

5

Issue : you are using the old array arr to display values and arraysplit will have resultant values of split method so you need to apply trim() on arraysplit's elements and assign back the elements to same indexes

String[] arraysplit;
arraysplit = str_array.split(":");

for (int i=0; i<arraysplit.length; i++)
{
    arraysplit[i] = arraysplit[i].trim();
    //              ^^^^^^^^^^^^ has values with spaces
    System.out.println(arr[i]);
}

System.out.println(arraysplit[i]);

To simplify solution without (list to array and array to string complication)

1.) Create array of length as sizeOfList * 3

2.) split the list element using \\s*:\\s*

3.) Use array copy with jas index of resultant array to keep the track of the array index

    String result[] = new String [lines.size()*3];
    int j=0;
    for (int i=0; i<lines.size(); i++)
    {
        System.arraycopy(lines.get(0).split("\\s*:\\s*"), 0, result, j, 3);
        j+=3;
    }
    System.out.println(Arrays.toString(result));

You can use regex str_array.split("\\s*:\\s*"); where

\\s*:\\s* : \\s* mean zero or more spaces then : character then zero or more spaces

arraysplit = str_array.split("\\s*:\\s*");
// just use values of arraysplit
Sign up to request clarification or add additional context in comments.

2 Comments

Please look at the image I have attached to my question, as I still encounter an error when running the code, it seems as though when I convert the array to a string, it puts commas in the string and therefore it causes problems as I'm trying to split the string on : not ,
@rxbert is there is chance that your input can have any special character other than alphabets and numbers? if no then easy way is arraysplit = str_array.split("\\s*\\W\\s*");
2

Split using this regex \s*:\s*

String[] arraysplit = str_array.split("\\s*:\\s*");

details :

  • \s* zero or more spaces
  • followed by lateral character :
  • followed by \s* zero or more spaces

regex demo

1 Comment

Please look at the image I have attached to my question, as I still encounter an error when running the code, it seems as though when I convert the array to a string, it puts commas in the string and therefore it causes problems as I'm trying to split the string on : not ,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.