• Post Reply Bookmark Topic Watch Topic
  • New Topic

reading text file with specific format  RSS feed

 
Ranch Hand
Posts: 126
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a text file with below formate

ID            Name            Age
1                            John                        25
2                            Abraham                 30
3                                                          40


I want to read data in to java class which is having (id,name,age).Issue here is that the third row having name as empty(while writing to file its empty) when I use Scanner to read it, is not considering empty space so the name coming as 40,Is there any way I can get the data as I inserted(like ID =3,name = empty/null,Age = 40).
 
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the file format is a random number of spaces as a separator?
Couldn't the code that produced this file have used tabs?
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you control the format, one solution is to use a separator between fields. Your text file could look like this:

Now, if you use a comma (optionally preceded and followed by spaces), you will get an empty string for the name field.
 
Ravi kapa
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:So the file format is a random number of spaces as a separator?
Couldn't the code that produced this file have used tabs?


Thanks for the reply dave,
Yes,I used Tab("\t") as delimiter while writing to the file,and scanner's next is skipping the empty String so the scanner.next () value (i.e 40) is coming as name.
 
Ravi kapa
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:If you control the format, one solution is to use a separator between fields. Your text file could look like this:

Now, if you use a comma (optionally preceded and followed by spaces), you will get an empty string for the name field.


Thanks for the reply Stephan,

I need data without ",".
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't you create a Scanner to read that text file? Look in its documentation to see what its default delimiter is, then work out whether you can get the ID with myScanner.nextInt().

By the way: what format does the name take? Is it always one token?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me?
I would read in line by line, and use a new Scanner on each individual line that is keyed to a tab as the separator.
That way you can use next() and nextInt() without any issue.
 
Ravi kapa
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why can't you create a Scanner to read that text file? Look in its documentation to see what its default delimiter is, then work out whether you can get the ID with myScanner.nextInt().

By the way: what format does the name take? Is it always one token?


Thanks for the reply Cambell, If I explained/understood you correctly I am using scanner for reading file.

scanner.next() for reading string (name in my case)(Sorry I did not understand what you are explaining here according my use case)

Currently I am assuming its only one token(might change in future)
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi kapa wrote:. . . Thanks for the reply Cambell . . . .
That's a pleasure
Currently I am assuming its only one token(might change in future)
It is dangerous to make such assumptions. It is also dangerous to create code which doesn't allow upgrading for future changes. When you have lines in the file like
1234567890 Campbell Ritchie 999
1234567891 Ravi Anand Kapa 23
you will need to work out how to parse them. If you assume the name occupies one token, those lines will break your code pretty quickly.

Did you know that you can read a line as a String and pass that line to a second Scanner and use methods like nextInt or hasNextInt on it? Or that you can split that line into a String[]? Either of those hints might help you parse your file.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi kapa wrote:I need data without ",".

Just because the file has commas in it doesn't mean you're processing has to use them.  I process data in what's called HL7 format all the time. it uses pipe characters '|' as separators, but the pipe itself isn't part of the data. it's all in how you scan and parse the lines you are reading.
 
Ravi kapa
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Ravi kapa wrote:. . . Thanks for the reply Cambell . . . .
That's a pleasure
Currently I am assuming its only one token(might change in future)
It is dangerous to make such assumptions. It is also dangerous to create code which doesn't allow upgrading for future changes. When you have lines in the file like
1234567890 Campbell Ritchie 999
1234567891 Ravi Anand Kapa 23
you will need to work out how to parse them. If you assume the name occupies one token, those lines will break your code pretty quickly.

Did you know that you can read a line as a String and pass that line to a second Scanner and use methods like nextInt or hasNextInt on it? Or that you can split that line into a String[]? Either of those hints might help you parse your file.


Yes Campbell,Understand your point that code should allow future upgrades, but at that moment my priority is just one token.I know there are couple of ways to parse data,will work further on that.

Thanks (smile)
 
Ravi kapa
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Ravi kapa wrote:I need data without ",".

Just because the file has commas in it doesn't mean you're processing has to use them.  I process data in what's called HL7 format all the time. it uses pipe characters '|' as separators, but the pipe itself isn't part of the data. it's all in how you scan and parse the lines you are reading.


My point of saying "I don't need ","" is that .I need to write the data without ",".So the client need not see , if I give them text file.Sorry if I misunderstood your explanation.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi kapa wrote:. . . at that moment my priority is just one token. . . .
I would suggest you work out how to parse names with any number of tokens; that way you won't have to rewrite lots of code when two‑token names appear.
Thanks (smile)
That's a pleasure
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read an interesting article the other day that pointed out how unfortunate we ended up with CSV when ASCII has purpose-built separator characters:

\u001C (28) - FS - file separator
\u001D (29) - GS - group separator
\u001E (30) - RS - record separator
\u001F (31) - US - unit separator

Instead, we have to deal with commas, tabs, or pipe characters, quoting vs not quoting, and of course escaping any of these characters that should be taken as part of the data.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!