 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tim Guest
|
Posted: Mon Apr 05, 2004 2:55 pm Post subject: ParseException Error. Please Help!! |
|
|
Hello all!
Very new to Java need to complete this assignment for a school
project. The code below creates a few books (Title, Author or Authors,
Publisher, Number of pages, Publication Date etc.)
Methods have been developed to display a book's information and to
compare two books by title, number of pages and, HERE IS THE PROBLEM,
publication date.
Date has to be in the "dd-MM-yyyy" format.
The code works alright if we leave out the compPubDate method (line
55) and the calling of that method at line 95.
The message I get from the compiler is this:
Library.java:95: unreported exception java.text.ParseException; must
be caught or declared to be thrown
book2.compPubDate(book4);
There must be something wrong with my method that I don't understand.
Could anyone please help me fix this for me? Any help would be greatly
appreciated!
import java.util.*;
import java.text.*;
class Book {
String title;
String author1;
String author2;
String author3;
String pubdate;
String isbn;
int numpages;
String publisher;
Book(String title, String author1, String author2, String author3,
String pubdate, String isbn, int numpages, String publisher) {
this.title = title;
this.author1 = author1;
this.author2 = author2;
this.author3 = author3;
this.pubdate = pubdate;
this.isbn = isbn;
this.numpages = numpages;
this.publisher = publisher;
}
void bookData (Book book1) {
System.out.println("Title: " + book1.title + 'n' + "Author1: " +
book1.author1 + 'n' + "Author2: " + book1.author2 + 'n' + "Author3:
" + book1.author3);
System.out.println("Publication Date: " + book1.pubdate + 'n' +
"ISBN: " + book1.isbn + 'n' + "Number of Pages: " + book1.numpages);
System.out.println("Publisher: " + book1.publisher + 'n');
}
void compPages(Book otherBook) {
if (this.numpages > otherBook.numpages)
System.out.println("nThe book " + this.title + ", has more pages
than book " + otherBook.title + " hasn");
else if (this.numpages < otherBook.numpages)
System.out.println("nThe book " + otherBook.title + ", has more
pages than book " + this.title + " hasn");
else if (this.numpages == otherBook.numpages)
System.out.println("nThe book " + this.title + ", has the same
pages as book " + otherBook.title + " hasn");
}
void compPublisher(Book otherBook) {
if (this.publisher.equals(otherBook.publisher))
System.out.println("The two books have the same Publisher: " +
this.publisher + 'n');
else
System.out.println("The two books DO NOT have the same Publisher" +
'n');
}
void compPubDate(Book otherBook) throws ParseException {
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date d1 = df.parse(this.pubdate);
Date d2 = df.parse(otherBook.pubdate);
String relation;
if (d1.equals(d2)) {
relation = " have the same publication date";
System.out.println("Book: " + this.title + " and " +
otherBook.title + "," + relation);
}
else if (d1.before(d2)) {
relation = " was published before Book: ";
System.out.println("Book: " + this.title + relation +
otherBook.title);
}
else {
relation = " was published after Book: ";
System.out.println("Book: " + this.title + relation +
otherBook.title);
}
}
}
public class Library {
public static void main(String[] arguments) {
Book book1 = new Book("Before The Dawn", "Tim Birtas", "MFA",
"GSGA", "23-3-2004", "030-536-278-9", 105, "Epic ltd");
Book book2 = new Book("A Brief History of Time", "Stephen W.
Hawking", "", "", "01-3-1988", "055-305-243-8", 198, "Bantam Books");
Book book3 = new Book("Teach Yourself C", "Herbert Schildt", "", "",
"01-4-2000", "960-512-228-6", 635, "McGraw-Hill");
Book book4 = new Book("Computer Networks", "Andrew S. Tanenbaum",
"", "GSGA", "23-3-2004", "0-13-394248-1", 813, "Prentice Hall");
Book book5 = new Book("Programming Languages II", "Cleanthis
Trabulides", "", "", "01-01-2001", "960-538-174-5", 252, "EAP");
book4.bookData(book4);
book2.compPages(book4);
book2.compPublisher(book4);
book2.compPubDate(book4);
}
}
|
|
| Back to top |
|
 |
Christophe Vanfleteren Guest
|
Posted: Mon Apr 05, 2004 2:57 pm Post subject: Re: ParseException Error. Please Help!! |
|
|
Tim wrote:
| Quote: | Hello all!
Very new to Java need to complete this assignment for a school
project. The code below creates a few books (Title, Author or Authors,
Publisher, Number of pages, Publication Date etc.)
Methods have been developed to display a book's information and to
compare two books by title, number of pages and, HERE IS THE PROBLEM,
publication date.
Date has to be in the "dd-MM-yyyy" format.
The code works alright if we leave out the compPubDate method (line
55) and the calling of that method at line 95.
The message I get from the compiler is this:
Library.java:95: unreported exception java.text.ParseException; must
be caught or declared to be thrown
book2.compPubDate(book4);
There must be something wrong with my method that I don't understand.
Could anyone please help me fix this for me? Any help would be greatly
appreciated!
|
<snip code/>
You'll need to learn about exceptions:
http://java.sun.com/docs/books/tutorial/essential/exceptions/
--
Kind regards,
Christophe Vanfleteren
|
|
| Back to top |
|
 |
Bryce (Work) Guest
|
Posted: Mon Apr 05, 2004 7:39 pm Post subject: Re: ParseException Error. Please Help!! |
|
|
On 5 Apr 2004 07:55:25 -0700, [email]tbirtas (AT) mfa (DOT) gr[/email] (Tim) wrote:
| Quote: | Library.java:95: unreported exception java.text.ParseException; must
be caught or declared to be thrown
book2.compPubDate(book4);
|
The error message gives you all the information you need. You have two
things you need to do.
1) wrap the code using try... catch, catching ParseException
Date d2 = df.parse(otherBook.pubdate);
2) declare your method to throw ParseException (which means you'll
need to repeat these steps with any calling function.
--
now with more cowbell
|
|
| Back to top |
|
 |
Tim Guest
|
Posted: Tue Apr 06, 2004 6:33 am Post subject: Re: ParseException Error. Please Help!! |
|
|
Thank you both for the input! I think I got it now!
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|