• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extracting part of a string.  RSS feed

 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay So I am creating an application but I'm not sure how to get certain parts of the string. I have read In a file as such:
*tp*|21394398437984|163600
*2*|AAA|1234567894561236|STOP|20140527|Success||Automated|DSPRN1234567
*2*|AAA|1234567894561237|STOP|20140527|Success||Automated|DPSRN1234568
*3*|2

I need to read the lines beggining with *2* so I done:

So this will read the whole line I'm fine with that, Now my issue is I need to extract the 2nd line beggining with numbers the 4th with numbers the 5th with success and the 7th(DPSRN). I was thinking about using a String dilemeter with | as the dilemeter but I'm not sure where to go after this any help would be great I practically need to work out how to extract them once i've used the dilemeter. I'd be thankfull for any help.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after line 4, you have the line that you just read from the file in the variable str1. You can then use the methods of the String class to for example split it on "|". So, yes, that is a good way to continue with the problem.
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:after line 4, you have the line that you just read from the file in the variable str1. You can then use the methods of the String class to for example split it on "|". So, yes, that is a good way to continue with the problem.


Thanks jesper I got as far as adding the

But I'm trying to work out how to only read the parts that I want, I know its probably something simple, I was originally using a starts with command but obviously this is impractical and can lead to misleads etc. If you could guide me maybe on to some material which would help me with my next step that would be great.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so with that code, the array temp would contain all the parts of a line. For example, you would have:

temp[0] = "*2*"
temp[1] = "AAA"
temp[2] = "1234567894561236"
temp[3] = "STOP"
temp[4] = "20140527"
temp[5] = "Success"
temp[6] = ""
temp[7] = "Automated"
temp[8] = "DSPRN1234567"


So, you can add some lines to print the parts that you are interested in. For example:

 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Ok, so with that code, the array temp would contain all the parts of a line. For example, you would have:

temp[0] = "*2*"
temp[1] = "AAA"
temp[2] = "1234567894561236"
temp[3] = "STOP"
temp[4] = "20140527"
temp[5] = "Success"
temp[6] = ""
temp[7] = "Automated"
temp[8] = "DSPRN1234567"


So, you can add some lines to print the parts that you are interested in. For example:



Exactly what I was after thanks very much, Like has been given. Knew it was something simple completely forgot that it was supposed to be "temp" I was trying to use "i" haaha silly mistake thanks for the help and the very clear explenation.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!