Array SignOut Function problems
Author |
Message |
compstudent
|
Posted: Mon Apr 11, 2011 8:41 pm Post subject: Array SignOut Function problems |
|
|
Hi,
I have this book class which has simple functions to sign in and out books.
import java.awt.*;
import hsa.Console;
import java.util.Calendar;
import java.text.*;
import java.util.Date;
public class book
{
static Console c;
private int year, copies, number, copiesOut;
private String author, name;
private String[] patrons;
private Date[] dueDate;
public book (String title, String writer, int numBooks, int bookNum, int bookYear)
{
name = title;
author = writer;
year = bookYear;
copies = numBooks;
number = bookNum;
patrons = new String [copies];
dueDate = new Date [copies];
}
public void setName (String title)
{
name = title;
}
public void setAuthor (String writer)
{
author = writer;
}
public void setCopies (int nums)
{
copies = nums;
}
public void setYear (int date)
{
year = date;
}
public void setNumber (int booknum)
{
booknum = number;
}
public boolean signOut (String name)
{
if (copiesOut == copies)
{
c.println ("Copies Out:" + copiesOut);
return false;
}
else
{
patrons [copiesOut] = name;
Calendar cal1 = Calendar.getInstance ();
cal1.add(Calendar.DAY_OF_MONTH, 14);
Date d = cal1.getTime ();
dueDate [copiesOut] = d;
copiesOut = copiesOut + 1;
return true;
}
}
public boolean signIn (String name)
{
String temp;
for (int i = 0 ; i < copiesOut ; i++)
{
if (patrons [i] == name)
{
if (i == copiesOut)
{
patrons [i] = null;
dueDate[i] = null;
copiesOut = copiesOut - 1;
}
else
{
patrons[i] = patrons[copiesOut-1];
dueDate[i] = dueDate[copiesOut-1];
copiesOut = copiesOut -1;
}
return true;
}
}
return false;
}
public void addCopy ()
{
copies++;
}
public void removerCopy ()
{
copies--;
}
public void output ()
{
DateFormat df = DateFormat.getDateInstance ();
c.println ("Book title: " + name);
c.println ("Book author: " + author);
c.println ("Book year: " + year);
c.println ("Book number: " + number);
c.println ("Book copies: " + copies);
c.println ("Copies available: " + (copies - copiesOut));
if (copiesOut > 0)
{
c.println ("Patrons with book: ");
for (int i = 0 ; i < copiesOut ; i++)
{
c.println (i + 1 + ": " + patrons [i] + " Due Date: " + df.format(dueDate[i]));
}
}
}
public static void main (String[] args)
{
c = new Console ();
String name, author;
int year, number, copies;
c.println ("Enter the book title");
name = c.readString ();
c.println ("Enter the book author");
author = c.readString ();
c.println ("Enter the book publishing date");
year = c.readInt ();
c.println ("Enter the number of copies");
copies = c.readInt ();
c.println ("Enter the book number");
number = c.readInt ();
book b1 = new book (name, author, copies, number, year);
b1.signOut ("Bob");
b1.signOut ("Mike");
b1.signOut ("Daivd");
b1.signOut ("Tom");
b1.signOut ("Jim");
b1.signOut ("Rod");
b1.output ();
b1.signIn ("Jim");
b1.signIn ("David");
b1.signIn ("Bob");
b1.signIn ("Rod");
b1.output ();
}
}
The problem is that all the sign in's work except for David, who doesn't get signed back in.
Any ideas why?
Thanks |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: Mon Apr 11, 2011 8:50 pm Post subject: RE:Array SignOut Function problems |
|
|
yes. Strings are objects. "David" and "David" might be two different instances. Also, you've spelled it "Daivd" once, but that shouldn't matter. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
compstudent
|
Posted: Mon Apr 11, 2011 9:08 pm Post subject: Re: Array SignOut Function problems |
|
|
I think that was my problem. Dumb mistake. Thanks |
|
|
|
|
 |
Tony

|
Posted: Mon Apr 11, 2011 9:14 pm Post subject: RE:Array SignOut Function problems |
|
|
Please tell me that you are actually referring to the misuse of ==, and not about the spelling. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
copthesaint

|
Posted: Tue Apr 12, 2011 8:20 am Post subject: Re: Array SignOut Function problems |
|
|
=) Tony, he'll be back when he realises he should use .equals() |
|
|
|
|
 |
md

|
Posted: Tue Apr 12, 2011 2:50 pm Post subject: RE:Array SignOut Function problems |
|
|
In his example he'd be using the literal "David", which javac will replace with a reference to the same String object at compile time.
So in the simple case of
code: | String c = "David";
System.out.println(c == "David" ? "omg!" : "ponies");
|
== works as expected. Sometimes the wrong thing does indeed work (and could be faster!) |
|
|
|
|
 |
Tony

|
Posted: Tue Apr 12, 2011 3:02 pm Post subject: Re: RE:Array SignOut Function problems |
|
|
md @ Tue Apr 12, 2011 2:50 pm wrote: javac will replace with a reference to the same String object at compile time.
But now the code behaviour relies on the specifics of the compiler's internals. javac makes this work sometimes (heck, it works in most simple cases), but (for example) my (as in the one I wrote, not one I use) Java compiler doesn't do this. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
md

|
Posted: Tue Apr 12, 2011 6:26 pm Post subject: RE:Array SignOut Function problems |
|
|
Certainly - I'm saying that the problem is still there, just that correcting the spelling is likely to solve the issue (incorrectly) and therefor we won't hear more about it. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
|
|