944,057 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 19135
  • Java RSS
Dec 1st, 2005
0

Parsing Text file

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSCelica is offline Offline
5 posts
since Apr 2005
Dec 1st, 2005
0

Re: Parsing Text file

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Dec 1st, 2005
0

Re: Parsing Text file

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSCelica is offline Offline
5 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Quick Project Question{
Next Thread in Java Forum Timeline: Programming for Cell Phones





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC