Hello,
I have a Java homework that I need to extract the data from ASCII Text file. Here is the ASCII text file http://weather.noaa.gov/data/nsd_bbsss.txt
72;211;KTPA;TAMPA INTERNATIONAL AIRPORT ;FL;United States;4;27-58N;082-32W;27-58-04N;082-31-33W;8;11;
72;212;KCTY;CROSS CITY AIRPORT;FL;United States;4;29-38N;083-06W;29-37-49N;083-06-31W;13;12;
72;213;KAYS;Waycross / Ware County, Ga;GA;United States;4;31-15N;082-24W;31-15N;082-24W;46;46;P
72;214;KTLH;TALLAHASSEE REGIONAL AIRPORT ;FL;United States;4;30-24N;84-21W;30-23-46N;084-21-20W;25;16;
72;215;----;Peachtree City, Ga.;GA;United States;4;33-22N;084-34W;33-22N;084-34W;262;244;
I need to extract only stations that locate in the UNited State only and unique station id. If the Station ID is ----, it will be skip
RIght now, I can get the Station ID but I need to get the Location, Latitude, and Longitude. Can everyone please help ?
Here is an example output
KTPA TAMPA INTERNATIONAL AIRPORT
Located in : FL
Latitude:27-58N
Longitude:082-32W
KCTY CROSS CITY AIRPORT
......
Here is my code that Im working so far

import java.util.Scanner; 
import java.io.*;         
import java.util.StringTokenizer;
 public class a19006
{
   public static void main (String[] args) throws IOException
   {
      String strPage;
      String strData;
      String strLine;
      Scanner myInputFile;
      int x;
      
      String LinkURL = "http://weather.noaa.gov/data/nsd_bbsss.txt";
     
      Inet net = new Inet();
      strPage = net.getURLRaw(LinkURL); 
          
      PrintWriter myOutputFile = new PrintWriter("INFORMATION.TXT");
      myOutputFile.println(strPage);
      myOutputFile.close();
      myInputFile = new Scanner(new File("INFORMATION.TXT"));
      
      for (x=0; x<8500; x++)
      {
         myInputFile.nextLine();
      }

      PrintWriter OutputFile = new PrintWriter("OUTPUT.txt");
      while (myInputFile.hasNext())
         {
            strLine = myInputFile.nextLine();
            
            StringTokenizer strTokenizer = new StringTokenizer(strLine, ";");
            
            while (strTokenizer.hasMoreTokens())
            {
               String str = strTokenizer.nextToken();
               if (str.startsWith("K") && str.length() == 4)
                  {
                  OutputFile.println(str);
                  
                  } 
            }   

         }
      OutputFile.close();
      myInputFile.close();     
   }
   
}

Recommended Answers

All 10 Replies

Uhm, don't. StringTokenizer is all but deprecated due to a few well known issues. Use a Scanner or a BufferedReader and String's split method.

Uhm, don't. StringTokenizer is all but deprecated due to a few well known issues. Use a Scanner or a BufferedReader and String's split method.

Hey I trynna use the String's split method to extract the data but look like it just the same as StringTokenizer. I have a OUTPUT.txt file that contain a list look like this
72
201
KEYW
KEY WEST INTERNATIONAL AIRPORT
FL
United States
4
24-33N
81-46W
24-33-11N
081-45-24W
Do u have any idea how can I only get the StationID, Name, Location, Latitude and Longtitude like these below.
KEYW
KEY WEST INTERNATIONAL AIRPORT
FL
24-33N
81-46W

What exactly is wrong with the method you have in your piece of code? It searches for the string that starts with K, and has a length of 4.

What output is the method generating, that is wrong?

What exactly is wrong with the method you have in your piece of code? It searches for the string that starts with K, and has a length of 4.

What output is the method generating, that is wrong?

Hey thank for reply, I need to extract the StationID, Name, Location, Latitude and Longtitude only
and the code above only generate the StationID. However, I don't know how to get those Name, Location, Latitude and the Longtitude.
For example, the line is
72;211;KTPA;TAMPA INTERNATIONAL AIRPORT ;FL;United States;4;27-58N;082-32W;27-58-04N;082-31-33W;8;11;
I need to get the text in red, so the output will be:
StationID: KTPA
NAME: TAMPA INTERNATIONAL AIRPORT
Location: FL
Latitude: 27-58N
Longitude: 082-32W

Aah, I get it.

If the contents of one line are always in the same order, you can use the following:

while (myInputFile.hasNext())
         {
            strLine = myInputFile.nextLine();
            
            StringTokenizer strTokenizer = new StringTokenizer(strLine, ";");
            
StringBuilder output = new StringBuilder();
strTokenizer.nextToken();
strTokenizer.nextToken();
output.append("StationID: " + strTokenizer.nextToken()+"\n");
output.append("Name: " + strTokenizer.nextToken());
output.append("Location: " + strTokenizer.nextToken()+"\n");
strTokenizer.nextToken();
output.append("Latitude: " + strTokenizer.nextToken()+"\n");
output.append("Longitude: " + strTokenizer.nextToken()+"\n");
        
OutputFile.println(output);

         }

I think this is the best way. Try it, maybe there are other options, but I can't think of any better at the moment.

Watch out if you hard-code your String parsing... If the file contain even an empty line in the middle or something else, it could easily screw up your code. You need to first define how each data you are expecting should look like before you do that type of parsing.

Hey Thank for helping. I just try ur code and it looking great
However, the output is in 1 line http://img88.imageshack.us/img88/7486/outputtd.jpg
I need to get the output look like this
StationID: KTPA
NAME: TAMPA INTERNATIONAL AIRPORT
Location: FL
Latitude: 27-58N
Longitude: 082-32W
Also, when I insert the strTokenizer.nextToken(); at line 14 to skip one more line, it show error at this line output.append("Longitude: " + strTokenizer.nextToken() + "\n");
Here is what the message shows
Error
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at a19006.main(a19006.java:57)

@Taywin: yes I know, this is bad and dirty. You should probably surround the block with a try/catch, and handle the errors.

@dj: The ' new line' symbol is a problem. You could try like this (actually easier):

while (myInputFile.hasNext())
         {
            strLine = myInputFile.nextLine();
 try{
            StringTokenizer strTokenizer = new StringTokenizer(strLine, ";");
 
strTokenizer.nextToken();
strTokenizer.nextToken();
OutputFile.println("StationID: " + strTokenizer.nextToken());
OutputFile.println("Name: " + strTokenizer.nextToken());
OutputFile.println("Location: " + strTokenizer.nextToken());
strTokenizer.nextToken();
OutputFile.println("Latitude: " + strTokenizer.nextToken());
OutputFile.println("Longitude: " + strTokenizer.nextToken());
 
OutputFile.println(output);
 } catch (NoSuchElementException) {
System.err.println(e.printStackTrace());
}
         }

Still, you have to make sure yourself that the string you use as input is exactly one line long, with the correct number of elements.

I just fixed my code and the message shows error: <identifier> expected
catch (NoSuchElementException)

Yeah, I forgot to add an identifier tot the exception. Add 'e' after the exception.
Wasn't that on google? Please think yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.