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

Thread: Please help with method return statement!

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Please help with method return statement!

    Im having trouble with my method return. I am new to using methods and cant seem to grasp the idea on the return part. I have to write a method that tells if a number is prime or not. This is what I have so far and it wont compile because it is saying "missing return statement } "... Please help!

    import javax.swing.JOptionPane;
     
    public  class IsPrimeMethod
    {
    	public  static  void    main(String []args)
    	{
    		String primeNum;
    		int number;
    		int i = 2;
     
    		primeNum =
    			JOptionPane.showInputDialog("Please enter a number: ");
    		number = Integer.parseInt(primeNum);
     
    		if(isPrime(number))
    		{
    			JOptionPane.showMessageDialog(null, "Your number is not prime");
    		}
    		else
    		{
    			JOptionPane.showMessageDialog(null, "Your number is prime");
    		}
    	}
    	public static boolean isPrime(int number)
    	{
    		for(int i = 2; i < number; i++)
    		{
    		if(number % i == 0)
    			return true;
     
    		else
    			return false;
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,187
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Please help with method return statement!

    The compiler doesn't go through the logic to see if for loop will execute or not.
    It wants a return for the possible case where the for loop doesn't ever execute. What if number is 0?
    Bottom line: add a return statement at the end of the method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please help with method return statement!

    Not sure I follow you, Norm. So there should only be one return statement?

    --- Update ---

    Should there not be a for loop in the method?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,187
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Please help with method return statement!

    No, there can be more than one.
    The compiler wants a guaranteed return statement. The posted code can skip the for loop with some values of number. Add a return at the end of the method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please help with method return statement!

    So I need to just add a guaranteed return statement and it will be fine? My coding is okay?

    --- Update ---

    I keep trying to add another return statement and cant seem to figure it out. Can you show me how or at least show me an example?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,847
    My Mood
    Mellow
    Thanks
    178
    Thanked 630 Times in 617 Posts

    Default Re: Please help with method return statement!

    I keep trying to add another return statement . . .
    Show what you tried.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,187
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Please help with method return statement!

    show me an example
    An example:
    return true;
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please help with method return statement!

    Okay I got it to compile and run. However, now it always returns not prime. I think its because i set boolean notPrime = true; but, if I dont set it to anything it says that notPrime needs to be initialized. HELP lol
    import javax.swing.JOptionPane;
     
    public  class IsPrimeMethod
    {
    	public  static  void    main(String []args)
    	{
    		String primeNum;
    		int number;
     
    		primeNum =
    			JOptionPane.showInputDialog("Please enter a number: ");
    		number = Integer.parseInt(primeNum);
     
    		if(isPrime(number))
    		{
    			JOptionPane.showMessageDialog(null, "Your number is not prime");
    		}
    		else
    		{
    			JOptionPane.showMessageDialog(null, "Your number is prime");
    		}
    	}
    	public static boolean isPrime(int number)
    	{
    		boolean notPrime = true;
     
    		for(int i = 2; i < number / 2; i++)
    		{
    			if(number % i == 0)
    				notPrime = true;
    			else
    				return false;
    		}
    		return notPrime;
    	}
    }


    --- Update ---

    Never mind I had notPrime = true when it should have been false, duh! All fixed. What do you think of the final program?
    import javax.swing.JOptionPane;
     
    public  class IsPrimeMethod
    {
    	public  static  void    main(String []args)
    	{
    		String primeNum;
    		int number;
     
    		primeNum =
    			JOptionPane.showInputDialog("Please enter a number: ");
    		number = Integer.parseInt(primeNum);
     
    		if(isPrime(number))
    		{
    			JOptionPane.showMessageDialog(null, "Your number is not prime");
    		}
    		else
    		{
    			JOptionPane.showMessageDialog(null, "Your number is prime");
    		}
    	}
    	public static boolean isPrime(int number)
    	{
    		boolean notPrime = false;
     
    		for(int i = 2; i <= number / 2; i++)
    		{
    			if(number % i == 0)
    				notPrime = true;
    			else
    				return false;
    		}
    		return notPrime;
    	}
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,187
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Please help with method return statement!

    When not prime is found, the code should stop looping.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please help with method return statement!

    Is my updated version OK? It seems to do what I need it to do...

Similar Threads

  1. can i return two values from a return method?
    By tonu in forum Object Oriented Programming
    Replies: 4
    Last Post: January 1st, 2014, 10:02 AM
  2. Missing return statement
    By beerye28 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 23rd, 2013, 05:48 PM
  3. [SOLVED] Return statement.
    By Melawe in forum Java Theory & Questions
    Replies: 18
    Last Post: June 20th, 2011, 11:54 AM
  4. Missing return statement
    By mrroberts2u in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 06:11 AM
  5. Prime number generator program missing return statement
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 10th, 2009, 09:17 AM