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 3 of 3

Thread: method must return a result of type int

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default method must return a result of type int

    Hi, I am getting an compiler error for this method below "This method must return a result of the type int" to my mind it does return an int; it takes the next int entered and if it is valid returns it? But obviously I have made an error.

     
     public int score()
        {
    	System.out.println("Enter score");
    	score= sc.nextInt();
    	while (score < 0 || score > 100)
        {
           System.out.println("Invalid number !");
           System.out.println ("Enter an average in range 0 to 100");
           score=sc.nextInt();
        return score;
        }
    	}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: method must return a result of type int

    The compiler does not know if the loop will execute or not. It was a return statement for the case that the loop does not execute.

    Note: Having an unconditional return statement inside of a loop means the loop will never iterate (go around a second time).
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: method must return a result of type int

    Thank you for your quick response, I have moved the return statement outside of the loop and is now compilling. I am at the very start of my Java journey so very much appreciate the support

Similar Threads

  1. this method must return an int type /// + if
    By imago23 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 9th, 2014, 10:06 PM
  2. return type of iterator() method
    By msnishan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2014, 08:16 AM
  3. Replies: 4
    Last Post: February 26th, 2012, 05:36 PM
  4. Ending a method that has no return type
    By Blehs in forum Java Theory & Questions
    Replies: 3
    Last Post: August 12th, 2011, 01:56 AM
  5. error: This method must return a result of type int
    By J05HYYY in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2011, 05:26 PM