Can any one help me to pass "menu method" to the "main methode", I'm having a problem, since the "main method" doesn'accept non-static. I tried instanciate the methode, since I'm passing the "String" there is a problem...
this is the Linked List:
package bookStore;
/*
An object of type bookStore represents a list of strings. Methods
are provided to insert a string into the list, to delete a string
from the list, and to check whether a given string occurs in the list.
*/
import java.io.*;
import java.util.*;
public class bookStore
{
private static class Node
{
String item;
Node next;
}
private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
private Node head; // A pointer to the first node in the linked list.
// If the list is empty, the value is null.
public static void main(String[] args)
{
menu();
}
public static void menu()
{
//Node insertItem = new Node();
try
{
//Menu choice
System.out.println("1. Add Book");
System.out.println("2. Delete an item.");
System.out.println("3. Find an item.");
System.out.println("4. Exit from this program.");
System.out.println("---------------------------");
//Input number
System.out.print("Select menu : ");
String InputInt = stdin.readLine();
if (Integer.parseInt(InputInt) == 1)
{
//insert("insertItem");
}
else if (Integer.parseInt(InputInt) == 2)
{
//delete();
}
else if (Integer.parseInt(InputInt) == 3)
{
//find();
}
else if (Integer.parseInt(InputInt) == 4)
{
}
}
catch (Exception e) { }
}
public boolean find(String searchItem)
{
// Returns true if the specified item is in the list, and
// false if it is not in the list. (For demonstration
// purposes, this method does not use the fact that the
// list is ordered.)
Node book;
book = head;
while (book != null)
{
// Go through the list looking at the string in each
// node. If the string is the one we are looking for,
// return true, since the string has been found in the list.
if (book.item.equals(searchItem))
return true;
book = book.next;
}
// At this point, we have looked at all the items in the list
// without finding searchItem. Return false to indicate that
// the item does not exist in the list.
return false;
}
public boolean delete(String deleteItem)
{
// If the specified string occurs in the list, delete it.
// Return true if the string was found and deleted. If the
// string was not found in the list, return false. (If the
// item exists multiple times in the list, this method
// just deletes the first one.)
if (head == null)
{
// The list is empty, so it certainly doesn't contain deleteString.
return false;
}
else if (head.item.equals(deleteItem))
{
// The string is the first item of the list. Remove it.
head = head.next;
return true;
}
else
{
// The string, if it occurs at all, is somewhere beyond the
// first element of the list. Search the list.
Node book;
Node previous;
book = head.next;
previous = head;
while (book != null && book.item.compareTo(deleteItem) < 0)
{
// Move previous and book along the list until book
// falls off the end or hits a list element that is
// greater than or equal to deleteItem. When this
// loop ends, book indicates the position where
// deleteItem must be, if it is in the list.
previous = book;
book = book.next;
}
if (book != null && book.item.equals(deleteItem))
{
// book points to the node that is to be deleted.
// Remove it by changing the pointer in the previous node.
previous.next = book.next;
return true;
}
else
{
// The item does not exist in the list.
return false;
}
}
} // end delete()
public void insert(String insertItem)
{
try{
// Add insertItem to the list. It is allowed to add
// multiple copies of the same item.
Node newNode;
newNode = new Node();
newNode.item = insertItem;
//Read a line of text from the user(Title).
System.out.print("Book title : ");
insertItem = stdin.readLine();
if (head == null)
{
// The new item is the first (and only) one in the list.
// Set head to point to it.
head = newNode;
}
else if (head.item.compareTo(insertItem) >= 0)
{
// The new item is less than the first item in the list,
// so it has to be inserted at the head of the list.
newNode.next = head;
head = newNode;
}
else
{
// The new item belongs somewhere after the first item
// in the list. Search for its proper position and insert it.
Node book;
Node previous;
book = head.next;
previous = head;
while (book != null && book.item.compareTo(insertItem) < 0)
{
// Move previous and book along the list until book
// falls off the end or hits a list element that is
// greater than or equal to insertItem. When this
// loop ends, book indicates the position where
// insertItem must be inserted.
previous = book;
book = book.next;
}
newNode.next = book;
previous.next = newNode;
}
}catch(Exception e){}
} // end insert()
public String[] getElements()
{
// Returns an array containing all the elements
// in the list.
int count;
Node book;
String[] elements;
// First, go through the list and count the number
// of elements that it contains.
count = 0;
book = head;
while (book != null)
{
count++;
book = book.next;
}
// Create an array just large enough to hold all the
// list elements.
elements = new String[count];
book = head;
count = 0;
while (book != null)
{
elements[count] = book.item;
count++;
book = book.next;
}
// Return the array that has been filled with the list elements.
return elements;
} // end getElements()
public void Print()
{
Node book = head;
while (book != null)
{
System.out.println(book.item);
book = book.next;
}
}
} // end class bookStore
Comments
:
: this is the Linked List:
:
: package bookStore;
:
: /*
: An object of type bookStore represents a list of strings. Methods
: are provided to insert a string into the list, to delete a string
: from the list, and to check whether a given string occurs in the list.
:
: */
: import java.io.*;
: import java.util.*;
:
: public class bookStore
: {
:
: private static class Node
: {
: String item;
: Node next;
: }
:
: private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
: private Node head; // A pointer to the first node in the linked list.
: // If the list is empty, the value is null.
:
: public static void main(String[] args)
: {
: menu();
: }
:
: public static void menu()
: {
: //Node insertItem = new Node();
: try
: {
: //Menu choice
: System.out.println("1. Add Book");
: System.out.println("2. Delete an item.");
: System.out.println("3. Find an item.");
: System.out.println("4. Exit from this program.");
: System.out.println("---------------------------");
:
: //Input number
: System.out.print("Select menu : ");
: String InputInt = stdin.readLine();
:
: if (Integer.parseInt(InputInt) == 1)
: {
: //insert("insertItem");
: }
: else if (Integer.parseInt(InputInt) == 2)
: {
: //delete();
: }
: else if (Integer.parseInt(InputInt) == 3)
: {
: //find();
: }
: else if (Integer.parseInt(InputInt) == 4)
: {
:
: }
: }
: catch (Exception e) { }
: }
:
: public boolean find(String searchItem)
: {
: // Returns true if the specified item is in the list, and
: // false if it is not in the list. (For demonstration
: // purposes, this method does not use the fact that the
: // list is ordered.)
:
: Node book;
:
: book = head;
: while (book != null)
: {
: // Go through the list looking at the string in each
: // node. If the string is the one we are looking for,
: // return true, since the string has been found in the list.
: if (book.item.equals(searchItem))
: return true;
: book = book.next;
: }
:
: // At this point, we have looked at all the items in the list
: // without finding searchItem. Return false to indicate that
: // the item does not exist in the list.
:
: return false;
:
: }
:
:
: public boolean delete(String deleteItem)
: {
: // If the specified string occurs in the list, delete it.
: // Return true if the string was found and deleted. If the
: // string was not found in the list, return false. (If the
: // item exists multiple times in the list, this method
: // just deletes the first one.)
:
: if (head == null)
: {
: // The list is empty, so it certainly doesn't contain deleteString.
: return false;
: }
: else if (head.item.equals(deleteItem))
: {
: // The string is the first item of the list. Remove it.
: head = head.next;
: return true;
: }
: else
: {
: // The string, if it occurs at all, is somewhere beyond the
: // first element of the list. Search the list.
: Node book;
: Node previous;
: book = head.next;
: previous = head;
: while (book != null && book.item.compareTo(deleteItem) < 0)
: {
: // Move previous and book along the list until book
: // falls off the end or hits a list element that is
: // greater than or equal to deleteItem. When this
: // loop ends, book indicates the position where
: // deleteItem must be, if it is in the list.
: previous = book;
: book = book.next;
: }
: if (book != null && book.item.equals(deleteItem))
: {
: // book points to the node that is to be deleted.
: // Remove it by changing the pointer in the previous node.
: previous.next = book.next;
: return true;
: }
: else
: {
: // The item does not exist in the list.
: return false;
: }
: }
:
: } // end delete()
:
:
: public void insert(String insertItem)
: {
: try{
: // Add insertItem to the list. It is allowed to add
: // multiple copies of the same item.
:
: Node newNode;
: newNode = new Node();
: newNode.item = insertItem;
:
: //Read a line of text from the user(Title).
: System.out.print("Book title : ");
: insertItem = stdin.readLine();
:
: if (head == null)
: {
: // The new item is the first (and only) one in the list.
: // Set head to point to it.
: head = newNode;
: }
: else if (head.item.compareTo(insertItem) >= 0)
: {
: // The new item is less than the first item in the list,
: // so it has to be inserted at the head of the list.
: newNode.next = head;
: head = newNode;
: }
: else
: {
: // The new item belongs somewhere after the first item
: // in the list. Search for its proper position and insert it.
: Node book;
: Node previous;
: book = head.next;
: previous = head;
: while (book != null && book.item.compareTo(insertItem) < 0)
: {
: // Move previous and book along the list until book
: // falls off the end or hits a list element that is
: // greater than or equal to insertItem. When this
: // loop ends, book indicates the position where
: // insertItem must be inserted.
: previous = book;
: book = book.next;
: }
: newNode.next = book;
: previous.next = newNode;
: }
: }catch(Exception e){}
:
: } // end insert()
:
:
: public String[] getElements()
: {
: // Returns an array containing all the elements
: // in the list.
:
: int count;
: Node book;
: String[] elements;
:
: // First, go through the list and count the number
: // of elements that it contains.
:
: count = 0;
: book = head;
: while (book != null)
: {
: count++;
: book = book.next;
: }
:
: // Create an array just large enough to hold all the
: // list elements.
:
: elements = new String[count];
: book = head;
: count = 0;
: while (book != null)
: {
: elements[count] = book.item;
: count++;
: book = book.next;
: }
:
: // Return the array that has been filled with the list elements.
:
: return elements;
:
: } // end getElements()
:
: public void Print()
: {
: Node book = head;
: while (book != null)
: {
: System.out.println(book.item);
: book = book.next;
: }
: }
: } // end class bookStore
:
:
:
:
Generally you create an object within the main method. Then that object can be used to run your program. You could code it like this:
[code]
public class MyApp (extends ...)
{
public void performMain(String[] args)
{
// actual running code
}
public static void main(String[] args)
{
MyApp myapp = new MyApp()
myapp.performMain(args);
}
}
[/code]