Error cannot find symbol. new to java programming

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 4921 Views - Last Post: 02 August 2011 - 08:03 PM Rate Topic: -----

#1 gonzo84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-August 11

Error cannot find symbol. new to java programming

Posted 02 August 2011 - 11:22 AM

Hello everyone,
I have class project and my professor instructions says
"In the Roster class provide a method listRoster(), that loops over the students array and
prints out the names. It also prints the index (starting with 0) for each student."

I am using jgrasp to compile

My question is on why am I getting this error on line 23 the Arraylist. Any help is welcomed thanks in advance, I know I'm missing something easy.

public class Roster
{
	private String[] students;
	private int numberOfStudents;
		
		public Roster()
			{
			students = new String[25];
			numberOfStudents = 0;
					
			}
			
			public void createAndAddStudent(String name)
    		           {
      	                for (int i = 0; i < numberOfStudents; i++)
                           students[i]= name; 
    		           }
			
			public void listRoster()
			{
				ArrayList<String> listRoster = new ArrayList<String>();
				
			}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error cannot find symbol. new to java programming

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 12:03 PM

Did you import java.util.ArrayList;
Was This Post Helpful? 0
  • +
  • -

#3 gonzo84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-August 11

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 12:06 PM

yes i did. thank you
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 12:20 PM

Your list has the same name as the method, also if you want to loop through student array, then you need to have a loop in that method that will print names and indexes as said

Quote

"In the Roster class provide a method listRoster(), that loops over the students array and
prints out the names. It also prints the index (starting with 0) for each student."

Your method should look something like:
public void listRoster()
{
for(int i = 0; i < students.Length;i++)
  System.out.println("Name:" + students[i] + "Index:" + i);
} 

Or something like that, may be you tell us what exactly the error message said and you mean to do here
Was This Post Helpful? 0
  • +
  • -

#5 gonzo84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-August 11

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 12:52 PM

Ok Thank you for your help thus far I graduate this summer and this is the last project of the semester
I have made some changes and got rid of the error and the program compiled until I corrected my method for list(roster). I know I'm missing something. To help with understanding what I'm trying to do I included all of my professor instructions.

/*
File Name: Student.java
*/

/**
	This class describes a student. A student just has a name
*/
import java.util.ArrayList; 
public class Student
{
	// Instance field	
	private String name;
	
	/**
		Creates a student with the empty string as the name.
	*/
	
	public Student()
	{
		name = "";
	}
	
	/**
		Returns the name of the student.
		@return the name of the student.
	*/
	public String getName()
	{
		return name;
	}
	
	/**
		Changes the name of the student.
		@param newName the new name of the4 student
	*/
	public void setName(String newName)
	{
		name = newName;
	}
	
	// Not required, but always worth a look

	// For @Override, see
	// http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why	
	@Override
	public String toString()
	{
		return "A student named " + name;
	}	
}
	
	class Roster
{
	private String[] students;
	private int numberOfStudents;
		
		public Roster()
			{
			students = new String[25];
			numberOfStudents = 0;
					
			}
			
			public void createAndAddStudent(String name)
    		{
      	for (int i = 0; i < numberOfStudents; i++)
            students[i]= name; 
    		}
			
			public void listRoster()
			{
				for(int i = 0; i < numberOfStudents; i++)
					System.out.println(i+":" + name);			
			}
			
}




Now I am getting an error
Student.java:77: cannot find symbol
symbol : variable name
location: class Roster
System.out.println(i+":" + name);	

^
I haven't created the tester yet.

professor instructions
Write a class Roster with an instance field students which is a Student[]. Additionally, it
contains an int variable numberOfStudents, that counts how many students are
currently enrolled (c. 316 317 in your textbook).
Write a default constructor (!) that creates space for 25 students in this array and initializes
the number of students well, correctly.
To add a student to the roster, we just need the name the Student instance will be created
by the Roster automatically.
In the Roster class provide a method createAndAddStudent(String name), that a)
creates an instance of Student with this name (by calling the appropriate constructor in the
Student class) and B) adds this to the end of the list.
At the moment, you don't have to worry about running over the array bounds!
Now we want to be able to produce a list of the roster, of course.
In the Roster class provide a method listRoster(), that loops over the students array and
prints out the names. It also prints the index (starting with 0) for each student.
After this is done, it's time to work on a tester.
Write a class RosterTester that a) creates an instance of Roster (by calling the default
constructor), B) in a loop reads a student name and adds it to the Roster instance by calling
the createAndAddStudent method. If the name is empty () the name should not be
added to the roster and the read loop should finish.
After this is done the tester calls the listRoster() method to print a list of all enrolled
students.

Here is the output he wants
Example:
>Enter a student's name: Suzy Creamcheese
>Enter a student's name: Gerald Bostock
>Enter a student's name: Peter Parker
>Enter a student's name: Amy Pond
>Enter a student's name: Sid Vicious
>Enter a student's name: Kaspar Hauser
>Enter a student's name:
>0: Suzy Creamcheese
>1: Gerald Bostock
>2: Peter Parker
>3: Amy Pond
>4: Sid Vicious
>5: Kaspar Hauser
Was This Post Helpful? 0
  • +
  • -

#6 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 12:58 PM

			public void listRoster()
			{
				for(int i = 0; i < numberOfStudents; i++)
					System.out.println(i+":" + name);			
			}



You probably want something like this:
			public void listRoster()
			{
				for(int i = 0; i < numberOfStudents; i++)
					System.out.println(i+":" + student[i].getName());			
			}



What are you trying to do here??

			public void createAndAddStudent(String name)
    		{
      	for (int i = 0; i < numberOfStudents; i++)
            students[i]= name; 
    		}


Was This Post Helpful? 0
  • +
  • -

#7 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 01:06 PM

If you are trying to create a student and add him to the roster I would assume you would need to do this.

++numberOfStudents; //add one to the number of students since we are adding one
student[numberOfStudents] = new Student(name); //Create a new student and add them to the array.



EDIT: You do not need a loop since you are adding one student. However you should probably also test to see if you have less than 25 (24 since you start at 0) students first since your array can only hold 25 students.

This post has been edited by ipushmycar: 02 August 2011 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#8 gonzo84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-August 11

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 01:12 PM

View Postipushmycar, on 02 August 2011 - 12:58 PM, said:

What are you trying to do here??

			public void createAndAddStudent(String name)
    		{
      	for (int i = 0; i < numberOfStudents; i++)
            students[i]= name; 
    		}


Trying to provide a method createAndAddStudent(String name),that a ) creates an instance of Student with this name (by calling the appropriate constructor in the Student class ) and b ) adds this to the end of the list.

Thanks
Was This Post Helpful? 0
  • +
  • -

#9 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 01:18 PM

Read post above yours. Post your updated code and any more errors or questions.
Was This Post Helpful? 0
  • +
  • -

#10 gonzo84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-August 11

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 01:54 PM

Here is my code for the program and it is compiling
/*
File Name: Student.java
*/

/**
	This class describes a student. A student just has a name
*/
import java.util.ArrayList; 
public class Student
{
	// Instance field	
	private String name;
	
	/**
		Creates a student with the empty string as the name.
	*/
	
	public Student()
	{
		name = "";
	}
	
	/**
		Returns the name of the student.
		@return the name of the student.
	*/
	public String getName()
	{
		return name;
	}
	
	/**
		Changes the name of the student.
		@param newName the new name of the4 student
	*/
	public void setName(String newName)
	{
		name = newName;
	}
	
	// Not required, but always worth a look

	// For @Override, see
	// http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why	
	@Override
	public String toString()
	{
		return "A student named " + name;
	}	
}
	
	class Roster
{
	private String[] students;
	private int numberOfStudents;
		
		public Roster()
			{
			students = new String[25];
			numberOfStudents = 0;
					
			}
			
			public void createAndAddStudent(String name)
    		{
      	for (int i = 0; i < numberOfStudents; i++)
    	        
				name = students[i]; 
    		}
			
			public void listRoster()
			{
				for(int i = 0; i < numberOfStudents; i++)
	        System.out.println(i+":" + students[i]);			
			}
			
}


Here is the Tester of the above program
import java.util.Scanner;
public class RosterTester
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		System.out.print("Enter Student name place: ");
		Roster list = new Roster();
		String name = in.nextLine();
		System.out.println(Roster.createAndAddStudent(name));
		
	}
}

Professor instructions for Tester program
Write a class RosterTester that a) creates an instance of Roster (by calling the default
constructor), B) in a loop reads a student name and adds it to the Roster instance by calling
the createAndAddStudent method. If the name is empty (“”) the name should not be
added to the roster and the read loop should finish.
After this is done the tester calls the listRoster() method to print a list of all enrolled
students.

I am getting 3 errors but i don't understand why.

RosterTester.java:8: cannot find symbol
symbol : class Roster
location: class RosterTester
Roster list = new Roster();
^
RosterTester.java:8: cannot find symbol
symbol : class Roster
location: class RosterTester
Roster list = new Roster();
^
RosterTester.java:10: cannot find symbol
symbol : variable Roster
location: class RosterTester
System.out.println(Roster.createAndAddStudent(name));
^
3 errors

This post has been edited by gonzo84: 02 August 2011 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 02:03 PM

I would suggest you put your Roster class in its own file(Roster.java). Also at this line:
 System.out.println(Roster.createAndAddStudent(name)); 

That method is not static, so you need to use an instance of Roster to call it, like:
 System.out.println(list.createAndAddStudent(name)); 


EDIT: Also I dont see where you used your Student class, I was expecting it will be used in Roster Class to store new Students?!!

This post has been edited by smohd: 02 August 2011 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 02:12 PM

Actually
		
	public void createAndAddStudent(String name)
        {
      	      for (int i = 0; i < numberOfStudents; i++)
                  students[i]= name; 
        }


should look like this
		
	public void createAndAddStudent(String name)
        {
      	      students[numberOfStudents++] = name;
        }


Was This Post Helpful? 0
  • +
  • -

#13 gonzo84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-August 11

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 03:41 PM

View Postpbl, on 02 August 2011 - 02:12 PM, said:

Actually
		
	public void createAndAddStudent(String name)
        {
      	      for (int i = 0; i < numberOfStudents; i++)
                  students[i]= name; 
        }


should look like this
		
	public void createAndAddStudent(String name)
        {
      	      students[numberOfStudents++] = name;
        }


Thanks you I have updated my code.

View Postsmohd, on 02 August 2011 - 02:03 PM, said:

I would suggest you put your Roster class in its own file(Roster.java). Also at this line:
 System.out.println(Roster.createAndAddStudent(name)); 

That method is not static, so you need to use an instance of Roster to call it, like:
 System.out.println(list.createAndAddStudent(name)); 


EDIT: Also I dont see where you used your Student class, I was expecting it will be used in Roster Class to store new Students?!!

I think I'm getting confuse now.

I edit my code to include Student class see below:
	import java.util.ArrayList;
	public class Roster
{
	private String[] students;
	private int numberOfStudents;
		
		public Roster()
			{
			students = new String[25];
			numberOfStudents = 0;
					
			}
						
			public void createAndAddStudent(String name)
    		{
      	students[numberOfStudents++] = name;
    		}
			
			
			public void listRoster()
			{
				for(int i = 0; i < numberOfStudents; i++)
	        System.out.println(i+":" + students[i]);			
			}
			
}
class Student
{
	// Instance field	
	private String name;
	
	/**
		Creates a student with the empty string as the name.
	*/
	
	public Student()
	{
		name = "";
	}
	
	/**
		Returns the name of the student.
		@return the name of the student.
	*/
	public String getName()
	{
		return name;
	}
	
	/**
		Changes the name of the student.
		@param newName the new name of the4 student
	*/
	public void setName(String newName)
	{
		name = newName;
	}
}


Also I am getting the error
RosterTester.java:10: 'void' type not allowed here
System.out.println(list.createAndAddStudent(name));

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#14 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 04:06 PM

Oh, I dint see if it returns void, So you are just adding value then no need to print method. The line will be just:
 list.createAndAddStudent(name); 


Also you have just rearranged your code, What I was trying to say is that even if you delete Student class your code will be doing the same as now since you dont create and use any of its instances in your class(or may be still in progress!)
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Error cannot find symbol. new to java programming

Posted 02 August 2011 - 04:57 PM

You want to work woith String or with Student ?
In that case your Roster should have an array of Student not String
Your Student class needs a constructor to receive the name
and a toString() method so it will display the Student name in a println() statement


public class Roster
{
	private Student[] students;
	private int numberOfStudents;

	public Roster()
	{
		students = new Student[25];
		numberOfStudents = 0;

	}

	public void createAndAddStudent(String name)
	{
		students[numberOfStudents++] = new Student(name);
	}


	public void listRoster()
	{
		for(int i = 0; i < numberOfStudents; i++)
			System.out.println(i+":" + students[i]);			
	}

	public static void main(String[] args) {
		Roster r = new Roster();
		r.createAndAddStudent("John");
		r.createAndAddStudent("Paul");
		r.createAndAddStudent("Ringo");
		r.createAndAddStudent("Georges");
        r.listRoster();
	}
}
class Student
{
	// Instance field	
	private String name;

	/**
		Creates a student with the empty string as the name.
	 */

	public Student(String name)
	{
		this.name = name;
	}

	/**
		Returns the name of the student.
		@return the name of the student.
	 */
	public String getName()
	{
		return name;
	}

	/**
		Changes the name of the student.
		@param newName the new name of the4 student
	 */
	public void setName(String newName)
	{
		name = newName;
	}
	
	// so it can be used in a println() statment
	public String toString() {
		return name;
	}
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2