Reputation: 255
I know the problem exists on the forum, but I am really stuck. I would like to call my grade()
method into my display()
method to appear the grade. I don't know how to do...
Here is my grade()
method
public static void grade(int note){
String grade = "";
if (note <= 70){
grade = "Satisfaction";
}
else{
grade = "Great Satisfaction";
}
}
In my display()
method, I want to retrieve the grade for the student.
public static void display(String[] tabStudent, int[] tabNote, char code){
for(int i=0; i<tabStudent.length;i++){
if (code == 'e'){
if (tabNote[ i ] < 50){
System.out.println(tabStudent[i] + " " + tabNote[i] );
}
}
else if(code == 's'){
if (tabNote[ i ] > 50){
System.out.println(tabStudent[i] + " " + tabNote[i] + grade() );
}
}
}
}
My problem is this line below:
System.out.println(tabStudent[i] + " " + tabNote[i] + grade() );
Thank you for your help.
Upvotes: 0
Views: 118
Reputation: 311
tl;dr you're missing a return statement
Imagine you have a bunch of people around you who can either do a task (hopefully...) perfectly or can calculate some value (again, hopefully...) perfectly. When you tell one person, "go put coins into a vending machine", they go do it and walk back. They don't say anything, they just kind of...go. At most, you might get an "Ok" or a "HEY SOMETHING WENT WRONG" when they come back but that's the extent of their verbal capacity.
On the other hand, you have people who you ask "What are the 1st through 3rd letters of 'hello'?" And they will say, "'el'." They may go grab a calculator or a book to figure out the answer, but they can do it and will report the value back to you.
The former is an example of a void function, while the latter is what you get when you return from a function. When you have a function simply calculate values in a void function, it's equivalent to asking a friend, "What's 5+3?", them doing the calculation in their head and saying "done! :D". Not super helpful.
While there are ways to get around needing to return while not having to literally return (such as using global variables), they are heavily frowned up much like passing notes to relay information was in middle school by your teacher.
Upvotes: 3
Reputation: 79620
The first problem is that you do not have a method grade()
. Instead, you have defined grade(int note)
which expects an integer.
Even if you correct this problem and call grade
like
System.out.println(tabStudent[i] + " " + tabNote[i] + grade(80));
it won't work because you have declared grade
as
public static void grade(int note)
which should be
public static String grade(int note)
i.e. return type should be String
instead of void
as per your requirement.
Define it as
public static String grade(int note){
String grade = "";
if (note <= 70){
grade = "Satisfaction";
}
else{
grade = "Great Satisfaction";
}
return grade;
}
Upvotes: 2
Reputation: 116
I can see two issues in your code: 1. you should change return type of grade() method and return grade. 2. Your grade() method expect one parameter but you are not passing any while calling it. Try below code.
public static String grade(int note){
String grade = "";
if (note <= 70){
grade = "Satisfaction";
}
else{
grade = "Great Satisfaction";
}
return grade;
}
public static void display(String[] tabStudent, int[] tabNote, char code){
for(int i=0; i<tabStudent.length;i++){
if (code == 'e'){
if (tabNote[ i ] < 50){
System.out.println(tabStudent[i] + " " + tabNote[i] );
}
}
else if(code == 's'){
if (tabNote[ i ] > 50){
System.out.println(tabStudent[i] + " " + tabNote[i] + " " + grade(tabNote[i]) );
}
}
}
}
Upvotes: 2
Reputation: 1847
Well, it needs to actually return something:
public static String grade(int note){
String grade = "";
if (note <= 70) {
grade = "Satisfaction";
} else {
grade = "Great Satisfaction";
}
return grade;
}
Upvotes: 2