Parsing Text file

Reply

Join Date: Apr 2005
Posts: 5
Reputation: NSCelica is an unknown quantity at this point 
Solved Threads: 0
NSCelica NSCelica is offline Offline
Newbie Poster

Parsing Text file

 
0
  #1
Dec 1st, 2005
Alright, This is really bothering me that I can't figure this out since I took 3 terms of Java.

Alright, I have a text file that contains addresses.

ex:
Name
1234 5th St
Orlando, Fl 19191

Name2
1423 4th ave
New York, NY 09987

and so on and so on.

Originally I was trying to have it take each line and put it into an 2d array.

addyArray[addyIndex][lineOfAddy].

I could not figure out how to make a linebreak (gap) be the delimiter to separate the values.

Most of the addresses will be 3lines, but I wanted the expandability of being able to handle 4 line addies.

Can anyone help me with this? If you need more details either post in here or email me at nscelica@yahoo.com.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Parsing Text file

 
0
  #2
Dec 1st, 2005
You can try the StringTokenizer which allows special delimeters. Also, I'm not sure a 2D array is the best way to go, but can't say for a fact that it's not. If you post your parsing code we could probably help more.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 5
Reputation: NSCelica is an unknown quantity at this point 
Solved Threads: 0
NSCelica NSCelica is offline Offline
Newbie Poster

Re: Parsing Text file

 
0
  #3
Dec 1st, 2005
  1. import cs1.Keyboard;
  2. import java.util.*;
  3. import java.io.*;
  4. public class PrintAddresses
  5. {
  6. public static void main (String[] args) throws IOException
  7. {
  8. String [][] addyArray = new String[50][4];
  9. for (int j=0; j<50; j++)
  10. {
  11. for (int k=0; k<4; k++)
  12. {
  13. addyArray[j][k] = "\n";
  14. }
  15. }
  16. // FileWriter outFile = new FileWriter ("results.txt");
  17. // PrintWriter out = new PrintWriter(outFile);
  18. FileReader inFile = new FileReader (args[0]);
  19. BufferedReader in = new BufferedReader (inFile);
  20. String line = "";
  21. int i = 0, a = 0;
  22. while(in.ready())
  23. {
  24. line = in.readLine();
  25. while (line != null && line != "\n")
  26. {
  27. addyArray[i][a] = line;
  28. line = in.readLine();
  29. a++;
  30. if (line == null) line = "\n";
  31. }
  32. i++;
  33. a = 0;
  34. }
  35. for(int j=0; j<3; j++)
  36. {
  37. for(int k=0;k<4;k++)
  38. {
  39. System.out.println((j+1) + "-" + (k+1) + " " + addyArray[j][k]);
  40. }
  41. }
  42. }
  43. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC