The Student Room Group

Java help please!

Hi guys,

I'm doing an MIT OCW Assignment on Java, here's the link:

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-092-introduction-to-programming-in-java-january-iap-2010/assignments/MIT6_092IAP10_assn04.pdf

For Book.Java, my code is:
==============================================================

public class Book {
String title;
public boolean borrowed;

public Book(String bookTitle){
title = bookTitle;
borrowed = false;
}

public void borrowed() {
borrowed = true;
}

public void returned() {
borrowed = false;
}

public boolean isBorrowed() {
if(borrowed == true) {
return true;
}
else {
return false;
}
}

public String getTitle() {
return title;
}

public static void main(String[] args) {
Book example = new Book("The Da Vinci Code");

System.out.println("Title: " + example.getTitle());

System.out.println("Borrowed? " + example.isBorrowed());

example.borrowed();
System.out.println("Borrowed? " + example.isBorrowed());

example.returned();
System.out.println("Borrowed? " + example.isBorrowed());
}
}

==============================================================

For Library.java my code is:

==============================================================

public class Library {
private String address;

private String[] books;


public Library(String a) {
address = a;
}

private void addBook(Book book) {

}

private static void printOpeningHours() {
System.out.print("9AM to 5PM daily");
}



public static void main(String[] args) {
//Create two libraries

Library firstLibrary = new Library("10 Main St");

Library secondLibrary = new Library("228 Liberty St");

//Add four books to each library
firstLibrary.addBook(new Book("The Da Vinci Code"));
firstLibrary.addBook(new Book("Le Petit Prince"));
firstLibrary.addBook(new Book("A Tale of Two Cities"));
firstLibrary.addBook(new Book("The Lord of the Rings"));

//Print opening hours and addresses
System.out.println("Library hours:");
printOpeningHours();
System.out.println();


//Try to borrow Lord of The Rings from both libraries
System.out.println("Borrowing The Lord of the Rings:");
firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings");
secondLibrary.borrowBook("The Lord of the Rings");


//Return the Lord Of The Rings to the first library

//Print titles of available books from first library
}
}

=============================================================

The bit I'm struggling with is making the method borrowBook(String)

I need to put the argument as the book title and then change that book's "borrowed" boolean variable to true.

I've tried:

public void borrowBook(String bookTitle) {

Book bookTitle.borrowed();
}

i.e. trying to use the borrow() method from Book.java.

This didn't work and now I'm stumped!!

Please help!!!!
Reply 1
Are you allowed to use other objects like arraylists / hashmaps? sorry i did the whole thing, just look at what you need to, PM me if you want it removed. I also dont know how you should be naming variables, so change as required i guess.

Edit: also made some changed to your code as there were simpler ways of doing it.

Book Class.

public class Book {
private String title;
private boolean borrowed;


// Creates a new Book
public Book(String bookTitle) {
this.title = bookTitle;
this.borrowed = false;
}


// Marks the book as rented
public void borrowed() {
this.borrowed = true;
}


// Marks the book as not rented
public void returned() {
this.borrowed = false;
}


// Returns true if the book is rented, false otherwise
public boolean isBorrowed() {
return this.borrowed;
}


// Returns the title of the book
public String getTitle() {
return this.title;
}


/*public static void main(String[] arguments) {
// Small test of the Book class
Book example = new Book("The Da Vinci Code");
System.out.println("Title (should be The Da Vinci Code): "
+ example.getTitle());
System.out.println("Borrowed? (should be false): "
+ example.isBorrowed());
example.borrowed();
System.out.println("Borrowed? (should be true): "
+ example.isBorrowed());
example.returned();
System.out.println("Borrowed? (should be false): "
+ example.isBorrowed());
}*/
}



Library Class

public class Library {
private String libraryAddress;
private Book[] Books = new Book[0];

public Library(String address) {
libraryAddress = address;
}

public void addBook(Book item) {
int Len = Books.length;
Book[] Temp = new Book[Len + 1];
for (int I = 0; I < Len; I++)
Temp = Books;

Temp[Len] = item;
Books = new Book[Temp.length];
for (int I = 0; I < Books.length; I++)
Books = Temp;
}



private void returnBook(String returningBook) {
for (int I = 0; I < Books.length; I++)
if (Books.getTitle().compareTo(returningBook) == 0)
if (Books.isBorrowed())
{
Books.returned();
System.out.println("You successfully returned " + returningBook);
return;
}
System.out.println("Sorry, this book is not from our catalog.");
}


private void printAvailableBooks() {
for (int I = 0; I < Books.length; I++)
if (!Books.isBorrowed())
System.out.println(Books.getTitle());

if (Books.length == 0)
System.out.println("No Books in catalog");
}


private void borrowBook(String requestedBook) {
for (int I = 0; I < Books.length; I++)
if (Books.getTitle().compareTo(requestedBook) == 0)
if (Books.isBorrowed())
{
System.out.println("Sorry, this book is already borrowed.");
return;
} else
{
Books.borrowed();
System.out.println("You successfully borrowed " + requestedBook);
return;
}
System.out.println("Sorry, this book is not in our catalog.");
}


private static void printOpeningHours() {
System.out.println("Libraries are open daily from 9am to 5pm.");
}


private void printAddress() {
System.out.println(this.libraryAddress);
}

/*public static void main(String[] args) {
// Create two libraries
Library firstLibrary = new Library("10 Main St.");
Library secondLibrary = new Library("228 Liberty St.");
firstLibrary.addBook(new Book("The Da Vinci Code"));
firstLibrary.addBook(new Book("Le Petit Prince"));
firstLibrary.addBook(new Book("A Tale of Two Cities"));
firstLibrary.addBook(new Book("The Lord of the Rings"));
// Print opening hours and the addresses
System.out.println("Library hours:");
printOpeningHours();
System.out.println();
System.out.println("Library addresses:");
firstLibrary.printAddress();
secondLibrary.printAddress();
System.out.println();
// Try to borrow The Lords of the Rings from both libraries
System.out.println("Borrowing The Lord of the Rings:");
firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings");
secondLibrary.borrowBook("The Lord of the Rings");
System.out.println();
// Print the titles of all available books from both libraries
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
System.out.println();
System.out.println("Books available in the second library:");
secondLibrary.printAvailableBooks();
System.out.println();// Return The Lords of the Rings to the first
// library
System.out.println("Returning The Lord of the Rings:");
firstLibrary.returnBook("The Lord of the Rings");
System.out.println();
// Print the titles of available from the first library
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
}*/
}
Reply 2
Create an array list of books in the library class.
Arraylist<book> allBooks

Then in your add method you can just use allBooks.add(book);

Then to find if a book is borrowed you run a for loop which will be i < allBooks.size() and for each iteration call allBooks.get(i) to retrieve the book, you can then call the books method after get() so .get(i).isBorrowed() Will return your Boolean. You can also check the book is equal to the book name by calling the getTitle() method and using .equals() like it mentions in your questions PDF.
Reply 3
Original post by hhxx
Are you allowed to use other objects like arraylists / hashmaps? sorry i did the whole thing, just look at what you need to, PM me if you want it removed. I also dont know how you should be naming variables, so change as required i guess.

Edit: also made some changed to your code as there were simpler ways of doing it.

Book Class.

public class Book {
private String title;
private boolean borrowed;


// Creates a new Book
public Book(String bookTitle) {
this.title = bookTitle;
this.borrowed = false;
}


// Marks the book as rented
public void borrowed() {
this.borrowed = true;
}


// Marks the book as not rented
public void returned() {
this.borrowed = false;
}


// Returns true if the book is rented, false otherwise
public boolean isBorrowed() {
return this.borrowed;
}


// Returns the title of the book
public String getTitle() {
return this.title;
}


/*public static void main(String[] arguments) {
// Small test of the Book class
Book example = new Book("The Da Vinci Code");
System.out.println("Title (should be The Da Vinci Code): "
+ example.getTitle());
System.out.println("Borrowed? (should be false): "
+ example.isBorrowed());
example.borrowed();
System.out.println("Borrowed? (should be true): "
+ example.isBorrowed());
example.returned();
System.out.println("Borrowed? (should be false): "
+ example.isBorrowed());
}*/
}



Library Class

public class Library {
private String libraryAddress;
private Book[] Books = new Book[0];

public Library(String address) {
libraryAddress = address;
}

public void addBook(Book item) {
int Len = Books.length;
Book[] Temp = new Book[Len + 1];
for (int I = 0; I < Len; I++)
Temp = Books;

Temp[Len] = item;
Books = new Book[Temp.length];
for (int I = 0; I < Books.length; I++)
Books = Temp;
}



private void returnBook(String returningBook) {
for (int I = 0; I < Books.length; I++)
if (Books.getTitle().compareTo(returningBook) == 0)
if (Books.isBorrowed())
{
Books.returned();
System.out.println("You successfully returned " + returningBook);
return;
}
System.out.println("Sorry, this book is not from our catalog.");
}


private void printAvailableBooks() {
for (int I = 0; I < Books.length; I++)
if (!Books.isBorrowed())
System.out.println(Books.getTitle());

if (Books.length == 0)
System.out.println("No Books in catalog");
}


private void borrowBook(String requestedBook) {
for (int I = 0; I < Books.length; I++)
if (Books.getTitle().compareTo(requestedBook) == 0)
if (Books.isBorrowed())
{
System.out.println("Sorry, this book is already borrowed.");
return;
} else
{
Books.borrowed();
System.out.println("You successfully borrowed " + requestedBook);
return;
}
System.out.println("Sorry, this book is not in our catalog.");
}


private static void printOpeningHours() {
System.out.println("Libraries are open daily from 9am to 5pm.");
}


private void printAddress() {
System.out.println(this.libraryAddress);
}

/*public static void main(String[] args) {
// Create two libraries
Library firstLibrary = new Library("10 Main St.");
Library secondLibrary = new Library("228 Liberty St.");
firstLibrary.addBook(new Book("The Da Vinci Code"));
firstLibrary.addBook(new Book("Le Petit Prince"));
firstLibrary.addBook(new Book("A Tale of Two Cities"));
firstLibrary.addBook(new Book("The Lord of the Rings"));
// Print opening hours and the addresses
System.out.println("Library hours:");
printOpeningHours();
System.out.println();
System.out.println("Library addresses:");
firstLibrary.printAddress();
secondLibrary.printAddress();
System.out.println();
// Try to borrow The Lords of the Rings from both libraries
System.out.println("Borrowing The Lord of the Rings:");
firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings");
secondLibrary.borrowBook("The Lord of the Rings");
System.out.println();
// Print the titles of all available books from both libraries
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
System.out.println();
System.out.println("Books available in the second library:");
secondLibrary.printAvailableBooks();
System.out.println();// Return The Lords of the Rings to the first
// library
System.out.println("Returning The Lord of the Rings:");
firstLibrary.returnBook("The Lord of the Rings");
System.out.println();
// Print the titles of available from the first library
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
}*/
}



Thanks - that's really helpful :smile: I have some questions:

Book.java:

using 'this' - I know that this tells java to call the method only in the class in which the method is defined? Is using 'this' necessary or is it just a good programming habit? [or both].

Library.java:

I take it your allowed to make arrays of objects? ie Book[] Books = new Book[0] makes an array of Book objects called Books with (initially) 0 elements?

The addBook(Book item) method.


int Len = Books.length;
Book[] Temp = new Book[Len + 1];
for (int I = 0; I < Len; I++)
Temp = Books;

Temp[Len] = item;
Books = new Book[Temp.length];
for (int I = 0; I < Books.length; I++)
Books = Temp;


As far as I can see this makes an int Len which is the length of the array 'Books'.
Then it makes another array called Temp which is an array of Book objects with the length of 'Books' + 1.

And then the for loop goes through each element of 'Books' - but what does the Books = Temp do and why?

returnBook(String returningBook):

for (int I = 0; I < Books.length; I++)
if (Books.getTitle().compareTo(returningBook) == 0)
if (Books.isBorrowed())
{
Books.returned();
System.out.println("You successfully returned " + returningBook);
return;

As far as I can see this method goes through each element of Books and get's the title using getTitle() and then 'compares' this to the String put into the method. What does == 0 mean when using compareTo(String)?

This then checks if the book is borrowed and if it is, returns it using the returned() method from Book.java.

Why is the 'return;' line needed, as opposed to ending the method the System.out.print.... line?

Thanks loads again for your help!!

P.S. In future, i'll use the code tags :P
Reply 4
Original post by wales321
Create an array list of books in the library class.
Arraylist<book> allBooks

Then in your add method you can just use allBooks.add(book);

Then to find if a book is borrowed you run a for loop which will be i < allBooks.size() and for each iteration call allBooks.get(i) to retrieve the book, you can then call the books method after get() so .get(i).isBorrowed() Will return your Boolean. You can also check the book is equal to the book name by calling the getTitle() method and using .equals() like it mentions in your questions PDF.


Thanks - I'll try writing it this way as well and post what I've written.
Reply 5
Original post by wales321
Create an array list of books in the library class.
Arraylist<book> allBooks

Then in your add method you can just use allBooks.add(book);

Then to find if a book is borrowed you run a for loop which will be i < allBooks.size() and for each iteration call allBooks.get(i) to retrieve the book, you can then call the books method after get() so .get(i).isBorrowed() Will return your Boolean. You can also check the book is equal to the book name by calling the getTitle() method and using .equals() like it mentions in your questions PDF.


Here's what I've come up with:
Books.java is the same as in my OP.

Library.java

import java.util.*;

public class Library {
String libaryAddress;
ArrayList<Book> books = new ArrayList<Book>(); //Does <Book> only need to be declared once?

public void addBook(Book item) {
books.add(item);
}

public void borrowBook(String requestedBook) {
for (int i = 0; i < Books.length; i++)
if (books(i).getTitle().compareTo(requestedBook) == 0)
if (books(i).isBorrowed())
{
System.out.println("Sorry, this book is already borrowed.");
return;
} else
{
books(i).borrowed();
System.out.println("You successfully borrowed " + requestedBook);
return;
}
System.out.println("Sorry, this book is not in our catalog.");
}

private void returnBook(String returningBook) {
for (int i = 0; i < Books.length; i++)
if (books(i).getTitle().compareTo(returningBook) == 0)
if (books(i).isBorrowed())
{
Books.returned();
System.out.println("You successfully returned " + returningBook);
return;
}
System.out.println("Sorry, this book is not from our catalog.");
}

public void printBooks() {
if(books.size != 0) {
for(int i=0; i < books.size(); i++) {
Book item = books.get(i)
String title = item.getTitle();
System.out.println(title);
}
}
else System.out.println("No available books");
}

private static void printOpeningHours() {
System.out.println("Libraries are open daily from 9am to 5pm");
}


public static void main(String[] args) {

//Create two libraries

Library firstLibrary = new Library("10 Main St");

Library secondLibrary = new Library("228 Liberty St");

//Add four books to each library
firstLibrary.addBook(new Book("The Da Vinci Code"));
firstLibrary.addBook(new Book("Le Petit Prince"));
firstLibrary.addBook(new Book("A Tale of Two Cities"));
firstLibrary.addBook(new Book("The Lord of the Rings"));

//Print opening hours and addresses
System.out.println("Library hours:");
printOpeningHours();
System.out.println();


//Try to borrow Lord of The Rings from both libraries
System.out.println("Borrowing The Lord of the Rings:");
firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings");
secondLibrary.borrowBook("The Lord of the Rings");


//Return the Lord Of The Rings to the first library
firstLibrary.returnBook("The Lord Of The Rings");


//Print titles of available books from first library
printOpeningHours();

}
}


I've lookup up the ArrayList syntax - it's the first time ive used this so it may be slightly wrong!

As for methods - I've copy pasted methods from the above post and adapted for ArrayLists instead of Arrays.

Any corrections/advice?

Thanks a lot.
Reply 6
Original post by Chemhistorian

using 'this' - I know that this tells java to call the method only in the class in which the method is defined? Is using 'this' necessary or is it just a good programming habit? [or both].


programming habbit, i tend to use it alot in c++, not so much in java. means the same exact thing. its just easier to read for me i guess. i was quite confused because you had methods the same name as variables.

Original post by Chemhistorian

I take it your allowed to make arrays of objects? ie Book[] Books = new Book[0] makes an array of Book objects called Books with (initially) 0 elements?


Yeah i did this because i had to initialize that variable either way. although it wasn't needed until some methods. i wasn't clear on what method so i just made it global. i don't know if this is allowed or not, i'm starting uni this September :biggrin:

Original post by Chemhistorian

As far as I can see this makes an int Len which is the length of the array 'Books'.
Then it makes another array called Temp which is an array of Book objects with the length of 'Books' + 1.

And then the for loop goes through each element of 'Books' - but what does the Books = Temp do and why?


it makes an array which is one bigger then the original array to add the new book. it then resizes the old array and copies all elements back over. this is the reason why i asked if your allowed to use arraylists or hashmaps. with Arraylists i could of simply done....Books.add(item);

Original post by Chemhistorian

As far as I can see this method goes through each element of Books and get's the title using getTitle() and then 'compares' this to the String put into the method. What does == 0 mean when using compareTo(String)?

This then checks if the book is borrowed and if it is, returns it using the returned() method from Book.java.

Why is the 'return;' line needed, as opposed to ending the method the System.out.print.... line?


your first question; compareTo returns 0 if the string lexicographically equal to the compared. returns -1 if the string is lexicographically less than, and returns 1 if the string is lexicographically greater than. for future reference use ==0 for equals and != for not equal to.
your second question; this is needed because if the loop finds the book then it should set borrowed to false and immediately exit; we dont want it to keep searching, the book has already been returned. secondly if no book was found then we don't return in the for loop so we still have a book that isn't in the catalogue so we put a simple println, which will ONLY print if the book isn't found, ie function hasn't already exited.

yeah lmao use code tags hard as hell reading unformatted code :P
Reply 7
Original post by hhxx
programming habbit, i tend to use it alot in c++, not so much in java. means the same exact thing. its just easier to read for me i guess. i was quite confused because you had methods the same name as variables.



Yeah i did this because i had to initialize that variable either way. although it wasn't needed until some methods. i wasn't clear on what method so i just made it global. i don't know if this is allowed or not, i'm starting uni this September :biggrin:



it makes an array which is one bigger then the original array to add the new book. it then resizes the old array and copies all elements back over. this is the reason why i asked if your allowed to use arraylists or hashmaps. with Arraylists i could of simply done....Books.add(item);



your first question; compareTo returns 0 if the string lexicographically equal to the compared. returns -1 if the string is lexicographically less than, and returns 1 if the string is lexicographically greater than. for future reference use ==0 for equals and != for not equal to.
your second question; this is needed because if the loop finds the book then it should set borrowed to false and immediately exit; we dont want it to keep searching, the book has already been returned. secondly if no book was found then we don't return in the for loop so we still have a book that isn't in the catalogue so we put a simple println, which will ONLY print if the book isn't found, ie function hasn't already exited.

yeah lmao use code tags hard as hell reading unformatted code :P


Thanks. With the 'return;' at the end of the loop - if this is to stop the loop continuing, why not use 'break;'?

Thanks :biggrin:
Reply 8
Original post by Chemhistorian
Thanks. With the 'return;' at the end of the loop - if this is to stop the loop continuing, why not use 'break;'?

Thanks :biggrin:


break only exits the loop. so the println would still be printed.
Reply 9
Original post by Chemhistorian
Here's what I've come up with:
Books.java is the same as in my OP.

Library.java

import java.util.*;

public class Library {
String libaryAddress;
ArrayList<Book> books = new ArrayList<Book>(); //Does <Book> only need to be declared once?

public void addBook(Book item) {
books.add(item);
}

public void borrowBook(String requestedBook) {
for (int i = 0; i < Books.length; i++)
if (books(i).getTitle().compareTo(requestedBook) == 0)
if (books(i).isBorrowed())
{
System.out.println("Sorry, this book is already borrowed.");
return;
} else
{
books(i).borrowed();
System.out.println("You successfully borrowed " + requestedBook);
return;
}
System.out.println("Sorry, this book is not in our catalog.");
}

private void returnBook(String returningBook) {
for (int i = 0; i < Books.length; i++)
if (books(i).getTitle().compareTo(returningBook) == 0)
if (books(i).isBorrowed())
{
Books.returned();
System.out.println("You successfully returned " + returningBook);
return;
}
System.out.println("Sorry, this book is not from our catalog.");
}

public void printBooks() {
if(books.size != 0) {
for(int i=0; i < books.size(); i++) {
Book item = books.get(i)
String title = item.getTitle();
System.out.println(title);
}
}
else System.out.println("No available books");
}

private static void printOpeningHours() {
System.out.println("Libraries are open daily from 9am to 5pm");
}


public static void main(String[] args) {

//Create two libraries

Library firstLibrary = new Library("10 Main St");

Library secondLibrary = new Library("228 Liberty St");

//Add four books to each library
firstLibrary.addBook(new Book("The Da Vinci Code"));
firstLibrary.addBook(new Book("Le Petit Prince"));
firstLibrary.addBook(new Book("A Tale of Two Cities"));
firstLibrary.addBook(new Book("The Lord of the Rings"));

//Print opening hours and addresses
System.out.println("Library hours:");
printOpeningHours();
System.out.println();


//Try to borrow Lord of The Rings from both libraries
System.out.println("Borrowing The Lord of the Rings:");
firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings");
secondLibrary.borrowBook("The Lord of the Rings");


//Return the Lord Of The Rings to the first library
firstLibrary.returnBook("The Lord Of The Rings");


//Print titles of available books from first library
printOpeningHours();

}
}


I've lookup up the ArrayList syntax - it's the first time ive used this so it may be slightly wrong!

As for methods - I've copy pasted methods from the above post and adapted for ArrayLists instead of Arrays.

Any corrections/advice?

Thanks a lot.

For your borrow and return methods you should be usings books.get(i) and not books(i).

You are also usings books and Books, the capital letter is important, make sure they are the same.

return; is used to return something not to break out of a loop.

If you had a method public int getNumber() you would use return to return an int.
If you had a method public String getString() you would use return to return a string.

Your methods are public void, meaning when you call these methods they do not return anything, it doesn't mean you can't println something, or change variables etc though.

You don't need the return in the if else statement, you don't even need a break which is what you are trying to achieve. If the condition is met it execute code within the parenthesis, otherwise it executes to code within else. If there is no else then nothing happens if the condition is not met.

To print out each book, write a toString method in books which you can call, it will save you having to get the title, author or whatever info you need individually.

What are you using to write this? Download an IDE such as net beans and it will flag up a lot of these mistakes for you.
(edited 10 years ago)
Original post by wales321
For your borrow and return methods you should be usings books.get(i) and not books(i).

You are also usings books and Books, the capital letter is important, make sure they are the same.

return; is used to return something not to break out of a loop.

If you had a method public int getNumber() you would use return to return an int.
If you had a method public String getString() you would use return to return a string.

Your methods are public void, meaning when you call these methods they do not return anything, it doesn't mean you can't println something, or change variables etc though.

You don't need the return in the if else statement, you don't even need a break which is what you are trying to achieve. If the condition is met it execute code within the parenthesis, otherwise it executes to code within else. If there is no else then nothing happens if the condition is not met.

To print out each book, write a toString method in books which you can call, it will save you having to get the title, author or whatever info you need individually.

What are you using to write this? Download an IDE such as net beans and it will flag up a lot of these mistakes for you.


I have eclipse at home - currently at work though so I'm using Notepad for the time being :P

Thanks :smile:
Reply 11
Original post by wales321

return; is used to return something not to break out of a loop.

If you had a method public int getNumber() you would use return to return an int.
If you had a method public String getString() you would use return to return a string.


Wrong. it can be used to break out of a loop + function; Immediately exits the function. which is what he wants. return doesnt always have to return a variable. if it is a procedure then it does not have to return anything. hence why it compiles.
Reply 12
My mistake, still not required in the program he posted though.
Reply 13
Original post by wales321
My mistake, still not required in the program he posted though.


How so?

There are lots of ways to do one thing. Just because your method doesn't require it, doesn't mean its invalid for him too?
Reply 14
I think I mis read the program slightly, was trying to highlight that not every method needs to include a return like I've seen from other people before. You should look at the difference between break and return though as they both have their uses.
Reply 15
Original post by wales321
I think I mis read the program slightly, was trying to highlight that not every method needs to include a return like I've seen from other people before. You should look at the difference between break and return though as they both have their uses.


break; - Breaks out of loops and used to specify the end in switch / case statements.

return; - exits the function / procedure, returns a variable if a function.
Reply 16
I meant the guy who started the thread but there you go :smile:

Quick Reply

Latest

Trending

Trending