Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Java Menu


  • Please log in to reply

#1
zell_ffhut

zell_ffhut

    Member

  • Member
  • PipPip
  • 34 posts
Hello

Im trying to make a menu screen for a Java program in a method, so i can call it whenever i want.

However, I having problems with this!

Can anyone see where im going wrong? :tazz:

package assignmentone;

/**
 *
 * @author chris
 */

import java.io.*;

public class Main 
{
	static private BufferedReader in =
	new BufferedReader(new InputStreamReader(System.in));
	
	private String menu;
	
	public String showMenu()
	{
		System.out.println("------------------------------------------------"); 
		System.out.println("-- Welcome to the Student Registration System --");
		System.out.println("--		  Please make your choice		   --");
		System.out.println("--											--");
		System.out.println("-- 1. Add Student							 --");
		System.out.println("-- 2. Submit Student Modules				  --");
		System.out.println("-- 3. Update Module Results				   --");
		System.out.println("-- 4. Display a Student Record				--");
		System.out.println("-- 5. Display all Student Records			 --");
		System.out.println("-- 6. Edit Module							 --");
		System.out.println("-- 7. Display Module Details				  --");
		System.out.println("-- 8. Display all Post Grad Students		  --");
		System.out.println("-- 9. Display all Under Grad Students		 --");
		System.out.println("------------------------------------------------");   
		
		return menu;
	}
	
	public static void main(String[] args)
	{
		
		System.out.println(menu);
	 
		String studName;
		int i = 1;
		  
		Student s1 = new Student(i);
		
		System.out.println("Please enter the Student Name - ");
		studName = Text.ReadString(in);
		System.out.println(studName);
	}
}


  • 0

Advertisements


#2
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
Well, first of all, you are trying to access non-static variables from within a static context (the main method). You need to either make them static, or create an instance of your class and use that.

Also, take a look at your method here:
	private String menu;
	
	public String showMenu()
	{
		System.out.println("------------------------------------------------"); 
		System.out.println("-- Welcome to the Student Registration System --");
		System.out.println("--		  Please make your choice		   --");
		System.out.println("--											--");
		System.out.println("-- 1. Add Student							 --");
		System.out.println("-- 2. Submit Student Modules				  --");
		System.out.println("-- 3. Update Module Results				   --");
		System.out.println("-- 4. Display a Student Record				--");
		System.out.println("-- 5. Display all Student Records			 --");
		System.out.println("-- 6. Edit Module							 --");
		System.out.println("-- 7. Display Module Details				  --");
		System.out.println("-- 8. Display all Post Grad Students		  --");
		System.out.println("-- 9. Display all Under Grad Students		 --");
		System.out.println("------------------------------------------------");   
		
		return menu;
	}
You'll probably get an error saying that menu might not have been initialized. Anyway, this method would be better off as void:
	public void showMenu()
	{
		System.out.println("------------------------------------------------"); 
		System.out.println("-- Welcome to the Student Registration System --");
		System.out.println("--		  Please make your choice		   --");
		System.out.println("--											--");
		System.out.println("-- 1. Add Student							 --");
		System.out.println("-- 2. Submit Student Modules				  --");
		System.out.println("-- 3. Update Module Results				   --");
		System.out.println("-- 4. Display a Student Record				--");
		System.out.println("-- 5. Display all Student Records			 --");
		System.out.println("-- 6. Edit Module							 --");
		System.out.println("-- 7. Display Module Details				  --");
		System.out.println("-- 8. Display all Post Grad Students		  --");
		System.out.println("-- 9. Display all Under Grad Students		 --");
		System.out.println("------------------------------------------------");   
	}

Hope this helps.
  • 0

#3
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Ok, Thanks.

I understand what your saying. but how do i then call that method under the public static void main(String[] args) part of the program?
  • 0

#4
Thef0rce

Thef0rce

    Member

  • Member
  • PipPipPip
  • 380 posts
you call it in main simply by using:

showMenu();

since it returns void, that call will simply print the menu out

edit: oh yeah, of course the showMenu function will have to be public and static. That's easier than making an instance of it, in my opinion anyway :tazz:

Edited by Thef0rce, 24 February 2006 - 02:00 PM.

  • 0

#5
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
As I said before, either make it static (public static void showMenu()) and just call it by doing showMenu() or create an instance of your class, and call it with that:
public static void main(String[] args) {
	Main main = new Main();
	main.showMenu();
}

  • 0

#6
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts

edit: oh yeah, of course the showMenu function will have to be public and static. That's easier than making an instance of it, in my opinion anyway :tazz:

No, it doesn't have to be public. No matter if it's private, public, or protected, it will always be accessible by the class it is in. Also, you shouldn't just make it static because it's "easier"... there is a big difference between instance and class members.

Edited by destin, 24 February 2006 - 02:16 PM.

  • 0

#7
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Thanks for the guidence. Been a great help.

What would be the easiest way to search for records in an array, with the intention of editing them.

I have this code now..


import java.io.*;

public class Main 
{
	static private BufferedReader in =
	new BufferedReader(new InputStreamReader(System.in));
	
	private String menu;
	
	public static void showMenu()
	{
		System.out.println("------------------------------------------------"); 
		System.out.println("-- Welcome to the Student Registration System --");
		System.out.println("--		  Please make your choice		   --");
		System.out.println("--											--");
		System.out.println("-- 1. Add Student							 --");
		System.out.println("-- 2. Submit Student Modules				  --");
		System.out.println("-- 3. Update Module Results				   --");
		System.out.println("-- 4. Display a Student Record				--");
		System.out.println("-- 5. Display all Student Records			 --");
		System.out.println("-- 6. Edit Module							 --");
		System.out.println("-- 7. Display Module Details				  --");
		System.out.println("-- 8. Display all Post Grad Students		  --");
		System.out.println("-- 9. Display all Under Grad Students		 --");
		System.out.println("------------------------------------------------");   
	}
	
	public static void main(String[] args)
	{
		
		showMenu();
	 
		int menuChoice;
		String studName;
		int sNum;
		int i = 1;
		
		System.out.println("Please choose your desired task");
		menuChoice = Text.ReadInt(in);
		  
		if (menuChoice == 1)
		{
			Student s1 = new Student(i);
			System.out.println("Please enter the Student Number - ");
			sNum = Text.ReadInt(in);
			System.out.println("Please enter the Students Name - ");
			studName = Text.ReadString(in);
			System.out.print("Thank you for registering with us " + studName); 
			System.out.println(", your student number is " + sNum);
			i++;
		}
		
	}
}


and the class..

import java.io.BufferedReader;

/**
 *
 * @author chris
 */
public class Student 
{
	//Attributes
	private String name;
	private int studNum [];
	private String grade;
	private int [] modules;
	
	/** Creates a new instance of Student */
	//Sets module numbers
	public Student(int moduleNum) 
	{
	  modules=new int[moduleNum]; 
	}
	
	//Sets student Name
	public void setName (String studName)
	{
		name = studName;
	}
	//Sets Student Number
	public void setStudNum (int sNum[])
	{
		studNum = sNum;
	}
	
	
}


I thought that prehaps using the student number as the array value, rarther than "i" would be a good start.. but since im new to Java, im not sure how to implement it properly!
  • 0

#8
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
I'm sorry, but I don't understand your question. Can you be more specific to your problem?
  • 0

#9
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Ok.

Im trying to create a student registration program.

I have a Student Class, with all the attributes i want. Am i right in thinking each instance of the class will need its own position in an Array? I belive it would in Pascal, which most of my programming experiance comes from.

Also, i want to be able to use the sNum variable to search for and display individual instances of the class Student. What would be the easiest way to do this?
  • 0

#10
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
If you want to have an array of Students, then you need to create that in your Main class, not the Student class.
Student[] student = new Student[sizeOfArray];
But seeing as you have add student and all, you probably want an ArrayList:
ArrayList<Student> student = new ArrayList<Student>();
Then, you can have things like:
public void addStudent(Student s) {
	student.add(s);
}
Etc... hope this helps.
  • 0

Advertisements


#11
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Ok, i understand the Array list i belive.. much like a linked list?

However, i get the error "generics not supported in -source 1.4, try -source 1.5 to enable generics"

Not sure what this means.

Im using netbeans 5, and JDK 1.42_10.


Also, the last method you describe, am i right in thinking this should go in the student class?

Edited by zell_ffhut, 28 February 2006 - 05:04 AM.

  • 0

#12
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
Looks like you have an older version of the JDK... you can't use generics. Your ArrayList must look like this.
ArrayList list = new ArrayList();

Im using netbeans 5, and JDK 1.42_10.
Also, the last method you describe, am i right in thinking this should go in the student class?

No. The last method is adding a student to the ArrayList of students. It wouldn't make any sense for it to be in the Student class.

Think of your student class as having things only students have.
public class Student {
	private int ID;
	private int name;
	private double GPA;
	
	// ...
}

  • 0

#13
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Ok, i think im starting to get a better picture of this now.

The "ArrayList list = new ArrayList();" line is comming up with the error "can not ind symbol, class ArrayList"
  • 0

#14
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
The ArrayList class is in the java.util package; you must import it.
import java.util.ArrayList; // imoprt java.util.*; will work too

  • 0

#15
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Thanks a lot :tazz:

Im now having trouble printing records of the list to screen. I belive it should be done using the code

student.toString();  

But it complains about a non static variable being referenced from a static context.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP