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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
OviConnect With a Mentor Commented:
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
 
dhymesCommented:
This sounds like a homework assignment?
0
All Courses

From novice to tech pro — start learning today.