Dear All,

I'm taking a course online for school on how to code with Java.

My current assignment is to have the computer read in a text file and make it split it into 5 different arrays. I am really bad with Psuedocode so if you answer like that it might not help much. I do not know how to read in a column array. I have to split the Strings and INTS seperate as well. All help would be great. Here is what I have to read in:

1980 Aug 945 100 Allen
1983 Aug 962 100 Alicia
1984 Sep 949 100 Diana
1985 Jul 1002 65 Bob
1985 Aug 987 80 Danny
1985 Sep 959 100 Elena
1985 Sep 942 90 Gloria
1985 Oct 971 75 Juan
1985 Nov 967 85 Kate
1986 Jun 990 75 Bonnie
1986 Aug 990 65 Charley
1987 Oct 993 65 Floyd
1988 Sep 984 70 Florence
1989 Aug 986 70 Chantal
1989 Sep 934 120 Hugo
1989 Oct 983 75 Jerry
1991 Aug 962 90 Bob
1992 Aug 922 145 Andrew
1993 Aug 960 100 Emily
1995 Aug 973 85 Erin
1995 Oct 942 100 Opal
1996 Jul 974 90 Bertha
1996 Sep 954 100 Fran
1997 Jul 984 70 Danny
1998 Aug 964 95 Bonnie
1998 Sep 987 70 Earl
1998 Sep 964 90 Georges
1999 Aug 951 100 Bret
1999 Sep 956 90 Floyd
1999 Oct 987 70 Irene
2002 Oct 963 80 Lili
2003 Jul 979 80 Claudette
2003 Sep 957 90 Isabel
2004 Aug 972 70 Alex
2004 Aug 941 130 Charley
2004 Aug 985 65 Gaston
2004 Sep 960 90 Frances
2004 Sep 946 105 Ivan
2004 Sep 950 105 Jeanne
2005 Jul 992 65 Cindy
2005 Jul 930 130 Dennis
2005 Jul 929 135 Emily
2005 Aug 975 85 Irene
2005 Aug 902 150 Katrina
2005 Sep 960 100 Maria
2005 Sep 979 80 Nate
2005 Sep 976 80 Ophelia
2005 Sep 985 70 Phillipe
2005 Sep 897 150 Rita
2005 Sep 979 70 Stan
2005 Sep 987 65 Vince
2005 Sep 882 150 Wilma
2005 Sep 960 100 Beta
2005 Sep 979 75 Epsilon
2006 Aug 995 65 Ernesto
2006 Sep 972 80 Florence
2006 Sep 955 105 Gordon
2006 Sep 954 110 Helene
2006 Sep 985 75 Isaac

Edited 5 Years Ago by JavaStudentCode: needed to add a detail

Sorry for the pseudocode, but I'm not going to write your homework for you.

You need to read the file and split each line into its component parts. The parts are delimited by spaces. Assuming that this is a consistent format, we can do this:

declare five arrays;  // 1

open the file;  // 2
while there are lines left to read // 3
{
  read a line //3
  split the line into parts  //4
  add each part to the appropriate array 
}

1: how will you make sure the arrays are sufficiently large? If you have the input file handy, you can just count the lines, but what if the file might change? For now, just count the lines, but think about how you'd solve this.

2: There are plenty of tutorials that will help you with basic file i/o, please try before you ask about this.

3: This will be covered in some of the tutorials mentioned in #2

4: The String class has a method called "split()". Use this. Check the sun API for details.


That should get you started. First thing you should do is open the file and read each line, printing it to the screen. Once you've done that, you can worry about splitting the lines.

Edited 5 Years Ago by jon.kiparsky: n/a

I agree with jon - though you need to make sure you store your file in the correct place or you will get an error, for example I use Eclipse so I'd store it in the same project or package as the class your working in.

As regards to the most important part - splitting the information read in - the data is separated by spaces, so for example if we wanted to split the individual line - "1980 Aug 945 100 Allen" we could use this:

// The \\s is considered an escape sequence
    String[] dummy = "1980 Aug 945 100 Allen".split("\\s");

And using some more code (for loop) to print the split string, the output would be:

1980
Aug
945
100
Allen

Then place each bit of data in it's corresponding array. Also have you considered using the StringTokenizer class?

This article has been dead for over six months. Start a new discussion instead.