473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class cube {
  4.     String time;
  5.  
  6.     public static void main (String[] args){
  7.  
  8.         cube [] times = new cube[5];
  9.         int x = 0;
  10.  
  11.         while (x < 5){
  12.             System.out.print(x);
  13.             System.out.print(" Enter your time: ");
  14.  
  15.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  16.  
  17.             times[x] = new cube();
  18.  
  19.             try {
  20.                 times[x].time = br.readLine();
  21.                 }
  22.             catch (IOException ioe) {
  23.                 System.out.println("IO error trying to read input!");
  24.                 System.exit(1);
  25.             }
  26.             x = x + 1;
  27.         }
  28.  
  29.     }
  30.  
  31. }
  32.  
Thanks a bunch.
Jun 2 '07 #1
3 1217
blazedaces
284 100+
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:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class cube {
  4.     String time;
  5.  
  6.     public static void main (String[] args){
  7.  
  8.         cube [] times = new cube[5];
  9.         int x = 0;
  10.  
  11.         while (x < 5){
  12.             System.out.print(x);
  13.             System.out.print(" Enter your time: ");
  14.  
  15.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  16.  
  17.             times[x] = new cube();
  18.  
  19.             try {
  20.                 times[x].time = br.readLine();
  21.                 }
  22.             catch (IOException ioe) {
  23.                 System.out.println("IO error trying to read input!");
  24.                 System.exit(1);
  25.             }
  26.             x = x + 1;
  27.         }
  28.  
  29.     }
  30.  
  31. }
  32.  
Thanks a bunch.
Check out this class: http://java.sun.com/j2se/1.3/docs/api/java/sql/Time.html

The time.valueOf(String) 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
Jun 2 '07 #2
JosAH
11,448 Expert 8TB
Check out this class: http://java.sun.com/j2se/1.3/docs/api/java/sql/Time.html

The time.valueOf(String) 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
Jun 2 '07 #3
blazedaces
284 100+
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
Jun 2 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jean-Fran?ois Lacrampe | last post by:
Hello, I've got two random number/statistics questions I'd like you to review. My first question is not directly related to PHP, but will be implemented in PHP, as explained in my second...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
9
by: Rafi Kfir | last post by:
Hi, This may look as a smiple task to most of you, but to me (a beginner with C), it drives me crazy. All I want is that one function passes a two dimensional array of strings to another...
7
by: drewnoakes | last post by:
I have an application that performs custom deserialisation of object state from byte arrays. This happens very regularly, so needs to be fast. In addition, most of the strings repeat, meaning I'm...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
9
by: Omatase | last post by:
I have a set of about 6 or so strings that I need to use to generate a unique hash. This hash will become the unique key in a database so the hash has to be the same each time I gen it for any 1...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
16
by: InDepth | last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble question to the gurus is: "What have we won with the decision to have string objects immutable? Or did we won?" ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.