![]() |
Use Java scanner delimiters separate artist year title etc? (Java)?
Okay, so i'm reading in a file with several lines formatted like this:
"Shania Twain: Greatest Hits" 2004 14.99 "Shania Twain" how wouid my delimiter be set up so that it reads everything inside the first quotation marks, then the year as an int, then the amount (ex 14.99) and then everything in between the last quotations. As in scanLine.next() would be == Shania Twain: Greatest Hits scanLine.nextInt() == 2004 scanLine.nextDouble() == 14.99 and scanLine.next() == Shania Twain in that order. Thanks for your input. My code atm: processLine = scanner.nextLine(); Scanner scanLine = new Scanner(processLine); scanLine.useDelimiter("\\s+"); and that reads from the first quote to the first space... thanks for you help. I was thinking about it, if I went through the string and removed the Quote:
How do I go about removing the Quote:
|
Just a quick query MJ but is this going to be part of a larger Java program?
As there are probably better suited command line tools for this job on a regular text file. |
Its not too much larger, its just a program I'm working on in my voyage to learn Java. This is the method that the problem wants me to use.
|
I managed to strip the String of all quotation marks, what exactly would my deliminator be to remove 2 or more spaces?
|
Quote:
My approach is to read one line at a time, remove quotations and scan using deliminator of two or more spaces (considering that titles and authors names need to be able to contain spaces). Please point out any and all problems that you can see with my code. Thanks, MJ |
have you checked this method ? I have not played with java a lot but I think you can extract string between double using this.
Code:
String findInLine(String pattern) |
Are bookyear and book price seperated by double whitespace ?
The call to scanLine.next() second time will give you bookyear and bookprice both if they are not seperated by double white space. |
Personally I would get rid of the following (unless it is something you are practicing):
Code:
processLine = processLine.replaceAll("\"", "");year and dollars and cents do yet another split on a space on the element in the array related to them. Code:
String [] temp = null; |
I found
Quote:
Thanks for your help! - |
No probs MJ, glad we got there, please mark as SOLVED if you have your solution.
Also, I just thought I would mention, that if you use the CODE tags instead of the QUOTE tags then your formatting will be maintained ;) |
| All times are GMT -5. The time now is 03:34 AM. |