-2

This is a data I am getting from the server. I want to obtain the hours and store them in a string

(09:00-12:00) (17:00-20:30)
(12:00-16:00) (20:00-22:00)
(11:00-15:00) (19:00-21:30)
(07:00-11:00) (16:00-20:00)
(10:00-14:00) (18:30-21:00)
(05:00-09:00) (14:00-17:00)
(06:00-10:00) (15:00-19:00)

so this is the result I want

String A = 09:00
String B = 12:00
String C = 17:00
String D = 20:30
...ans so on until

String A2 = 19:00

My try with the code is basically useless so could you please help me out with this ?

So far all I could do was

public class Main
{
    public static void main(String[] args)
    {

        String data ="(09:00-12:00) (17:00-20:30)" + '\n' +
            "(12:00-16:00) (20:00-22:00)" + '\n' +
            "(11:00-15:00) (19:00-21:30)" + '\n' +
            "(07:00-11:00) (16:00-20:00)" + '\n' +
            "(10:00-14:00) (18:30-21:00)" + '\n' +
            "(05:00-09:00) (14:00-17:00)" + '\n' +
            "(06:00-10:00) (15:00-19:00)";

//          String regex ="\\(|\\)";
//          data= data.replaceAll(regex,"");
//          

        String[] splited_hyphen_1,splited_hyphen_2;

        for (int i=0;i < data.length();i++)
        {
            //Get the string from json
            //JSONObject c = jsonArray.getJSONObject(i);
            String str= data;

            //split the string with space
            String[] splited_space = str.split("\\s+");

            //split the first string with hypen
            splited_hyphen_1 = splited_space[0].split("-");

            //split the first string with hypen
            splited_hyphen_2 = splited_space[1].split("-");

            for (int j=0;j < splited_hyphen_1.length;j++)
            {
                if (splited_hyphen_1[j].contains("("))
                    splited_hyphen_1[j] =  splited_hyphen_1[j].replace("(","");

                if (splited_hyphen_1[j].contains(")"))
                    splited_hyphen_1[j] =  splited_hyphen_1[j].replace(")", "");
                //System.out.println(Arrays.asList(splited_hyphen_1));
            }


            for (int j=0;j < splited_hyphen_2.length;j++)
            {
                if (splited_hyphen_2[j].contains("("))
                    splited_hyphen_2[j] =  splited_hyphen_1[j].replace('(', ' ');

                if (splited_hyphen_2[j].contains(")"))
                    splited_hyphen_2[j] =  splited_hyphen_1[j].replace(')', ' ');


                //System.out.println(splited_hyphen_2[j]);


            }


        }




    }
}
choman
  • 787
  • 1
  • 5
  • 24

2 Answers2

1

use this split("-") ex :

 String string = "09:00-12:00"
 String Array[] = string.toString().split("-");
1

Try:

   String[] splited_hyphen_1,splited_hyphen_2;

    for(i=0;i<jsonArray.length();i++)
    {
       //Get the string from json
       JSONObject c = jsonArray.getJSONObject(i);
       String str=c.getString("key_value");

       //split the string with space
       String[] splited_space = str.split("\\s+");

        //split the first string with hypen
        splited_hyphen_1= splited_space[0].split("-");

        //split the first string with hypen
        splited_hyphen_2= splited_space[1].split("-");

       for(int j=0;j<splited_hyphen_1.length;j++)
       {
         if(splited_hyphen_1[j].contains('('))
         splited_hyphen_1[j]=  splited_hyphen_1[j].replace('(',' ');

         if(splited_hyphen_1[j].contains(')'))
         splited_hyphen_1[j]=  splited_hyphen_1[j].replace(')',' ');

       }


        for(int j=0;j<splited_hyphen_2.length;j++)
       {
         if(splited_hyphen_2[j].contains('('))
         splited_hyphen_2[j]=  splited_hyphen_1[j].replace('(',' ');

         if(splited_hyphen_2[j].contains(')'))
         splited_hyphen_2[j]=  splited_hyphen_1[j].replace(')',' ');

       }

    }

Now print the arrays splited_hyphen_1,splited_hyphen_2;

kgandroid
  • 5,507
  • 5
  • 39
  • 69
  • I tried replicating your code but I don't get the result. I have updated the code above in the first post. Also, I changed your single quotes to double quotes. Please take a look at the first post and suggest ? – choman Sep 15 '16 at 11:00