Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello.
I am trying to add 10 student of class Student into arraylist which i built it in Section Class but i failed to do this so i need some suggestions to my problem this is the code i used in section and followed by the code will be executed in the main :
Java
ArrayList<string> section = new ArrayList<string>();</string></string>


Java
public ArrayList<string> getStudent() {
			return section;

	}

	public void setStudent(String student) {
		section.add(student);
	}</string>

Java
public static void main(String[] args) throws Throwable {
		Teacher teacher = new Teacher();
		Course course = new Course();
		Section section = new Section();
		Student student = new Student();
		student.setName("Hani");
		student.setName("Salah");
		section.section.add(student.getName());
		student.setName("Amjad");
		System.out.println(section.section.size());
		section.setStudent(student.getName());
		System.out.println(section.getStudent());

	}
Posted
Updated 5-Dec-11 0:01am
v3
Comments
TorstenH. 5-Dec-11 8:32am    
voted +5 because THIS is the way to ask a homework related question! well done.

1 solution

Just do this:

Java
ArrayList<string> section = new ArrayList<string>();

section.add("name1");
section.add("name2");
section.add("name3");
...
section.add("name10");


0) Why are you using a method to do something as simple as adding a student name to the arraylist?

1) I don't understand why you're calling section.section.add. This shouldn't even compile (of course, Java is its own animal, and i don't write java code).

2) Consider using whatever debugger is available to you. It will reveal ALL of your mistakes in less time than it took you to type this question and wait for a response.
 
Share this answer
 
Comments
hamzah1 5-Dec-11 6:17am    
0)I used a methods because the main is not in same class that Arraylist Available so Imade getter and setter.
1)i used section .section .add trying to add item to list from the main as a suggestion.
2) i dont know how to use java debbuger.
Dylan Morley 5-Dec-11 6:38am    
2) i dont know how to use java debbuger.

If this is true, then it should be your no 1 priority! How can you expect to code without using a debugger? Guess work and asking on internet won't get you very far!

Are you using Eclipse? There are walkthroughs, examples, instructions etc all just waiting for you to read them

http://www.ibm.com/developerworks/library/os-ecbug/

TorstenH. 5-Dec-11 8:25am    
I'll second that, please use Eclipse or Netbeans for coding, both are free and will help you to write your code (also both don't know what you want to do, so you'll have to write your code yourself. But you'll get hints about your errors.)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900