LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Use Java scanner delimiters separate artist year title etc? (Java)? (http://www.linuxquestions.org/questions/programming-9/use-java-scanner-delimiters-separate-artist-year-title-etc-java-801682/)

mitchell7man 04-13-2010 01:28 AM

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:

"
marks from the string, all I would have to do is deliminate where there are two or more spaces which is scanLine.useDelimiter("\\s\\s"); or something like that right?

How do I go about removing the
Quote:

"
from the string that I am reading in at the time?

grail 04-13-2010 02:03 AM

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.

mitchell7man 04-13-2010 02:09 AM

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.

mitchell7man 04-13-2010 02:11 AM

I managed to strip the String of all quotation marks, what exactly would my deliminator be to remove 2 or more spaces?

mitchell7man 04-13-2010 02:35 AM

Quote:

public void addBookFile(String fileName) throws FileNotFoundException
{
File cdFile = new File(fileName);
Scanner scanner = new Scanner(cdFile);
while(scanner.hasNextLine())
{
processLine = scanner.nextLine();
processLine = processLine.replaceAll("\"", "");
Scanner scanLine = new Scanner(processLine);
scanLine.useDelimiter("\\s\\s");
if (scanLine.hasNext())
{
bookTitle = scanLine.next();
System.out.println(bookTitle);
bookYear = Integer.parseInt(scanLine.next());
System.out.println(bookYear);
bookPrice = Double.parseDouble(scanLine.next());
System.out.println(bookPrice);
bookAuthor = scanLine.next();
System.out.println(bookAuthor);
}
Book newBook = new Book(bookTitle, bookYear, bookPrice, bookAuthor);
bookArray[bookIndex] = newBook;
bookIndex = bookIndex + 1;
}
scanner.close();
}
Okay, i'm providing more code b/c i think this may be where I'm having larger issues. I'm reading in a .txt file with many lines formatted as indicated above ("Shania Twain: Greatest Hits" 2004 14.99 "Shania Twain"). I'm trying to separate them and save them to their strings, make an object and add that object to an array. I want to go through the whole file add all of these books to the book array.

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

PMP 04-13-2010 02:41 AM

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)
String        findInLine(Pattern pattern)


PMP 04-13-2010 02:46 AM

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.

grail 04-13-2010 05:39 AM

Personally I would get rid of the following (unless it is something you are practicing):
Code:

processLine = processLine.replaceAll("\"", "");
Scanner scanLine = new Scanner(processLine);
scanLine.useDelimiter("\\s\\s");
if (scanLine.hasNext())
{
    bookTitle = scanLine.next();
    System.out.println(bookTitle);
    bookYear = Integer.parseInt(scanLine.next());
    System.out.println(bookYear);
    bookPrice = Double.parseDouble(scanLine.next());
    System.out.println(bookPrice);
    bookAuthor = scanLine.next();
    System.out.println(bookAuthor);
}

And instead just return once from processLine.split("\"") (which returns a string array) and then for your
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;
String [] temp2 = null;

processLine = scanner.nextLine();
temp = processLine.split("\"");

bookTitle = temp[1]; //I think (not tested) temp[0] would be the start of the line hence ""
bookAuthor = temp[3];
temp2 = temp[2].split("\\s");
bookYear = temp2[0];
bookPrice = Double.parseDouble(temp2[1]);

This is untested but should be close ;)

mitchell7man 04-13-2010 04:09 PM

I found
Quote:

public void addBookFile(String fileName) throws FileNotFoundException
{

File bookFile = new File(fileName);
Scanner scanner = new Scanner(bookFile);
while(scanner.hasNextLine())
{
Book[] tempBook = new Book[bookIndex + 1];
int bookCounter = 0;
while(bookCounter < bookIndex)
{
tempBook[bookCounter] = bookArray[bookCounter];
bookCounter++;
}

bookCD[1] = bookIndex + 1;

String [] temp = null;

processLine = scanner.nextLine();
temp = processLine.split("\"");
//
System.out.println(temp[1]);
bookTitle = temp[1];
//
System.out.println(temp[3]);
bookAuthor = temp[3];
//
Scanner scanLine = new Scanner(temp[2]);
scanLine.useDelimiter("\\s+");
bookYear = Integer.parseInt(scanLine.next());
bookPrice = Double.parseDouble(scanLine.next());
System.out.println(bookYear);
System.out.println(bookPrice);
//
Book newBook = new Book(bookTitle, bookYear, bookPrice, bookAuthor);
tempBook[bookIndex] = newBook;
bookArray = tempBook;
//Print book.
System.out.println(bookArray[bookIndex].toString());
bookIndex = bookIndex + 1;
}
}
To work perfectly. For some reason, the String with the two numbers in it was not splitting, so I used scanner.


Thanks for your help!
-

grail 04-13-2010 07:24 PM

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 10:46 AM.