A little java question

TubaRiver

Masterful
Donor
Joined
Aug 6, 2011
Messages
278
Reaction score
317
Hello all, its that time again! Where i ask a programming question on the forum and see if anyone is able to help. Mainly due to google failing me. pahahaha xD

So here's the question. I have a .txt file with alot of junk within it. If you want an example its basically like this.
fhdidjnfjdnfjdngjufdnjgn product: apple, price: $5.00, tax: $0.25 ndjbnjdsjusndfjsfsfds
dfjdnhfdjnfdnfdjnbndfjkdnfdnfdjnfd product: cereal, price: $3.50. tax: $0.13 hjnubdfjdfdfdfds..... etc

yeah..... basically, i want to get the values next to "product", "price" and "tax" without including literally the words product, price, tax. In my output. I can't actually go line by line taking out a specific string, because the pattern could be just about anywhere within the file. I am unsure what syntax or methods to use, i get the logic, and i can easily output the file itself, but i am just not sure how to manipulate what i have.

I have tried stringtokenizer, stringsplit, if statements, and for loop with delimiter, but nothing seems to be working.

If you want little bit of my code of what i have, of what actually works, it would be this.
Code:
 import java.io.*;
 
public static void main(String[] args) {
 
//open the txt file
bufferedreader in = new bufferedreader(new filereader(store.txt);
 
string line;
string product;
 
// read the txt file
while((line = in.readLine()) != null) {
 
}
 
System.out.print(line);
 
}
Any ideas?
 

san00b

No.1 Admin & Founder
Joined
Aug 2, 2011
Messages
154
Reaction score
560
Quickly playing around in Bash (I don't know Java):

Code:
fhdidjnfjdnfjdngjufdnjgn product: apple, price: $5.00, tax: $0.25 ndjbnjdsjusndfjsfsfds
dfjdnhfdjnfdnfdjnbndfjkdnfdnfdjnfd product: cereal, price: $3.50, tax: $0.13 hjnubdfjdfdfdfds
Bash:
#!/bin/bash
 
IF='store.txt'
 
grep -o -E '(product|price|tax): .*' ${IF}
Outputs:
Code:
product: apple, price: $5.00, tax: $0.25 ndjbnjdsjusndfjsfsfds
product: cereal, price $3.50, tax: $0.13 hjnubdfjdfdfdfds
It's not exactly what you wanted, but here are the two things you can do:

  • Refine the Regex statement, do not capture the product|price|tax piece, would which force the output of only the numbers.
  • Strip out the garbage at the end.
I might have more time to play with this tomorrow. Just threw it together in two minutes.
 

TubaRiver

Masterful
Donor
Joined
Aug 6, 2011
Messages
278
Reaction score
317
Bash is fun! To bad this is mandatory to be done in java. bleh. lol

I've been going back through some Java books i have, and as per one of your suggestions refining the Regex seems the be the only possibility.

That or I'd need to use a completely different IO file class. I figured BufferedReader should have been able to get me what i needed, but i may have to do some testing with the scanner class.

Does this logic make sense? (what I'm thinking if i use the scanner class)

Scan file,
Find a target string (in this case the target strings would be product|price|tax)
Deliminate targets,
getNextInteger/getNextString

These methods can only be used with scanner, not bufferedreader and
To me, that logic would delete the strings and provide the immediate next Integer or String.
Which should be the values immediately after the 3 targets.
 

TubaRiver

Masterful
Donor
Joined
Aug 6, 2011
Messages
278
Reaction score
317
.....i forgot about that wildcard regex.

Still my problem with that is, I should be able to grab the price and tax easily with that. However, since the products widely vary i don't believe its so simple with the wildcard matching.

So id have to scan the entire file for the getNextString.
 

TubaRiver

Masterful
Donor
Joined
Aug 6, 2011
Messages
278
Reaction score
317
That just reads a file and appends the data not what i wanted.


(?::\s)(.*?)(?:,|\s)
Will do what you wanted. Just make sure to only grab the capture groups, and not the entire match. Otherwise you'll end up with more than needed.
Yellow is the entire match, green is the capture group.

The regex works perfect! My problem was, the BufferedReader (class i used to read the input file data) has a limit for things like this, so i quickly changed to a different input file class. Scanner, and works as it should. Gracias.
 
Top