java compiling error

I’m confused. I’m attempting to compile this program, but it refuses to do so because it can’t find 4 variables, (Name, Type, numLegs, and temp) in the test program. but i thought that since i had defined these variables in the class, the main method should not be asking for them. what the heck did i miss?
[CODEpublic class animal
{
public String Name;
public String Type;
public int numLegs;

public animal()
{
	Name = "NoName";
	Type = "NoClass";
	numLegs = 0;
}
public String getName()
{
	return Name;
}
public String getType()
{ 
	return Type;
}
public int getnumLegs()
{
	return numLegs;
}
public void setName(String NameString)
{
	Name = NameString;
	if (Name.equals(" ")) {
		Name = "Blank Name";
	}
}
public void setType(String TypeString)
{
	Type = TypeString;
	if (Type.equals(" ")) {
		Type = "Blank Type";
	}
}
public void setnumLegs(int num)
{
	numLegs = num;
	if (numLegs < 0) {
		numLegs = 0;
	}
}
public String toString()
{
	String temp = Name + " " + Type + " " + numLegs;
	return temp;
}

}]






import java.util.Scanner;

class TestAnimal
{
public static void main(String args)
{
animal a1 = new animal();
Scanner keyboard = new Scanner(System.in);

	System.out.println("Enter the name of an animal");
	
	Name== = keyboard.next();
	System.out.println("Enter the class");
	Type = keyboard.next();
	System.out.println("Enter the number of legs");
	numLegs = keyboard.nextInt();
	
	System.out.println("You have entered:" + temp);
	System.out.println("Thank you");
}

}


I think the problem is, where you think you want Name, Type, and numLegs, you really want a1.Name, a1.Type, and a1.numLegs. Just because the variables are defined Public in the class doesn’t make them global - it just means you can access them from outside of the class code itself.

Also, instead of “temp”, I think you want a1.toString().

And the declarations for those variable types should probably be private (fields), since you have a set of get/set (property) methods in the class. You don’t want someone to bypass those properties.

Also, isn’t toString() an inherited member?

You might want to override.

thank you. that actually solved the problem nicely. however i made a new program and it works about 95%. the problem i have now is that when it takes input for the student info, i have a nice gap for the ID number. When it comes to the first and last name, the program insist on putting the two together. I don’t know why.


public class student
{
	public String firstName, lastName, temp;
	public int studentID;
	
	public String getfirstName()
	{
		return firstName;
	}
	public String getlastName()
	{
		return lastName;
	}
	public int getstudentID()
	{
		return studentID;
	}
	public void setfirstName(String newfirstName)
	{
		firstName = newfirstName;
	}
	public void setlastName(String newlastName)
	{
		lastName = newlastName;
	}
	public void setstudentID(int newstudentID)
	{
		studentID = newstudentID;
	}
	public String toString()
	{
		temp = studentID + " " + lastName + " " + firstName;
		return temp;
	}
}


import java.util.Scanner;

public class gradeBook6{
	
	public static void main(String[] args) 
	{
		Scanner keyboard = new Scanner(System.in);
		
		char grade;
		String courseName;
		int courseSection, courseNumber;
		int score;
		boolean exit = false;
		student s1 = new student();
		student s2 = new student();
		student s3 = new student();
		student s4 = new student();
		student s5 = new student();
		while (!exit) {
			System.out.println("Enter \'c\' to input course info, \'n\' for student, \'g\' for grades, \'q\' to exit, ");
			String response = keyboard.next();
			
			if (response.equalsIgnoreCase("c")) {
				System.out.println("Enter the course name");
				courseName = keyboard.next();
				System.out.println("Enter the course section");
				courseSection = keyboard.nextInt();
				System.out.println("Enter the course number");
				courseNumber = keyboard.nextInt();                   
				System.out.println("The course info is " + courseName + " " + courseSection + " "
					+ courseNumber);
			 }
			else if (response.equalsIgnoreCase("n")) {
				System.out.println("Enter the student 1's ID");
				s1.studentID = keyboard.nextInt();
				
				System.out.println("Please enter student 1's first name:");
				s1.firstName = keyboard.nextLine();
				
				System.out.println("Please enter student 1's last name:");
				s1.lastName = keyboard.nextLine();
				
				System.out.println("Please enter student 2's ID");
				s2.studentID = keyboard.nextInt();
				
				System.out.println("Please enter student 2's first name");
				s2.firstName = keyboard.nextLine();

				System.out.println("Please enter student 2's last name");
				s2.lastName = keyboard.nextLine();
				
				System.out.println("Please enter student 3's ID");
				s3.studentID = keyboard.nextInt();
				
				System.out.println("Please enter student 3's first name");
				s3.firstName = keyboard.nextLine();
				
				System.out.println("Please enter student 3's last name");
				s3.lastName = keyboard.nextLine();
				
				System.out.println("Please enter student 4's ID");
				s4.studentID = keyboard.nextInt();
				
				System.out.println("Please enter student 4's first name");
				s4.firstName = keyboard.nextLine();
				
				System.out.println("Please enter student 4's last name");
				s4.lastName = keyboard.nextLine();
				
				System.out.println("Please enter student 5's ID");
				s5.studentID = keyboard.nextInt();
				
				System.out.println("Please enter student 5's first name");
				s5.firstName = keyboard.nextLine();
				
				System.out.println("Please enter student 5's last name");
				s5.lastName = keyboard.nextLine();
				
				System.out.print("You have entered: ");
				System.out.println(s1.toString());
				System.out.println(s2.toString());
				System.out.println(s3.toString());
				System.out.println(s4.toString());
				System.out.println(s5.toString());
			}
			else if (response.equalsIgnoreCase("g")) {
				System.out.println("enter the score");
				score = keyboard.nextInt();
				
				switch (score)
				{
				case 100:
				case 99:
				case 98:
				case 97:
				case 96:
				case 95:
				case 94:
				case 93:
				case 92:
				case 91:
				case 90:
					grade = 'A';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
				break;
				case 89:
				case 88: 
				case 87:
				case 86:
				case 85:
				case 84:
				case 83:
				case 82:
				case 81:
				case 80:
					grade = 'B';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
					break;
				case 79:
				case 78:
				case 77:
				case 76:
				case 75:
				case 74:
				case 73:
				case 72:
				case 71:
				case 70:
					grade = 'C';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
					break;
				case 69:
				case 68:
				case 67:
				case 66:
				case 65:
				case 64:
				case 63:
				case 62:
				case 61:
				case 60:
					grade = 'D';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
					break;
				case 59:
				case 58:
				case 57:
				case 56:
				case 55:
				case 54:
				case 53:
				case 52:
				case 51:
				case 50:
				case 49:
				case 48:
				case 47:
				case 46:
				case 45:
				case 44:
				case 43:
				case 42:
				case 41:
				case 40:
				case 39:
				case 38:
				case 37:
				case 36:
				case 35:
				case 34:
				case 33:
				case 32:
				case 31:
				case 30:
				case 29:
				case 28:
				case 27:
				case 26:
				case 25:
				case 24:
				case 23:
				case 22:
				case 21:
				case 20:
				case 19:
				case 18:
				case 17:
				case 16:
				case 15:
				case 14:
				case 13:
				case 12:
				case 11:
				case 10:
				case 9:
				case 8:
				case 7:
				case 6:
				case 5:
				case 4:
				case 3:
				case 2:
				case 1:
				case 0:
					grade = 'F';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
					break;
				default:
					System.out.println("You have entered a non-valid score.");
					break;
				}
			}
			else if (response.equalsIgnoreCase("q")) {
					exit = true;
			}
		} System.out.println("Goodbye");
	}
} 

Multiple issues, see below:

  1. firstName, lastName and studentID need to be private in the student class. You have the getters and setters to deal with them, don’t let anything directly access them except for your getters and setters.
  2. Don’t create temp as a global variable within your class. It is a waste of memory. Create and initialize it within your overloaded toString() method. Or, better yet, just return the value. Don’t store it in a variable at all.
  3. Don’t directly access the variables in the toString() method. Use your getters.
    Example:


public String toString()
{
	return this.getStudentID() + ": " + this.getLastName() + ", " + this.getFirstName();
}


  1. In your main method, don’t access the variables directly to set the value (which won’t be allowed anyway once they are properly given private visibility) When you set the first name on the class, use

s1.setFirstName(keyboard.nextLine());

and do the same for all of the other object properties.
5) If you aren’t going to use a constructor in the Student class then at least initialize the strings. Currently if you create a student object and call “student.getFirstName()” before setting any of the values you’ll get a Null Reference Exception and the program will exit.
6) Don’t create your Student objects until the user indicates an “n”. Otherwise you are setting aside memory for them and, potentially, never using it. (Not to mention causing issues down the road if you ever allow the user to create multiple groups of students) That goes for all of your other variables as well. They are only used in specific circumstances. No reason to give them resources until you need to.
7) Switch statements are not for input validation. You can accomplish the same thing as your score switch statement with a one line if statement and then a 5 line switch (or if) statement as opposed to the current 100 line switch statement.