Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Return letter grade to main method

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Return letter grade to main method

    Main Method:

    System.out.println("Student Name\tHigh Score\tLow Score\tAverage\t\tGrade");
    for (int p = 0; p < Student.length; p++) {
    System.out.println(Student[p] + "\t\t" + highGrade(studentGrades[p])
    + "\t\t" + lowGrade(studentGrades[p]) + "\t\t"
    + averageGrade(studentGrades[p]) + "\t\t" + letterGrade(p));

    __________________________________________________ __________________________________________________ __________________

    public static String letterGrade(double arrayIn[]) {


    double avgGrade = 0.0;
    for (int i = 0; i < arrayIn.length; i++) {
    avgGrade += arrayIn[i];

    double newavg = avgGrade / arrayIn.length;

    if (newavg >= 94) {
    return "A";
    } else if (newavg >= 90) {
    return "A-";
    } else if (newavg >= 87) {
    return "B+";
    } else if (newavg >= 84) {
    return "B";
    } else if (newavg >= 80) {
    return "B-";
    } else {
    return "F";
    }
    }
    }
    __________________________________________________ ______________________


    It tells me I am missing me return statement. So Im not sure how to make my letter grades return from me if else statements. I am a beginner. Also thanks for your help!

    Jason


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Return letter grade to main method

    The warning is because the compiler does not see a guarantee that one of the returns will always succeed in the for loop. Assuming the loop completes without returning, the compiler wants to know what to return at that point. Usually this is the location to place your "default" value, or the "catch all". In this case "F" seems to be your "catch all". It has to be returned outside the loop.

    Hoping you understand what is happening above, now it is time to say, A method can return only one value each time it is called. So the idea of returning inside a loop will end after the first return is found.

    Edit:
    please see announcements for the use of code tags and other information

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Return letter grade to main method

    Thank you! Sometimes it just takes someone else to look at it in order to tip you off in the right direction. I appreciate it!

    My Solution:

    Main:
    System.out.println("Student Name\tHigh Score\tLow Score\tAverage\t\tGrade");
    for (int p = 0; p < Student.length; p++) {
    System.out.println(Student[p] + "\t\t" + highGrade(studentGrades[p])
    + "\t\t" + lowGrade(studentGrades[p]) + "\t\t"
    + averageGrade(studentGrades[p]) + "\t\t" + letterGrade(studentGrades[p]));
    }


    __________________________________________________ ______________________
    public static String letterGrade(double arrayIn[]) {

    double avgGrade = 0.0;
    for (int i = 0; i < arrayIn.length; i++) {
    avgGrade += arrayIn[i];

    double newavg = avgGrade / arrayIn.length;

    if (newavg >= 94) {
    return "A";
    } else if (newavg >= 90) {
    return "A-";
    } else if (newavg >= 87) {
    return "B+";
    } else if (newavg >= 84) {
    return "B";
    } else if (newavg >= 80) {
    return "B-";
    } else if (newavg >= 77) {
    return "C+";
    } else if (newavg >= 74) {
    return "C";
    } else if (newavg >= 70) {
    return "C-";
    } else if (newavg >= 67) {
    return "D+";
    } else if (newavg >= 64) {
    return "D";
    } else if (newavg >= 60) {
    return "D-";
    }

    else {

    }
    }
    return "F";
    }

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Return letter grade to main method

    Did you read the part about returning inside a for loop?

Similar Threads

  1. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  2. Method return value(s)
    By mwr76 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2011, 02:38 PM
  3. Replies: 5
    Last Post: August 11th, 2011, 12:39 PM
  4. [SOLVED] why does this code print 0.0 for the value and "F" as the letter grade?
    By etidd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 12th, 2010, 10:38 PM
  5. help with the logic on this letter grade program.
    By etidd in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 09:14 PM