LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-13-2010, 01:28 AM   #1
mitchell7man
Member
 
Registered: Jan 2007
Location: Provo, UT
Distribution: OSX - Fedora
Posts: 458

Rep: Reputation: 31
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?

Last edited by mitchell7man; 04-13-2010 at 01:54 AM.
 
Old 04-13-2010, 02:03 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
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.
 
Old 04-13-2010, 02:09 AM   #3
mitchell7man
Member
 
Registered: Jan 2007
Location: Provo, UT
Distribution: OSX - Fedora
Posts: 458

Original Poster
Rep: Reputation: 31
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.
 
Old 04-13-2010, 02:11 AM   #4
mitchell7man
Member
 
Registered: Jan 2007
Location: Provo, UT
Distribution: OSX - Fedora
Posts: 458

Original Poster
Rep: Reputation: 31
I managed to strip the String of all quotation marks, what exactly would my deliminator be to remove 2 or more spaces?

Last edited by mitchell7man; 04-13-2010 at 02:27 AM.
 
Old 04-13-2010, 02:35 AM   #5
mitchell7man
Member
 
Registered: Jan 2007
Location: Provo, UT
Distribution: OSX - Fedora
Posts: 458

Original Poster
Rep: Reputation: 31
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
 
Old 04-13-2010, 02:41 AM   #6
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
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)
 
Old 04-13-2010, 02:46 AM   #7
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
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.
 
Old 04-13-2010, 05:39 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
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
 
Old 04-13-2010, 04:09 PM   #9
mitchell7man
Member
 
Registered: Jan 2007
Location: Provo, UT
Distribution: OSX - Fedora
Posts: 458

Original Poster
Rep: Reputation: 31
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!
-
 
Old 04-13-2010, 07:24 PM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
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
 
  


Reply

Tags
java, scanner


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Streamtuner: Show song Artist/Title MargNat Linux - Software 4 12-09-2006 10:32 AM
Amarok sorting song list - Artist THEN Title? ironfistchamp Linux - Software 0 06-02-2006 05:56 PM
File>> Artist>> Title>> Time>> Size amjad General 1 02-02-2005 03:30 PM
how do I modify .mp3 information (artist/title)? servnov Linux - General 5 11-12-2004 03:21 AM
fluxbox java awt no title bar Kman Linux - Software 0 06-18-2003 10:47 AM


All times are GMT -5. The time now is 06:45 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration