Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: [Help] How to read each column of data into a separate one dimensional array

  1. #1
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default [Help] How to read each column of data into a separate one dimensional array

    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. Idk how to do that though.

    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


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: [Help] How to read each column of data into a separate one dimensional array

    The most straight forward thing to do is to read the file from top to bottom. Most likely you will use a BufferedReader. (There's an example of usage at java2s.com)

    I have to have 4 separate arrays to read in the years (ignore the months), pressures, wind speeds, and names.
    Consider a small child confronted with a huge jumbled heap of building blocks and charged with the task organising them by colour into tidy piles. Now they might go through the large heap finding all the red ones, then start again and find all the green ones and continue in that way until all the tidy piles had been constructed. But an infinitely more efficient approach would be to take the blocks one by one from the large heap - in the order they are most easily found - and place them into the appropriate tidy pile.

    Perhaps you could take the same approach. Read the file line by line, extract the elements you are interested in from the line, and place each into the appropriate array. (Of course you would have constructed all four arrays first.)

    ---

    I guess it's a homework problem. But it's one that reflects badly on the teacher. (Feel free to link him or her to this page.)

    The thing is that the various elements of each line belong together. "Aug? Which Aug?!" - "Aug, 1980" etc. It is together that the various elements will be sorted, or searched for etc. There is a logical intuition that cries out against putting the data into separate arrays. The teacher's business is to foster that intuition.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,207 Times in 2,180 Posts

    Default Re: [Help] How to read each column of data into a separate one dimensional array

    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?
    There is no way to read a text file other than row by row.
    If you read a line from the file you will get a row/line that contains all the columns.
    Parse the row into separate tokens for each column and then save each column's token in its own array.
    The String class's split() method is useful for separating a line into tokens.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Converting Two dimensional array into an Array list
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 11
    Last Post: September 29th, 2012, 04:23 PM
  2. How to read a 2 dimensional text file from an array?
    By seaofFire in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 9th, 2012, 07:44 AM
  3. 2 Dimensional Arrays: I cant manage to read/write them.
    By seaofFire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 2nd, 2012, 01:32 PM
  4. Replies: 3
    Last Post: October 26th, 2011, 03:37 PM
  5. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 08:13 PM