• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get specific value from txt file

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that it's possible to retrieve a line from a txt file but say for example my file looked like this:



It is possible to retrieve just the staff id's?
 
Tony Docherty
Saloon Keeper
Posts: 3142
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could read in the whole file and ignore any line that does not begin with "staff id". Then for lines that do begin with "staff id" use the String split() method to split the line at '=' and the id will be in the returned array at index 1.
Note: You may need to use trim() to get rid of spurious white spaces, your test for startsWith() may need to be case insensitive and you may need to handle cases where there is no id value given.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats not going to work is it? Am I on the right track though?..

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're on the right track, except that a) no line "equals" "Staff id", and b) no line even starts with "Staff id". So line 13 needs changing.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but I thought line 1 does at least? Do you know of any sample codes I could look at as everything I'm finding appears to be trying to count how many time a word appears or they want to retrieve something that appears on a certain line number.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*Edit I fixed it I was searching for "Staff id" when it should of been "staff id".I also changed .equals to .contains. Such a noob error - thanks for help guys!
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking of "startsWith", but "contains" works just as well in this case.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!