Strings Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • allabout42
    New Member
    • Jun 2007
    • 1

    #1

    Strings Question

    I'm trying to teach myself Java in preparation for a CS class next semester. I've read a few chapters of an introductory Java book. I thought I write a program for fun, where it would take a bunch of times (5 to be exact) and average them. The format would be in min:sec (eg 1:41). My question is that, is there a procedure or method in Java that can check if the ":" is a member of the input? Also are there similar "car" or "cdr" procedures in Java too?

    Here's my code:
    Code:
    import java.io.*;
    
    public class cube {
    	String time;
    
    	public static void main (String[] args){
    		
    		cube [] times = new cube[5];
    		int x = 0;
    		
    		while (x < 5){
    			System.out.print(x);
    			System.out.print(" Enter your time: ");
    
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    			
    			times[x] = new cube();
    				      
    			try {
    				times[x].time = br.readLine();
    				}
    			catch (IOException ioe) {
    				System.out.println("IO error trying to read input!");
    				System.exit(1);
    			}
    			x = x + 1;
    	    }
    
    	}
    
    }
    Thanks a bunch.
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    Originally posted by allabout42
    I'm trying to teach myself Java in preparation for a CS class next semester. I've read a few chapters of an introductory Java book. I thought I write a program for fun, where it would take a bunch of times (5 to be exact) and average them. The format would be in min:sec (eg 1:41). My question is that, is there a procedure or method in Java that can check if the ":" is a member of the input? Also are there similar "car" or "cdr" procedures in Java too?

    Here's my code:
    Code:
    import java.io.*;
    
    public class cube {
    	String time;
    
    	public static void main (String[] args){
    		
    		cube [] times = new cube[5];
    		int x = 0;
    		
    		while (x < 5){
    			System.out.print(x);
    			System.out.print(" Enter your time: ");
    
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    			
    			times[x] = new cube();
    				      
    			try {
    				times[x].time = br.readLine();
    				}
    			catch (IOException ioe) {
    				System.out.println("IO error trying to read input!");
    				System.exit(1);
    			}
    			x = x + 1;
    	    }
    
    	}
    
    }
    Thanks a bunch.
    Check out this class: http://java.sun.com/j2se/1.3/docs/api/java/sql/Time.html

    The time.valueOf(St ring) does exactly what you want, make a Time class object from a String of format hh:mm:ss.

    And I'm sorry I don't know what car and cdr functions represent...

    Good luck, hope I helped,
    -blazed

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by blazedaces
      Check out this class: http://java.sun.com/j2se/1.3/docs/api/java/sql/Time.html

      The time.valueOf(St ring) does exactly what you want, make a Time class object from a String of format hh:mm:ss.

      And I'm sorry I don't know what car and cdr functions represent...
      Almost everything in that Time class is deprecated; better use a DateFormat
      class for this (or a subclass thereof). btw, 'car' and 'cdr' are Lisp functions:
      content of address register and content of data register which cryptically mean
      the head (first element) and tail (everything but the first element) of, say, a list.

      kind regards,

      Jos

      Comment

      • blazedaces
        Contributor
        • May 2007
        • 284

        #4
        Originally posted by JosAH
        Almost everything in that Time class is deprecated; better use a DateFormat
        class for this (or a subclass thereof). btw, 'car' and 'cdr' are Lisp functions:
        content of address register and content of data register which cryptically mean
        the head (first element) and tail (everything but the first element) of, say, a list.

        kind regards,

        Jos
        Good to know, thank you very much.

        -blazed

        Comment

        Working...