The Artima Developer Community
Sponsored Link

Java Answers Forum
Vector trouble

5 replies on 1 page. Most recent reply: Nov 6, 2006 5:43 PM by Shemane Russell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Shemane Russell

Posts: 4
Nickname: jarell
Registered: Oct, 2006

Vector trouble Posted: Oct 30, 2006 9:57 PM
Reply to this message Reply
Advertisement
I have an assignment due... well last Monday but I've been having a problem with Vectors, the jist of my trouble is I had to code a vector for my assignment which didnt work so I tried running the teacher's example in one of our lectures (as showen below) I keep getting an <identifier> expected error and was wondering if anyone here could help me de-bug (as I'm awful at de-bugging... and Java for that matter)

import java.util.*;

public class Library
{
private Vector<Book> mBooks = new Vector<Book>();

public Library()
{
}

public void addBook(Book newBook)
{
mBooks.add(newBook);
}

public void printAllBalances()
{
System.out.println("The library contains the following books:");
for (Book nextBook : mBooks)
{
nextBook.display();
}
}

public void getTotalBorrowings()
{
int total = 0;
for (Book nextBook : mBooks)
{
total += nextBook.getBorrowedCount();
}
System.out.println("There have been a total of " + total + " borrowings from the library");
}
}


Book (just incase it was needed)
public class Book
{
private String mAuthor;
private String mTitle;
private boolean mBorrowed;
private int mBorrowedCount;

public Book(String author, String title)
{
mAuthor = author;
mTitle = title;
mBorrowed = false;
mBorrowedCount = 0;
}

public boolean borrowBook()
{
if (mBorrowed)
{
System.out.println("Sorry, " + mTitle + " by " + mAuthor + " is already borrowed");
return false;
}
else
{
mBorrowed = true;
mBorrowedCount++;
return true;
}
}

public void returnBook()
{
if (mBorrowed)
{
mBorrowed = false;
}
else
{
System.out.println("Error - this book was not borrowed so can't be returned");
}
}
public void display()
{
System.out.print(mTitle + " by " + mAuthor + " has been borrowed " + mBorrowedCount + " times and is currently ");
if (mBorrowed)
System.out.println("out");
else
System.out.println("in");
}
public int getBorrowedCount()
{
return mBorrowedCount;
}
}


Any help would be most appreciated and please dont flame me I already know I'm crap at Java :P

<3 Jarell


Shemane Russell

Posts: 4
Nickname: jarell
Registered: Oct, 2006

Re: Vector trouble Posted: Oct 30, 2006 10:12 PM
Reply to this message Reply
my appologies I forgot to include the fact that I'm using Blue J version 2.1.3 (java version 1.4.2_11)

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Vector trouble Posted: Oct 31, 2006 2:03 AM
Reply to this message Reply
You also forgot to format (see "formatting your post" section) your code and to specify where the error occured.

To your problem:

you copied your professor's description into the code.

In case you didn't notice, there are 2 classes:
Library and Book.

Both compile perfectly.

You need to put the class Library in a file named "Library.java" and the class Book in a file named "Book.java"

Don't forget to remove the line "Book (just incase it was needed)"
Or put at least "//" before it, so it will become a comment.

Shemane Russell

Posts: 4
Nickname: jarell
Registered: Oct, 2006

Re: Vector trouble Posted: Oct 31, 2006 1:16 PM
Reply to this message Reply
I know there are 2 sections of code here I added the book class in case you needed to see it :( So nothing wrong with the code meens something wrong with my java program >.<
Thanks for your help :)

Ravi Venkataraman

Posts: 80
Nickname: raviv
Registered: Sep, 2004

Re: Vector trouble Posted: Nov 1, 2006 10:27 AM
Reply to this message Reply
You are using a compiler for Java 1.4.x. The code uses Java Geneerics and requires a Java 5 compiler.

Shemane Russell

Posts: 4
Nickname: jarell
Registered: Oct, 2006

Re: Vector trouble Posted: Nov 6, 2006 5:43 PM
Reply to this message Reply
yeah I realised that a couple days ago *hides behind her couch*
Thanks heaps for helping this noob :)

Flat View: This topic has 5 replies on 1 page
Topic: Vector trouble Previous Topic   Next Topic Topic: COM interface to java application

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use