0

I have this string:

   fname lname, GTA V: 120 : 00000, Minecraft : 20 : 10, Assassin’s Creed IV : 90 : 800, Payday 2 : 190 : 2001 ,Wolfenstein TNO : 25 : 80, FarCry 4 : 55 : 862

I want to use a loop to split this string into an array at the comma [,] example:

[0]fname lname
[1]GTA V: 120 : 00000
[2]Minecraft : 20 : 10
[3]Assassin’s Creed IV : 90 : 800
[4]Payday 2 : 190 : 2001
[5]Wolfenstein TNO : 25 : 80
[6]FarCry 4 : 55 : 862

Then I want to use another loop to split this further at : into another array example

[0]fname lname
[1]GTA V
[2]120
[3]00000
[4]Minecraft
[5]20
[6]10
....

Is there a better way of doing this?

currently I have:

    List<String> lines = new ArrayList<String>();
    while (scan.hasNextLine()) 
    {
      lines.add(scan.nextLine());
    }

    //converts the list array to string array 

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

    //converts the string array into one large string

    String str_array = Arrays.toString(scanarray);

    String[] arraysplit;


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

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

EDIT:

Currently my program creates 3 identical arrays, containing the example you can see in the second block of code above.

2
  • How do you define "better"? What is your goal? Commented Nov 17, 2017 at 10:01
  • Better in the sense that, I can just use one loop to do what is specified as appose to using two loops. Commented Nov 17, 2017 at 10:02

3 Answers 3

2

You can use the split method from String class with multiple delimiters

public static void main(String[] args) {
    String myOriginalString = "   fname lname, GTA V: 120 : 00000, Minecraft : 20 : 10, Assassin’s Creed IV : 90 : 800, Payday 2 : 190 : 2001 ,Wolfenstein TNO : 25 : 80, FarCry 4 : 55 : 862";
    // | is the regex OR operator
    String[] splited = myOriginalString.split(",|:");
    for(String s : splited)
    System.out.println(s.trim());
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can achieve it what you are looking for with REGEX, just put what all thing you get separated with string split method.

I tried below code locally and it is pretty much same what you are looking for.

public class StackSol1 {

    public static void main(String[] args) {



        String str = "fname lname, GTA V: 120 : 00000, Minecraft : 20 : 10, Assassin’s Creed IV : 90 : 800, Payday 2 : 190 : 2001 ,Wolfenstein TNO : 25 :80, FarCry 4 : 55 : 862";
        String delimiters = "\\s+|,\\s*|\\:\\s*";

        // analyzing the string
        String[] tokensVal = str.split(delimiters);

        // prints the number of tokens
        System.out.println("Count of tokens = " + tokensVal.length);

        String finalStr="";

        for (String token : tokensVal) {

            finalStr = finalStr+"\n"+token;
        }

        System.out.println(finalStr);
    }

}

Comments

0

How about using split with regex? e.g.

String aa = "fname lname, GTA V: 120 : 00000, Minecraft : 20 : 10, Assassin’s Creed IV : 90 : 800, Payday 2 : 190 : 2001 ,Wolfenstein TNO : 25 : 80, FarCry 4 : 55 : 862";
String [] a = aa.split("[,:]");

Comments

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.