reading columns of data

Please, help with input of doubles, ints and character in column layout

I am trying to port in java the following Fortran code:

Program Read

Implicit None

Integer, Parameter:: wp=Kind(1.0d0)

Integer::i,lenk,error

Character(len=40)::name

Integer, Allocatable::time(:)

Integer*8, Allocatable :: vol(:)

Character (len=4),Allocatable:: tick(:)

Real (wp),Allocatable :: po(:),ph(:),pl(:),p(:)

print *, 'enter file name'

read *, name

name ='/home/hpc1367/data/' // trim(name)

Open (2,file=name)

Do!!$ just to count the number of rows

Read(2,*, iostat = error)

If (error == -1) Exit

lenk = lenk + 1

Enddo

Print *, lenk

Rewind(2) !!$go back to beginning of file

Allocate(p(lenk),tick(lenk),time(lenk),po(lenk),ph(lenk),pl(lenk),vol(lenk))

Do i=1,lenk

Read (2, *) tick(i), time(i), po(i),ph(i),pl(i),p(i),vol(i)

Enddo

Close(2)

End Program Read

the file looks like:

AABC,20050103,14.2,14.2,14.2,14.2,590

AACC,20050103,21.53,21.65,21.3,21.35,51548

AACE,20050103,29.41,29.75,28.29,28.5,77257

AAME,20050103,3.07,3.07,3.01,3.07,7927

AANB,20050103,20,20.001,18.701,18.701,3803

AAON,20050103,16.5,16.99,14.8,15.01,97040

AAPL,20050103,32.39,32.555,31.3,31.645,24764860

AATK,20050103,2.35,2.35,2.15,2.2,140212

AAUK,20050103,23.75,23.88,23.25,23.301,125183

ABAX,20050103,14.98,14.98,14.05,14.05,125680

[1541 byte] By [stephen.ba] at [2007-10-3 3:24:30]
# 1
If I'm understanding your question I think you could just do a "readLine()" and then tokeninze each line by comma. Since each line seems to have the same pattern of ints, doubles and chars you can just stuff them into whatever collection you need based on the order they are in on each
billyChilla at 2007-7-14 21:17:16 > top of Java-index,Core,Core APIs...
# 2

Thanks a lot billy.

By tokenize do you mean that I have to loop the string searching for the comma or java has the capability to read a double from the line, followed by an integer etc. pls provide a sample if you have the time.

I have looked at (maybe) all the examples posted on the web and there is nothing that shows how to read simple data like what I have. Most examples illustrate things that I will never have a need for, strangely.

stephen.ba at 2007-7-14 21:17:16 > top of Java-index,Core,Core APIs...
# 3

like the post before said:

use stringtokenizer

there is a very detailed answer and example here

http://www.discussjava.com/wforum/viewforum.php?f=7

under "Re: reading columns of data"

Message was edited by:

moskvitch

Message was edited by:

moskvitch

Message was edited by:

moskvitch

moskvitcha at 2007-7-14 21:17:16 > top of Java-index,Core,Core APIs...
# 4

This code won't compile and is not the best way but should give you and idea. You'll need to add some try-catch blocks.

Use BufferedReader.readLine() to grab each line of text.

Use StringTokenizer to break up each line. In your case the delimiter will be a comma.

Should look something like this:

BufferedReader in = new BufferedReader(new FileReader("yourFileName"));

StringBuilder sb = new StringBuilder();

StringTokenizer st;

while ((sb = in.readLine() ) != null)

{

st = new StringTokenizer(sb.toString, ",");

st.nextToken() -> your chars

st.nextToken() ->your date

st.nextToken() -> your double

etc...

}

billyChilla at 2007-7-14 21:17:16 > top of Java-index,Core,Core APIs...