*
The moose likes Beginning Java and the fly likes How to read each column of data into a separate one dimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to read each column of data into a separate one dimensional array" Watch "How to read each column of data into a separate one dimensional array" New topic
Author

How to read each column of data into a separate one dimensional array

Ace Eclar
Greenhorn

Joined: Dec 26, 2012
Posts: 3
Hi, I'm a beginner in Java and I have this assignment for my Comp Sci class. The list below has a list of stuff for my program that I need to read in. How do you read in the data column by column rather than row by row? I have to have 4 separate arrays to read in the years (ignore the months), pressures, wind speeds, and names. Can someone help me with this?

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
forget the 59 lines for the moment.

can you break this line into the 4 variables you need
1980 Aug 945 100 Allen

so you end up wth
year = 1980
pressure = 945
windSpeed = 100
name = Allen

if you can do that, the rest is simple.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38006
    
  22
Welcome to the Ranch

Have you really been told you must use parallel arrays? That is usually poor design. You should be designing Hurricane objects. You don’t think about columns and rows … until you do database work.
Ace Eclar
Greenhorn

Joined: Dec 26, 2012
Posts: 3
Thanks for the welcoming and the help. I've coded 99% of it. Now the only thing I need is that I have to print out the statistics to a text file. I made a little note near the bottom of what I actually need to print out to a text file. How do I do that? Here's the code I have so far.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38006
    
  22
I work on the assumption that any method that long is wrong, I am afraid. Particularly a main method, which is intended for starting the application, not for doing all the work.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38006
    
  22
That method needs to be divided up; you can probably make ten small methods from it.
Ace Eclar
Greenhorn

Joined: Dec 26, 2012
Posts: 3
Campbell Ritchie wrote:That method needs to be divided up; you can probably make ten small methods from it.


Sorry, I'm not following. What do you mean by that? How do I divide up my method?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38006
    
  22
Sorry for the delay; I have been away.
You can have methods like createReaderForFile(), readLineFromFile(), createHurricaneObject() etc. You can call them all from one method which puts the whole thing together.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11156
    
  16

One of the basic tenets of good software design is that you should have many short methods, each of which does one thing and one thing only.

Your program has one method that does EVERYTHING. A better way would be to do this:


Then, when you define each of those four methods, they may call other methods (which you will also have to define). There are many reasons for doing it this way. For one, it's immediately obvious what your code does in your main method, because it is short enough I can read it all in a few moments. If i were coming in to debug your code because i need to change how it process the data, I'd know where to start narrowing my search.

Now, true-and-proper OOP design wouldn't even do it this way, but I'll leave that for someone else to flesh out.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to read each column of data into a separate one dimensional array
 
Similar Threads
GregorianCalendar object
How many class' are there in JDK?
Reading seperate text file columns into seperate arrays
Just started today, please help with basic knowledge
Two actions in a single JSP