Java program help
Im writing this program for my programming class, when you use the setRefNumber method, it is supposed to set it to what you type if ref is greater than 3. But the problem is when you type in something greater than 3, it just sets it back to the default. any help? (i didnt include all of the source code)
[PHP]
public class Book
{
private String author;
private String title;
private int pages;
private String refNumber;
private int borrowed;
public Book(String bookAuthor, String bookTitle, int bookPages)
{
author = bookAuthor;
title = bookTitle;
pages = bookPages;
refNumber = " ";
}
public int getBorrowed() {
return borrowed;
}
public void setBorrowed(int booksBorrowed) {
borrowed = booksBorrowed;
}
public String getAuthor() {
return author;
}
public String getBookTitle() {
return title;
}
public int getBookPages() {
return pages;
}
public void setRefNumber(String ref) {
if(ref.length() > 3) {
ref = refNumber;
}
else {
refNumber = " ";
System.out.println("Error, Reference Number must be atleast 3 digits");
}
}[/PHP]
[PHP]
public class Book
{
private String author;
private String title;
private int pages;
private String refNumber;
private int borrowed;
public Book(String bookAuthor, String bookTitle, int bookPages)
{
author = bookAuthor;
title = bookTitle;
pages = bookPages;
refNumber = " ";
}
public int getBorrowed() {
return borrowed;
}
public void setBorrowed(int booksBorrowed) {
borrowed = booksBorrowed;
}
public String getAuthor() {
return author;
}
public String getBookTitle() {
return title;
}
public int getBookPages() {
return pages;
}
public void setRefNumber(String ref) {
if(ref.length() > 3) {
ref = refNumber;
}
else {
refNumber = " ";
System.out.println("Error, Reference Number must be atleast 3 digits");
}
}[/PHP]
0
Comments
should be
this.refNumber=ref;
whenever you refer to a field of the class, you should use the identifier "this.". It makes it easier to see exactly what is happening, and catch errors like that.
the professor has never mentioned that before
these sections of code wont display the else statements when they should
[PHP] public void setName(String fn, String ln) {
if (fn.length() > 0) {
this.firstName = fn;
}
else {
System.out.println("Please enter a First Name");
}
if (ln.length() > 0) {
this.lastName = ln;
}
else {
System.out.println("Please enter a Last Name");
}
}
public void setCountry(String coun) {
if (coun.length() >= 2) {
this.country = coun;
}
else {
System.out.println("Please enter a country name with more than two characters");
}
}
public void setNumberOfBooks(int num) {
if (num > 0) {
this.numOfBooks = num;
}
else if (num <= 0) {
num = 1;
System.out.println("Please enter a number which is > 1");
}
}[/PHP]
bump
Perhaps there's a trailing character?
Try printing the length of the parameter right after you call the method. If it's not what you expect it to be, try printing the parameter followed by a "|".
System.out.println(ln + "|");
for example. That should show you if there's a trailing " " or newline (or something else).