Results 1 to 15 of 15
- 09-07-2011, 12:07 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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
- 09-07-2011, 12:16 AM #2
Re: Having a problem with a program telling me that variables are not being used
If that is some compiler error message, please copy the full text of the message and post it here.main method of StudentMethods it is telling me that my variables are not being used
If not from the compiler, please explain what program is giving the message.
- 09-07-2011, 12:21 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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.
- 09-07-2011, 12:30 AM #4
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.
- 09-07-2011, 12:34 AM #5
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?
- 09-07-2011, 12:34 AM #6
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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. :/
- 09-07-2011, 12:36 AM #7
- 09-07-2011, 12:45 AM #8
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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.
- 09-07-2011, 12:48 AM #9
Re: Having a problem with a program telling me that variables are not being used
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.I tried printing out s1 and s2 in the swapGPAs method
The IDE should warn you about that just like it warned you about s1 and s2.
- 09-07-2011, 01:04 AM #10
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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.
- 09-07-2011, 01:06 AM #11
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.
- 09-07-2011, 01:11 AM #12
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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.
- 09-07-2011, 01:12 AM #13
Re: Having a problem with a program telling me that variables are not being used
Have you called the method?
- 09-07-2011, 01:13 AM #14
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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.
- 09-07-2011, 01:16 AM #15
Similar Threads
-
hi guys , please help me in telling about how Objects are passed by reference. :(
By funkygarzon in forum New To JavaReplies: 3Last Post: 06-14-2011, 09:35 PM -
compiler telling me to make a file for a public class
By silverglade in forum New To JavaReplies: 10Last Post: 04-26-2011, 07:47 AM -
problem with variables
By javahead in forum New To JavaReplies: 4Last Post: 12-09-2008, 05:55 PM -
Trouble with For loop and variables in a program
By dablyz in forum New To JavaReplies: 12Last Post: 05-06-2008, 04:25 AM -
Problem with variables in java
By carl in forum AWT / SwingReplies: 1Last Post: 07-31-2007, 07:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks