Results 1 to 15 of 15
  1. #1
    Deafsilver is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Having a problem with a program telling me that variables are not being used

    I have a program with two classes and as far as I can tell I have everything correct, but obviously I don't. In the main method of StudentMethods it is telling me that my variables are not being used. I have no idea why it is telling me this. Can someone please tell me what is going on here?
    Java Code:
    public class Student {
    	String name;
    	double gpa;
    	
    	public Student(String n, double g){
    		name = n;
    		gpa = g;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	
    	public double getGpa() {
    		return gpa;
    	}
    	
    	public void setName(String n) {
    		name = n;
    	}
    	
    	public void setGpa(double g) {
    		gpa = g;
    	}
    	
    	public boolean equals(Student s) {
    		if (s.name == name && s.getGpa() == gpa) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    }
    
    
    
    
    
    
    public class StudentMethods {
    	
    	public static double getStudentWithHigherGpa(double s1, double s2) {	
    		if (s1 > s2) {
    			return s1;
    		} else {
    			return s2;
    		}
    	}
    	
    	public static void swapGPAs(double s1, double s2){
    		double temp = s1;
    		s1 = s2;
    		s2 = temp;
    		System.out.println(s1);
    		System.out.println(s2);
    	}
    	
    	public boolean testGetStudentWithHigherGpa(double s1, double s2, double expectedResult) {
    		double result = getStudentWithHigherGpa(s1, s2);
    		if (equals(result == expectedResult)) {
    			System.out.println("The higher GPA is " + s2);
    			return true;
    		} else {
    			System.out.println("The higher GPA fails.");
    			return false;
    		}
    	}
    	
    	public static void main(String[] args) {
    		Student s1 = new Student("Jim", 3.4);
    		Student s2 = new Student("Jane", 3.6);
    		Student expectedResult = new Student("Jane", 3.6);
    	}
    }
    Last edited by Norm; 09-07-2011 at 12:16 AM. Reason: added cod tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Having a problem with a program telling me that variables are not being used

    main method of StudentMethods it is telling me that my variables are not being used
    If that is some compiler error message, please copy the full text of the message and post it here.
    If not from the compiler, please explain what program is giving the message.

  3. #3
    Deafsilver is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Having a problem with a program telling me that variables are not being used

    In Eclipse under StudentMethods, it has a yellow exclamation point saying "The value of local variable s1 is not used." It says the same thing for s2 and expectedResult as well.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Having a problem with a program telling me that variables are not being used

    I guess your IDE is trying to be helpful. Not using a variable is legal. Using a variable is not required in a program.
    Ignore the messages if the variables are not needed.

    What does it say about the args variable? You are not using it either.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Having a problem with a program telling me that variables are not being used

    Yellow means it is a warning. You code will still compile and run if it contains warnings.

    As Norm says you can ignore it if you like but the question remains why have you declared a variable if you do not intend on using it?

  6. #6
    Deafsilver is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Having a problem with a program telling me that variables are not being used

    I know that it isn't required to use a variable, but I am trying to use them and they aren't being used. I have a couple of lines in StudentMethods that try to print out s1 and s2, but nothing is printed.

    I don't know about the args variable. I'm still very new to java and I'm not exactly sure what the args variable does. I have always used the args variable in my main methods. :/

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Having a problem with a program telling me that variables are not being used

    Quote Originally Posted by Deafsilver View Post
    but nothing is printed.
    As far as I can see is you declare 3 variables, create 3 objects and nothing else. If you don't call any methods how do you expect anything to happen?

  8. #8
    Deafsilver is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Having a problem with a program telling me that variables are not being used

    I tried printing out s1 and s2 in the swapGPAs method in StudentMethods and nothing happens. Also, in the testStudentWithHigherGpa method, I tried to call the equals method from Student.java and it doesn't print anything out either.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Having a problem with a program telling me that variables are not being used

    I tried printing out s1 and s2 in the swapGPAs method
    Where do you call those methods? The java program starts your program executing in the main method. After that it is 100% up to you to call any methods you want called. If you don't call them they don't get called.
    The IDE should warn you about that just like it warned you about s1 and s2.

  10. #10
    Deafsilver is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Having a problem with a program telling me that variables are not being used

    Ok, now I see what you guys are saying. Sorry about that. I'm extremely new to all of this haha.

    I'm still quite curious as to why nothing is being printed out in my testStudentWithHigherGpa method though... I put in a System.out.println statement in there so I could know whether it is coming out true or false, but nothing is printing.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Having a problem with a program telling me that variables are not being used

    The computer does EXACTLY what you tell it to do. No more, no less.

  12. #12
    Deafsilver is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Having a problem with a program telling me that variables are not being used

    Yes, I know that it is going to do only what I tell it to do, which is exactly why I do not know why it isn't doing anything when I am trying to put in an if statement to print something if it is true or print something else if it is false.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Having a problem with a program telling me that variables are not being used

    Have you called the method?

  14. #14
    Deafsilver is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Having a problem with a program telling me that variables are not being used

    I believe that I called the equals method, unless I am calling it incorrectly.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Having a problem with a program telling me that variables are not being used

    :headdesk:

    We are talking about the testGetStudentWithHigherGpa method. If you never call that method the the code inside it will never get executed. Computers cannot read your mind (not yet).

Similar Threads

  1. Replies: 3
    Last Post: 06-14-2011, 09:35 PM
  2. compiler telling me to make a file for a public class
    By silverglade in forum New To Java
    Replies: 10
    Last Post: 04-26-2011, 07:47 AM
  3. problem with variables
    By javahead in forum New To Java
    Replies: 4
    Last Post: 12-09-2008, 05:55 PM
  4. Trouble with For loop and variables in a program
    By dablyz in forum New To Java
    Replies: 12
    Last Post: 05-06-2008, 04:25 AM
  5. Problem with variables in java
    By carl in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 07:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •