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

Thread: Stumped?

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Stumped?

    Here's my code:

    import java.util.Scanner;
    public class Main
    {
            public static void main(String[] args)
            {
                int entry;
                Scanner keyBoard = new Scanner(System.in);
                System.out.print("Enter a number which you wish to square:");
                entry = keyBoard.nextInt();
     
                System.out.println("The number chosen is " + entry);
                predictSquare(entry);
            }
     
         public static int predictSquare(int entry)
       {        
                int newAmount;
                newAmount = entry * entry;
                return newAmount;
                System.out.println("Cubed: " + newAmount);
     
       }

    I'm getting a 'missing return statement' error on the predictSquare(). I have a return statement in there? What is going on here? I've been banging my head against a wall for a while now.
    Last edited by KevinGreen; October 2nd, 2009 at 10:41 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Stumped?

    Simple, you have un-reachable code. The System.out.println("Cubed: " + newAmount); can never be reached because your method has already returned. Change your program to this:

    import java.util.Scanner;
    public class Main
    {
            public static void main(String[] args)
            {
                int entry;
                Scanner keyBoard = new Scanner(System.in);
                System.out.print("Enter a number which you wish to square:");
                entry = keyBoard.nextInt();
     
                System.out.println("The number chosen is " + entry);
                System.out.println("Squared: " + predictSquare(entry));
            }
     
         public static int predictSquare(int entry)
       {        
                int newAmount;
                newAmount = entry * entry;
                return newAmount;
       }

    I'm assuming you meant to say "squared" instead of "cubed"