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,
reading a text file
Started by deelicious, May 16 2010 02:57 AM
5 replies to this topic
#1
Posted 16 May 2010 - 02:57 AM
|
|
|
#2
Posted 16 May 2010 - 05:38 AM
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 ;) )
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
Posted 18 May 2010 - 12:26 AM
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
{
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
Posted 18 May 2010 - 06:45 AM
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).
(and split takes String as an argument, not char).
#5
Posted 18 May 2010 - 09:45 AM
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).
(and split takes String as an argument, not char).
-True
Any chance you could post your code?
#6
Posted 20 May 2010 - 07:31 PM
Thanx guys for the help i managed to solve it and will be submitting the assignment today. Thanx ya'll,


Sign In
Create Account

Back to top









