Jump to content

reading a text file

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
deelicious

deelicious

    Newbie

  • Members
  • Pip
  • 4 posts
Hey guys,

Im not much of a good programmer but im really trying to work on it. I have an assignment im doing which i need to read some data from a txt file. The problem is the data is in different formats and well ok this is how it looks like,

California 109 66 2 0.9 0 90

So i have a file with records like the above and the first is the town name and then the longitude in degrees mins and seconds and lastly the latitude. So i need to calculate the distance for the city using the harvesine formula so will need to read these inputs latitude and longitude. I dont know how i can read each of the figures as in longitude degrees, longitude minutes, longitude seconds and as well for latitude. I already have a class iv called coodinate that has degrees, minutes and seconds as attributes. but now mayb i could use a method with an algorithm like this

readline (townName, long.degrees, long.minutes, long.seconds, latitude.degrees and so on....). But now to write the code i dont know how i only have an idea of the c++ way but not for java. Please help me on how i can read this input file. Thanx in advance,

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
readline (townName, long.degrees, long.minutes, long.seconds, latitude.degrees)
Doesn't exists in Java.

I think it's easiest to just do a readline and store it in a String, reading the whole line. Followed by something like String[] stringArray = newline.split(' '); (if the space gives a problem, i think \s is the special character for space)
And then
townName = stringArray[0];
degrees = Integer.ParseInt(stringArray[1];

and so on and on...

To read from a file it'll be something like this: (many examples on google ;) )

try {
    BufferedReader in = new BufferedReader(new FileReader("infilename"));
    String str;
    while ((str = in.readLine()) != null) {
        String[] stringArr = str.split(' ');
        townName = stringArray[0];
        degrees = Integer.ParseInt(stringArray[1];
        .......
    }
    in.close();
} catch (IOException e) {
}


#3
deelicious

deelicious

    Newbie

  • Members
  • Pip
  • 4 posts
hey thanx a lot for the reply. i used the method u gave me but now wen i display the data in my file using for( int i = 0; i < stringArr.length; i++ )
{
System.out.println( stringArr[ i ] );
}
its only showing me data for like 6 cities instead of 15 AND the first city its name is not shown only the longitude and latitude. I have been trying to play around with the code but still cannot get to display the data correctly. im sure im just missing something small, though cant figure out what it is

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
StringArr isn't the length you need. StringArr.length gives you the splitlength of the last row.

(and split takes String as an argument, not char).

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts

Quote

StringArr isn't the length you need. StringArr.length gives you the splitlength of the last row.

(and split takes String as an argument, not char).
-True.
-True

Any chance you could post your code?

#6
deelicious

deelicious

    Newbie

  • Members
  • Pip
  • 4 posts
Thanx guys for the help i managed to solve it and will be submitting the assignment today. Thanx ya'll,