editing string

consider the following string '8AM-10PM'. I want to divide this string in 4 variables each holding 8, AM, 10, PM respectively.  The string may also be
as such 10AM-8PM.

how to go about doing this.

ThankYou
qader99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dhymesCommented:
This sounds like a homework assignment?
0
OviCommented:
import java.util.StringTokenizer;

public class TimeParser {
      
      public TimeParser(String time) {
            int h1 = 0;
            int h2 = 0;
            String st1 = null;
            String st2 = null;
            StringTokenizer st = new StringTokenizer(time, "-");
            String s1 = st.nextToken();
            String s2 = st.nextToken();
            if(s1.length() == 3) {
                  h1 = Integer.parseInt(s1.substring(0,1));
                  st1 = s1.substring(1,3);
            }
            if(s1.length() == 4) {
                  h1 = Integer.parseInt(s1.substring(0,2));
                  st1 = s1.substring(2,4);
            }
            if(s2.length() == 3) {
                  h2 = Integer.parseInt(s2.substring(0,1));
                  st2 = s2.substring(1,3);
            }
            if(s2.length() == 4) {
                  h2 = Integer.parseInt(s2.substring(0,2));
                  st2 = s2.substring(2,4);
            }
            System.out.println("h1 : "+h1);
            System.out.println("st1 : "+st1);
            System.out.println("h2 : "+h2);
            System.out.println("st2 : "+st2);
      }
      
      public static void main(String arg[]) {
            TimeParser p;
            if(arg[0].indexOf("-") != -1)
                  p = new TimeParser(arg[0]);
      }
}


Compile'it and runnit : java TimeParser 10AM-15PM, or java TimeParser 3AM-15PM.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.