• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic reading of an input file  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi so i am working on a program that needs to get several variables off one line of a .txt file.

A line looks like


I need to store "P", "0", "Wilburn Thomas" and "12/19/1973" as variables, I think with "P" being a char and the rest being Strings. I know how to store the whole line as a variable but not with multiple variables.
 
Ranch Hand
Posts: 144
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should be able to achieve this using StringTokenizer and chatAt() method. I just quickly wrote this program but you should be able to implement the same logic once line is read.



This way c1 & c2 will have P & 0 respectively.

Hope this helps.
 
Mark Captain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zeeshan Sheikh wrote:You should be able to achieve this using StringTokenizer and chatAt() method. I just quickly wrote this program but you should be able to implement the same logic once line is read.



This way c1 & c2 will have P & 0 respectively.

Hope this helps.


Thanks that did help me a lot for saving the char, is there anyway to save the Strings though?
 
Mark Captain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still desperately in search of how to finish this. The file i am reading from to get these strings is laid out like

P0 Wilburn Thomas 12/19/1973
P1 Addie Penrod 11/01/1972
...ect
C0 Atlantic Northern
C1 St. Anky Beer
C2 Allied Biscuit
C3 Leeding Engines Ltd.
..ect


The "P" represents a person that I need to store the "P" as a char (which i have done) store the next digit as String id( ids range from 1 to 3 digits), store the name as a String, and the birth date as a String.
The "C" represents a company, I need to store "C" as a char, and then the company name. I understand how to use the split method but that doesnt seem like it will work seeing as the company names have different lengths. Here is the code i am working with so far, Im really just stuck on the id # and the company names at this point.

 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Captain wrote: Im really just stuck on the id # and the company names at this point.

Why? How do you see, an 'int', a 'String' , and a 'char' and if you know any ways in Java to convert one from another?
 
Mark Captain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akhilesh Trivedi wrote:
Mark Captain wrote: Im really just stuck on the id # and the company names at this point.

Why? How do you see, an 'int', a 'String' , and a 'char' and if you know any ways in Java to convert one from another?


I am stuck on the id # becuase I do not understand how to split P0, P14, C173 ect. I used the charAt to to properly get P or C. But then I am stuck with how to get the second part, the 0, 14, or 173. My confusion comes in because they range in # of digits and have the C or P attached to the front.

The company names range in the amount of words. If they were all the same amount of words, I would be able to do this, But they are not so I am trying to figure it out. I know how to convert, but i need separate and correct variables first. Sorry if my terminology is terrible, I am trying.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the API for StringTokenizer, which advises you not to use it.
I think you will have to learn about regular expressions (regex); I think you can design a regex which will pick out the different parts of those Strings.

You should also seek the exact specification of those Strings. Where do the individual letters and numbers and spaces fall? Do they always appear in the same order? Can you have a number inside a name? Can you use the first and last occurrences of numbers to delimit the name? If you have a really strict specification, you might be able to find the locations of the different numbers and divide the text into Substrings.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you certain the lines always begin with C or P?

And who was the genius who designed that system?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Captain wrote:The "C" represents a company, I need to store "C" as a char, and then the company name. I understand how to use the split method but that doesnt seem like it will work seeing as the company names have different lengths

Assuming that everything after the CXXX part is the company name then take another look at the String.split methods. Note that there is more than one.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Captain wrote:I am stuck on the id # becuase I do not understand how to split P0, P14, C173 ect. I used the charAt to to properly get P or C. But then I am stuck with how to get the second part, the 0, 14, or 173. My confusion comes in because they range in # of digits and have the C or P attached to the front.


Any harm in keeping P0, P14 and C173 as is? Else can a substring() with beginIndex as 1 chop it off?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!