6 Replies - 732 Views - Last Post: 16 April 2010 - 06:50 PM Rate Topic: -----

#1 draftcopy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-February 10

Missing Return Statement

Posted 16 April 2010 - 06:09 PM

Hello...

I keep receiving a Missing Return Statement error for line 58, but I'm not sure what to do exactly. Could somebody please explain this to me? Thanks!

/*
 * GradeConverter.java from Chapter 7
 * Determines the letter grade that corresponds to a numeric grade.
 * Lawrenceville Press
 * June 10, 2005
 */
 
 import java.util.Scanner;
 
 /**
  * Display the letter grade that corresponds to the numeric
  *	grade entered by the user.
  */
  
 public class GradeConverter {

	/**
	 * Determines if a numeric entry is valid.
	 * pre: none
	 * post: true has been returned if minNum <= userNum <= maxNum;
	 * false has been returned otherwise
	 */
	 
	public static boolean isValidNumber(int userNum, int minNum, int maxNum) {
		if (minNum <= userNum && userNum <= maxNum) {
			return(true);
		} else {
			return(false);
		}
	}


	/**
	 * Determines the letter grade that corresponds to the numeric grade.
	 * pre: 0 <= numGrade <= 100
	 * post: The letter grade A+, B+, C+, D+, A, B, C, D, or F has been returned.
	 */
	public static String getLetterGrade(int numGrade) {  //start here from where class left off
		if (numGrade == 100) {
			return "A+";
	    } else if (numGrade == 89) {
			return "B+";
	    } else if (numGrade == 79) {
			return "C+";
	    } else if (numGrade == 69) {
			return "D+";
	}   if (numGrade < 60) {
			return("F");
		} else if (numGrade >= 60 && numGrade <= 69) {
			return("D");
		} else if (numGrade >= 70 && numGrade <= 79) {
			return("C");
		} else if (numGrade >= 80 && numGrade <= 89) {
			return("B");
		} else if (numGrade >= 90 && numGrade <= 99) {
			return("A");
		}
	}

	public static void main(String[] args) {
		final int FLAG = -1;
		final int minValue = 0;
		final int maxValue = 100;
		int numericGrade;
		String letterGrade;
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter a numeric grade (-1 to quit): ");
		numericGrade = input.nextInt();
		
		while (numericGrade != FLAG) {
			if (isValidNumber(numericGrade, minValue, maxValue)) {
				letterGrade = getLetterGrade(numericGrade);
				System.out.println("The grade " + numericGrade + " is a(n) " + letterGrade + ".");	
			} else {
				System.out.println("Grade entered is not valid.");
			}
			System.out.print("Enter a numeric grade (-1 to quit): ");
			numericGrade = input.nextInt();
		}
	}
}


This post has been edited by PsychoCoder: 16 April 2010 - 06:17 PM
Reason for edit:: Code tags fixed :)


Is This A Good Question/Topic? 0
  • +

Replies To: Missing Return Statement

#2 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Re: Missing Return Statement

Posted 16 April 2010 - 06:14 PM

It simple. If none of the conditions in
public static String getLetterGrade(int numGrade)
method are fulfilled, this method will not return anything - but it has to!
You should provide 'else' statement with default return value ;)
Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Missing Return Statement

Posted 16 April 2010 - 06:23 PM

Just to try and put it simpler. Everything in your getLetterGrade method is enclosed in an if or else if statement. These all return something. You can look at these as inner returns. However, if none of them conditions are met, you need an outer return. This is why you should finish with a final else, to say, well, if none of the previous statements got invoked, return this instead.

Hope thats simpler :rolleyes2:
Was This Post Helpful? 0
  • +
  • -

#4 draftcopy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-February 10

Re: Missing Return Statement

Posted 16 April 2010 - 06:26 PM

What exactly is a default return value. I think my instructor left out some important information in our last class. :|
Was This Post Helpful? 0
  • +
  • -

#5 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Re: Missing Return Statement

Posted 16 April 2010 - 06:28 PM

I was not enough clear, sorry :) Your method HAS TO return some value, and as nick2price said, it will not if none of the conditions in 'if' and 'else if' statements are met - so it won't work ;)

This post has been edited by k0b13r: 16 April 2010 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Missing Return Statement

Posted 16 April 2010 - 06:35 PM

Oh, you were clear enough to me, but just wanted to reinforce the exact reason as to why the error occurs.

@OP - Its your assignment, so the default value could be anything. As a logical guess, something like this would problably be ok.
else{
return ("Grade calculation malfunction - try again");
}

This post has been edited by nick2price: 16 April 2010 - 06:36 PM

Was This Post Helpful? 0
  • +
  • -

#7 draftcopy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-February 10

Re: Missing Return Statement

Posted 16 April 2010 - 06:50 PM

Oh, I see! Christ... I'm just not that good at this stuff. Hey, I appreciate your time. Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1