• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How do you read text file records (Scanner) when records length vary?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, and thank you in advance for your help! I really love this place.

Using the MVC pattern for the very first time, my GUI is going to read a short text file and show it in the JPanel. The text will look like this in the gui:

"ID Customer Address City State Phone
101 Modern Quals 21 Main St. Fairfield CT 203 254-9563
102 Affordable Quals 456 State St. New Haven CT 203 456-2345 "

But in the given text file, it looks like like this: "ID Customer Address City State Phone 101 Modern Quals 21 Main St. Fairfield CT 203 254-9563 102 Affordable Quals 456 State St. Bridgeport CT 203 456-2345" Some of them use spaces, some use tabs, though I think each line is separated with a carriage return, that might be helpful!

So using a Scanner, I am reading the text, and placing them into the Customer class's variables. The problem is that some values are of varying length, such as one customer's city being 1 word, and another being 2 words, "New Haven." I am unsure how to go about setting these values to the correct variables in a loop if each line varies in length. Here is the code to illustrate:



Thank you again!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sabin mash wrote:Some of them use spaces, some use tabs, though I think each line is separated with a carriage return, that might be helpful!


If it has lines, my suggestion would be to use hasNextLine() and nextLine(), and then String.split("\\s+") to break up the individual fields. The "\\s" stands for 'whiitespace', which includes spaces and tabs.

Winston
 
Marshal
Posts: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that using text files like this introduces tight coupling between the format of the text and the program which reads and interprets it. So you will have to be careful about how you split the lines. There are more robust techniques available, eg XML, databases, but these require more knowledge to use.
You will have to be careful about the difference between Fairfield and New Haven. Becasue one town is a single word, and the other is two words, you need to work out how many tokens to count. What about the name of the streets? What if you had a line like this?

999 ACME Quals 123 George Washington Road New Haven CT 203 123‑4567

What about a different delimiter? I had to create a rather similar file for somebody recently and they recommended pipes as delimiters. Like this

ID Customer Address City State Phone
101|Modern Quals|21|Main St.|Fairfield|CT|203|254-9563
102|Affordable Quals|456|State St.|New Haven|CT|203|456-2345

Note the pipe is a metacharacter for regular expressions, so you must escape it "\\|"
 
sabin mash
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thee suggestions worked great! I used a /t delimeter. Thank you again!
 
It's a tiny ad only because the water is so cold.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic