-1

EDIT------------- Seems my explanation wasn't all that good so here is a better one of what I need to do. Here is an example file(ignore the space between lines):

10=android|156.94.34.212|Device 1|5|9

15=IOS|134.58.48.343|Data|3|7

I want to parse each line. I need to store each part of the line excluding the "=" and the "|" characters and the last 2 numbers are also irrelevant. So the 5,9,3 and 7 I don't need. My problem is when I come across Device 1. I need to save that space and the 1 but can't figure out how. This would be what I need to show for the first line:

id = 10

device = android

ipAddress = 156.94.34.212

deviceName = device 1

Sample Code:

Scanner scan = new Scanner(file);
while(scan.hasNextLine()){
    Scanner id = new Scanner(scan.next()).useDelimiter("=");
    String deviceID = id.next();
    Scanner dataScan = new Scanner(id.next()).useDelimiter("[|]");
    String device = dataScan.next();
    String address = dataScan.next();
    String deviceName = dataScan.next();
    scan.nextLine();
 }

Printout leaves off the 1 in "device 1" everything else parses correctly.

  • 1
    @ochi Nope, both questions aren't related. Vote to close as "unclear" or "why isn't this code working" would be sufficient, since this question can't be answer without OPs code. – Tom Jun 29 '16 at 00:24
  • @Tom I was thinking along the lines of @ iciaran's answer -> read the line, split by the known delimiters (but I did not fully articulate that, my bad) – blurfus Jun 29 '16 at 00:26
  • @ochi OPs questions reads a bit like he actually does that currently, but that is why this question is unclear (at least to me), because the results he gets are strange. – Tom Jun 29 '16 at 00:28
  • @Tom to me, OP is using `.next()` to read the input from scanner - which is why `.nextLine()` is a better choice IMHO – blurfus Jun 29 '16 at 00:29
  • @ochi *"I can parse the whole file and all the lines however"* doesn't sound like he parses lines with `#next()`, because he wouldn't know when a line ends. Or he calls `#next()` five times as a "group", which would be pretty strange/bad. – Tom Jun 29 '16 at 00:31
  • 2
    Please show a [mcve] with an [edit]. We would like to see how you set the delimiter – OneCricketeer Jun 29 '16 at 00:38
  • Why do you use `new Scanner(scan.next())`, when you already know, that there is a `nextLine()` method? – Tom Jun 29 '16 at 17:52
  • Because I'm reading a piece of each line, I don't need the whole line. Not sure how I would use nextLine() to parse each element within that line. I did figure out a solution, might not be ideal but I put an if statement when reading "device 1" that checks if a whitespace was reached, if it was then I use string.concat to append the the rest of the line after the white space and just used string.split to get the words upto the first | character. It works but like I said might not be the ideal way. – chris23balln Jun 29 '16 at 18:21
  • Yes you need the whole line and _then_ you start parsing its content by using `=` and `|` as delimiter. Have you ever tried to use `new Scanner(scan.nextLine()).useDelimiter("=");` to see how it works? – Tom Jun 29 '16 at 20:13

1 Answers1

2

Rather than using scanner.next(), use scanner.nextLine() which returns the whole line, you can then split up the line with string.split("\\|") or similar to get a String array of the elements in the line, split with the "|" delimiter.

iciaran
  • 41
  • 5