Extracting dates exercise

Hello,

I am very new to this forum, as well to Java programming... I learn it using Ivor Horton's book Beginning Java.

Just came at a point where I'm not able to find the error in an exercise. Maybe some of you might be able to help?

In the exercise i have to make an array containing 10 dates that have to be extracted using a StringTokenizer. Everything seems right, I can even compile the program, but get exception error when I execute the program. There must be something wrong with the StringTokenizer.

Any help would be highly appreciated

Thank you,

Here is the code:

import java.util.StringTokenizer;

publicclass ConvertingDates{

publicstaticvoid main(String[] args){

String[] dates ={"29/10/99","5/12/01","17/3/03","16/1/78","7/9/07",

"31/7/98","15/12/99","18/5/01","1/1/80","26/3/03"};

String[][] datesExtracted =new String[dates.length][3];

// Extract the dates and put them in a 10x3 array

for(int i=0; i<dates.length; i++){

StringTokenizer st =new StringTokenizer(dates[i]);

for(int j=0; j><3; j++){

datesExtracted[i][j] = st.nextToken();

}

}

// Display the array

for(int i = 0; i<dates.length; i++){

for(int j = 0; j><3; j++){

System.out.println(datesExtracted[i][j]);

}

}

}

}

[2638 byte] By [Mandolinpluckera] at [2007-11-27 10:41:54]
# 1

I get six compilation errors. Is the code you posted the same as what you're running?

I fixed the errors. I think the problem is that you didn't specify a delimiter in the StringTokenizer. From your example it's easy to guess that the delimiter should be "/".

This code works:

import java.util.StringTokenizer;

public class ConvertingDates

{

public static void main(String[] args)

{

String[] dates = {"29/10/99", "5/12/01", "17/3/03", "16/1/78", "7/9/07",

"31/7/98", "15/12/99", "18/5/01", "1/1/80", "26/3/03"};

String[][] datesExtracted = new String[dates.length][3];

try

{

for(int i=0; i<dates.length; i++)

{

StringTokenizer st = new StringTokenizer(dates[i], "/");

int numTokens = st.countTokens();

System.out.println("Date " + dates[i] + " has " + numTokens + " tokens");

for(int j=0; j><3; j++)

{

datesExtracted[i][j] = st.nextToken();

}

}

for(int i = 0; i<dates.length; i++)

{

for(int j = 0; j><3; j++)

{

System.out.println(datesExtracted[i][j]);

}

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

%

duffymoa at 2007-7-28 19:15:20 > top of Java-index,Java Essentials,New To Java...
# 2

Since when does the code tag mess up loops? Now I see where the compilation errors came from. Look at how it messes up the for loop.

Easy to fix. The code will work when you fix the errors.

%

duffymoa at 2007-7-28 19:15:20 > top of Java-index,Java Essentials,New To Java...
# 3

> In the exercise i have to make an array containing 10

> dates that have to be extracted using a

> StringTokenizer. Everything seems right, I can even

> compile the program, but get exception error when I

> execute the program. There must be something wrong

> with the StringTokenizer.

You have left out one key piece of information, the error message itself. Please paste the entire message here, and we should be better able to help you. Even better would be if you to closely read the error message (it's telling you something) and then figure out your own mistake. If that happens, please post back here regardless to let us know.

Good luck!

petes1234a at 2007-7-28 19:15:20 > top of Java-index,Java Essentials,New To Java...
# 4

> Since when does the code tag mess up loops? Now I

> see where the compilation errors came from. Look at

> how it messes up the for loop.

>

> Easy to fix. The code will work when you fix the

> errors.

>

> %

OP, never mind posting the error message, I was to slow to post here. In future messages, though, do post it and closely look at it.

To duffymo, I think the forum glitches will correct if we put spaces between the variables and the greater than or lessthan signs (again, I think, but will not stake my life on it).

petes1234a at 2007-7-28 19:15:20 > top of Java-index,Java Essentials,New To Java...
# 5

> Since when does the code tag mess up loops? Now I

> see where the compilation errors came from. Look at

> how it messes up the for loop.

>

> Easy to fix. The code will work when you fix the

> errors.

>

> %

I am surprised you never noticed that. I never really looked into it but it seems that when you omit the white spaces in the middle condition of the for-statement, it messes up the next occurrence.

Test 1:for(int i = 0; i < x; i++) {

}

for(int i = 0; i < x; i++) { // OK

}

Test 2:for(int i = 0; i<x; i++) {

}

for(int i = 0; i >< x; i++) { // MESS

}

prometheuzza at 2007-7-28 19:15:20 > top of Java-index,Java Essentials,New To Java...
# 6

You are correct, Pete. Thanks.

I've never seen that before, because I always add the spaces. - %

import java.util.StringTokenizer;

public class ConvertingDates

{

public static void main(String[] args)

{

String[] dates = {"29/10/99", "5/12/01", "17/3/03", "16/1/78", "7/9/07",

"31/7/98", "15/12/99", "18/5/01", "1/1/80", "26/3/03"};

String[][] datesExtracted = new String[dates.length][3];

try

{

for(int i = 0; i < dates.length; i++)

{

StringTokenizer st = new StringTokenizer(dates[i], "/");

int numTokens = st.countTokens();

System.out.println("Date " + dates[i] + " has " + numTokens + " tokens");

for(int j = 0; j < 3; j++)

{

datesExtracted[i][j] = st.nextToken();

}

}

for(int i = 0; i < dates.length; i++)

{

for(int j = 0; j < 3; j++)

{

System.out.println(datesExtracted[i][j]);

}

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

duffymoa at 2007-7-28 19:15:20 > top of Java-index,Java Essentials,New To Java...