-
Strings Problem
Hello guys, I'm in APCS and right now we're working on a Sun Microsistems tutorial exercise that involves making a library program. It's really simple, you only have to show the books that you have on the shelf, the ones on loan, check in, check out, add and remove books. They provide us a with a Library Class that has every method needed, Ijust have to make a libraryUser class wich only interacts with the user to see what he/she wants to do. The problem is that when I try to input an String(options 3-6), it skeep that part and diplays the menu again. Can someone tell me how can be capable of inputing the Strings please? Thx.
This is the code I have so far:
Library Class
Code:
/**
* Library.java demonstrates use of ArrayLists
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
import java.util.Scanner;
public class Library {
private ArrayList onShelf; // instance variables: arraylists for books checked in and out
private ArrayList onLoan;
public Library() { // Constructor for objects of class Library
onLoan = new ArrayList(); // initialize onLoan ArrayList
onShelf = new ArrayList(); // initialize onShelf ArrayList
onShelf.add("Green Eggs and Ham");
onShelf.add("The Best Beaches in Mexico");
onShelf.add("Java for Dummies");
onShelf.add("Sleepy Hollow");
onShelf.add("Rip van Winkle");
onShelf.add("The Devil and Tom Walker");
onShelf.add("The fall of the house of Usher");
/* TASK #1 - ADD MORE BOOKS TO THE SHELF */
}
public boolean isCheckedOut(String title) { // isCheckedOut() returns true if book is onLoan, false otherwise
if (onLoan.contains(title)) { // check the arraylist for title
return true;
}
else return false; // if not, test is false
} // end of isCheckedOut()
public void checkOut(String title) { //Move book to onLoan list
if (isCheckedOut(title)) { // see if already checked out
System.out.println("We're sorry, that book is not available.\n");
}
else { // not checked out
int index = onShelf.indexOf(title); // get its index
String temp = (String)onShelf.get(index); // save the name
onShelf.remove(index); // remove it from the onShelf list
onLoan.add(temp); // add it to the onLoan list
}
} // end of checkout()
public void checkIn(String title) { // Move book to onShelf list
if (isCheckedOut(title)) { // see if already checked out
int index = onLoan.indexOf(title); // get its index
String temp = (String)onLoan.get(index); // save the name
onLoan.remove(index); // remove it from the onShelf list
onShelf.add(temp); // add it to the onLoan list
}
}
public void addBook(String title) { //Add new Book to onShelf list
onShelf.add(title); // add it to the onLoan list
} // end of addBook()
public void removeBook(String title){
int index = onShelf.indexOf(title);
onShelf.remove(index);
}
/**
*
*/
public void showBooksIn() { //Lists all books checked in
String temp;
System.out.println("CAT Library\nBooks checked in\n----------------\n");
Iterator iter = onShelf.iterator(); // create an iterator object
while (iter.hasNext()) { // and keep going so long as there are more elements in the list
temp = (String)iter.next(); // get & convert current element
System.out.println(temp); // print it!
}
} // end of showBooksIn()
public void showBooksOut() { //Lists all books checked out
String temp;
System.out.println("CAT Library\nBooks checked out\n----------------\n");
Iterator iter = onLoan.iterator(); // create an iterator object
while (iter.hasNext()) { // and keep going so long as there are more elements in the list
temp = (String)iter.next(); // get & convert current element
System.out.println(temp); // print it!
} // end while loop
} // end method ShowBooksOut
} // end of Library class
LibraryUser Class
Code:
/**
* Library.java demonstrates use of ArrayLists
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
import java.util.Scanner;
class LibraryUser {
public static void main(String[] args) { // START MAIN METHOD
Scanner input=new Scanner(System.in);
Library biblio=new Library();
int option;
do{
System.out.println("\n\t\tCAT Library\n");
System.out.println("Choose one of the following options: ");
System.out.println("\t1.-Show books in");
System.out.println("\t2.-Show books out");
System.out.println("\t3.-Check Out");
System.out.println("\t4.-Check in");
System.out.println("\t5.-Add book");
System.out.println("\t6.-Remove book");
System.out.println("\t7.-Exit\n");
System.out.print("Option: ");
option=input.nextInt();
switch (option){
case 1:
biblio.showBooksIn();
break;
case 2:
biblio.showBooksOut();
break;
case 3:
System.out.print("Enter name of book: ");
String CheckO=input.nextLine();
biblio.checkOut(CheckO);
break;
case 4:
System.out.print("Enter name of book: ");
String CheckI=input.nextLine();
biblio.checkIn(CheckI);
break;
case 5:
System.out.println("Enter name of book: ");
String newB=input.nextLine();
biblio.addBook(newB);
break;
case 6:
System.out.println("Enter name of book: ");
String removeB=input.nextLine();
biblio.removeBook(removeB);
break;
}
}while(option!=7);
}
}
-
I wonder if your problem is your Scanner object not dealing directly with the end of line (EOL) token. What if where you have a call to input.nextInt(); you follow this with a call to input.nextLine();? e.g., change this:
Code:
option=input.nextInt();
switch (option) {
//....
to this:
Code:
option=input.nextInt();
input.nextLine(); // to allow the Scanner to "swallow" the EOL token
switch (option) {
//....
-
Hey thx it worked! I still have 1 problem, when I try the check out method, and input a book that is in the library(OnShelf arraylist), I get an error message. My teacher told me that it was because I inputed an string and then compare it with the ones of the Arraylist, but the arraylist doesn't have strings, all of the contents are objects. So, how can I compare the string the the object?
-
Show the exact line of code and the full text of the error message.
-
OK, so run the LibraryUser class, select 3 as the option, and then input "Green Eggs and Ham". After you put the input.nextInt(); on every string inputed. You should get the following error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.get(Unknown Source)
at Library.checkOut(Library.java:49)
at LibraryUser.main(LibraryUser.java:40)
-
Ok so I make some research, and the "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1" error means that the checkOut method doesn't find the Strings using the contains method. Will it be better if I use an iterator?
-
Yes, indexOf() returns -1 if the object is not in the arraylist.
Before the call to get(), just check if the value of index is -1. You can then provide an error, or whatever you want to do with it.
-
I already solved the error messages, now, the error is that when I try to check a book. I enter a String with the name of the book I want to check out, sent it to the Library class to check if it is on the Library, I use the contains methods to see if the ArrayList has the name of the book, but It always tell me that It doesn't have the book, even if I am sure it does. I even tried a simpler code that does the same thing and it worked, I don't understand why this one don't. Oh, and what my teacher told me that I can't compare the Strings with the elements inside the ArrayList is wrong, it's possible. Can someone tell me how can I make the CheckOut work?
This is the code progress I made, have new mathod called IsInLibrary to check if teh book is in the onShelf ArrayList:
Library Class
Code:
/**
* Library.java demonstrates use of ArrayLists
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
import java.util.Scanner;
public class Library {
private ArrayList onShelf; // instance variables: arraylists for books checked in and out
private ArrayList onLoan;
public Library() { // Constructor for objects of class Library
onLoan = new ArrayList(); // initialize onLoan ArrayList
onShelf = new ArrayList(); // initialize onShelf ArrayList
onShelf.add("Green Eggs and Ham");
onShelf.add("The Best Beaches in Mexico");
onShelf.add("Rip van Winkle");
onShelf.add("The Devil and Tom Walker");
onShelf.add("The fall of the house of Usher");
onShelf.add("a");
onLoan.add("Java for Dummies");
onLoan.add("Sleepy Hollow");
/* TASK #1 - ADD MORE BOOKS TO THE SHELF */
}
public boolean isCheckedOut(String title) { // isCheckedOut() returns true if book is onLoan, false otherwise
if (onLoan.contains(title)) { // check the arraylist for title
return true;
}
else return false; // if not, test is false
} // end of isCheckedOut()
public boolean isInLibrary(String title) {
if (onShelf.contains(title)) {
return true;
}
else return false;
}
public void checkOut(String title) { //Move book to onLoan list
if (onShelf.contains(title)) { // not checked out
int index = onShelf.indexOf(title); // get its index
String temp = (String)onShelf.get(index); // save the name
onShelf.remove(index); // remove it from the onShelf list
onLoan.add(temp); // add it to the onLoan list
System.out.println("Check out Succesful!\n");
}
else {
System.out.println("Sorry, this book is no in the library");
}
} // end of checkout()
public void checkIn(String title) { // Move book to onShelf list
if (isCheckedOut(title)) { // see if already checked out
int index = onLoan.indexOf(title); // get its index
String temp = (String)onLoan.get(index); // save the name
onLoan.remove(index); // remove it from the onShelf list
onShelf.add(temp); // add it to the onLoan list
}
else {
System.out.println("This book is not in loan\n");
}
}
public void addBook(String title) { //Add new Book to onShelf list
onShelf.add(title); // add it to the onLoan list
} // end of addBook()
public void removeBook(String title){
int index = onShelf.indexOf(title);
onShelf.remove(index);
}
/**
*
*/
public void showBooksIn() { //Lists all books checked in
String temp;
System.out.println("CAT Library\nBooks checked in\n----------------\n");
Iterator iter = onShelf.iterator(); // create an iterator object
while (iter.hasNext()) { // and keep going so long as there are more elements in the list
temp = (String)iter.next(); // get & convert current element
System.out.println(temp); // print it!
}
} // end of showBooksIn()
public void showBooksOut() { //Lists all books checked out
String temp;
System.out.println("CAT Library\nBooks checked out\n----------------\n");
Iterator iter = onLoan.iterator(); // create an iterator object
while (iter.hasNext()) { // and keep going so long as there are more elements in the list
temp = (String)iter.next(); // get & convert current element
System.out.println(temp); // print it!
} // end while loop
} // end method ShowBooksOut
} // end of Library class
LibraryUser Class
Code:
/**
* Library.java demonstrates use of ArrayLists
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
import java.util.Scanner;
class LibraryUser {
public static void main(String[] args) { // START MAIN METHOD
Scanner input=new Scanner(System.in);
Library biblio=new Library();
int option;
do{
System.out.println("\n\t\tCAT Library\n");
System.out.println("Choose one of the following options: ");
System.out.println("\t1.-Show books in");
System.out.println("\t2.-Show books out");
System.out.println("\t3.-Check Out");
System.out.println("\t4.-Check in");
System.out.println("\t5.-Add book");
System.out.println("\t6.-Remove book");
System.out.println("\t7.-Exit\n");
System.out.print("Option: ");
option=input.nextInt();
switch (option){
case 1:
biblio.showBooksIn();
break;
case 2:
biblio.showBooksOut();
break;
case 3:
System.out.print("Enter name of book: ");
String CheckO=input.nextLine();
input.nextLine();
biblio.checkOut(CheckO);
break;
case 4:
System.out.print("Enter name of book: ");
String CheckI=input.nextLine();
input.nextLine();
biblio.checkIn(CheckI);
break;
case 5:
System.out.println("Enter name of book: ");
String newB=input.nextLine();
input.nextLine();
biblio.addBook(newB);
break;
case 6:
System.out.println("Enter name of book: ");
String removeB=input.nextLine();
input.nextLine();
biblio.removeBook(removeB);
break;
case 7:
System.out.println("Good bye!\n");
}
}while(option!=7);
}
}
-
You're going to need to stick some debug println()s in there so you can see what's going on, specifically in the checkOut code.
Print the value of title, with some symbols at the front and end so you can see if there's a spurious space or not, eg:
"##" + title + "##"
-
Ok I alreay fix it, thx guys.
-
Would have been nice to know what the problem was...:)
-
Sorry man, what I did was to remove all the input.nextLine() after I inputed and string and stored that value in a varaible. I put that after I asked for the option. This is the class the final class that worked:
Code:
/**
* Library.java demonstrates use of ArrayLists
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
import java.util.Scanner;
class LibraryUser {
public static void main(String[] args) { // START MAIN METHOD
Scanner input=new Scanner(System.in);
Library biblio=new Library();
int option;
int choose;
do{
System.out.println("\n\t\tCAT Library\n");
System.out.println("Currently, there are "+biblio.booksOnShelf()+" on the library and "+biblio.booksOnLoan()+" books on loan\n");
System.out.println("Choose one of the following options: ");
System.out.println("\t1.-Show books in");
System.out.println("\t2.-Show books out");
System.out.println("\t3.-Check Out");
System.out.println("\t4.-Check in");
System.out.println("\t5.-Add book");
System.out.println("\t6.-Remove book");
System.out.println("\t7.-Turn in all books");
System.out.print("Option: ");
option=input.nextInt();
input.nextLine();
switch (option){
case 1:
biblio.showBooksIn();
break;
case 2:
biblio.showBooksOut();
break;
case 3:
System.out.print("Enter name of book: ");
String CheckO=input.nextLine();
biblio.checkOut(CheckO);
break;
case 4:
System.out.print("Enter name of book: ");
String CheckI=input.nextLine();
biblio.checkIn(CheckI);
break;
case 5:
System.out.print("Enter name of book: ");
String newB=input.nextLine();
biblio.addBook(newB);
break;
case 6:
System.out.print("Enter name of book: ");
String removeB=input.nextLine();
biblio.removeBook(removeB);
break;
case 7:
biblio.checkInAll();
break;
}
System.out.print("Do you want to go back to the main menu?(1=yes nad 2=no): ");
choose=input.nextInt();
}while(choose==1);
}
}