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

Thread: Prime number generator program missing return statement

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Prime number generator program missing return statement

    Below is what I am trying to get to but I keep getting a compiler error of a missing return statement that looks like this:

    PrimeNumber.java:85: missing return statement
    }//End Method


    I dont understand the error and cant find an explination that I like to make me get it anywhere.

    Declare a method to determine whether an integer is a prime number
    Use the following method declarations: public static Boolean isPrime (int num)
    An integer greater than 1 is a prime number if its only divisor is 1 or itself.
    For example, isPrime (11) returns true, and isPrime (9) returns false.

    Us the isPrime method to find the first thousand prime numbers and display every ten prime numbers in a row, as follows:

    2 3 5 7 11 13 17 19 23 29
    31 37 41 43 47 53 59 61 67 71
    73 79 83 89 97 … …

    Important Notes: The input and output must use JOptionPane dialog and display boxes.


     
    import javax.swing.*;
    import java.util.Scanner;
     
    public class PrimeNumber{
     
        public static boolean isPrime(int num){
     
            Scanner input = new Scanner(System.in);
     
            int Odd = input.nextInt();
            int Count = 0;
            int Prime = 1;
            int Num1;
     
        String checker=
            JOptionPane.showInputDialog("Input your Values:");
            Integer Number1 = Integer.parseInt(checker);
     
        do{
     
            Num1 = (Prime + 2) / 2;
     
     
            boolean isPrime = true;
            Count++;
     
            if(Num1 != 1){
            isPrime = false;
            break;}
    }
     
         while(Count <= 1000);    
     
        String message2=
            String.format("All Prime Numbers: %d\n", Odd);    
            JOptionPane.showMessageDialog(null, message2);
     
        }//End method 
     
    }//End Class PrimeNumber


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need help...Missing Return Statement

    Hello 03EVOAWD.

    You are getting this error because of this method:

    public static boolean isPrime(int num) {
     
    }

    You need to return a result of type boolean.

    For example:

     
    import javax.swing.*;
    import java.util.Scanner;
     
    public class PrimeNumber{
     
        public static boolean isPrime(int num){
     
            Scanner input = new Scanner(System.in);
     
            int Odd = input.nextInt();
            int Count = 0;
            int Prime = 1;
            int Num1;
     
        String checker=
            JOptionPane.showInputDialog("Input your Values:");
            Integer Number1 = Integer.parseInt(checker);
     
        do{
     
            Num1 = (Prime + 2) / 2;
     
     
            boolean isPrime = true;
            Count++;
     
            if(Num1 != 1){
            isPrime = false;
            break;}
    }
     
         while(Count <= 1000);    
     
        String message2=
            String.format("All Prime Numbers: %d\n", Odd);    
            JOptionPane.showMessageDialog(null, message2);
            [B]return false;[/B]
     
        }//End method 
     
    }//End Class PrimeNumber

    I'm not sure if this will work as expected but it will stop that error.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help...Missing Return Statement

    Thanks it did stop that error and the program compiles now.....

    But alas another error:
    Exception in thread "main" java.lang.NoSuchMethodError:main

    Gotta love it

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need help...Missing Return Statement

    Ummmm try adding:

    public static void main(String[] args){
     
    }

    You will need to call your boolean method from within this main method.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    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: Need help...Missing Return Statement

    Ooh... don't compute every prime seprately If you know what range you need prime numbers up to, use the Sieve of Eratosthenes, or if you're feeling bold the Sieve of Atkin.

  6. #6
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help...Missing Return Statement

    I have called the method in a seperate class and now there are no errors at all not in compiling or running but when I run the program nothing happens...

    updated program caller is located below

    import javax.swing.*;
    import java.util.Scanner;
     
    public class PrimeNumber{
     
    public static boolean isPrime(int num){
     
    		Scanner input = new Scanner(System.in);
     
    		int Odd = input.nextInt();
    		int Count = 0;
    		int Prime = 1;
    		int Num1;
     
    	String checker=
    		JOptionPane.showInputDialog("Input your values:");	
    		int number1 = Integer.parseInt(checker);
     
    	do{
     
    		Num1 = (Prime + 2) / 2;
     
     
    		boolean isPrime = true;
    		Count++;
     
    		if(Num1 != 1){
    		isPrime = false;
    		break;}
    }
     
     	while(Count <= 1000);
     
    	String message2=
    		String.format("Total number of Entries: %d\n", Odd);	
    		JOptionPane.showMessageDialog(null, message2);
    		return false;                   
     
     
    }//End method main
     
    }//End Class PrimeNumber

    Used to call the method

    public class Tester{
     
    public static void main(String[] args){
     
    PrimeNumber method = new PrimeNumber();
     
     
    }//End method main
     
    }//End Class Tester

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need help...Missing Return Statement

    What are you expecting to happen?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. #8
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help...Missing Return Statement

    find the first thousand prime numbers and display every ten prime numbers in a row, ----eventually

    Right now I just want it to ask me to input a value

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need help...Missing Return Statement

    Quote Originally Posted by 03EVOAWD View Post
    find the first thousand prime numbers and display every ten prime numbers in a row, ----eventually

    Right now I just want it to ask me to input a value

    System.out.println("Enter a value: ");
    Scanner input = new Scanner(System.in);
    int Odd = input.nextInt();
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  10. #10
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help...JOPTIONPANE GOTO VERY BOTTOM FOR UPDATED QUESTION

    Okay I made some progress......

    I decided to just get it working in the CMD prompt and then worry about JOptionPane later now I cant get it to print in JOptionPane like it does in the CMD Prompt.

    x x x x x x x x x x
    x x x x x x x x x x....10 numbers across all the way up to 1000

    Any Help is appreciated

    import javax.swing.JOptionPane;
     
    public class PrimeNumber
    {
        public static void main (String[] args)
            {
     
                final int Maximum_Primes = 1000;//Number of primes to display
     
                final int Primes_Per_Line = 10; //Display 10 per line
     
                int count = 0;//Count number of prime numbers
     
                int number = 2;
     
                System.out.println("The first 1000 prime numbers are \n");
     
                while (count < Maximum_Primes)
                {
                    boolean isPrime = true;
     
                    int temp = number/2; // had to declare new variable since you cannot do math in a for loop
     
                    for (int divisor = 2;divisor<=temp;divisor++) //you cannot declare divisor 3 times here and it             
     
                                                                  //cannot be split on multiple lines 
                    {                  
    		    if (number % divisor == 0){
                                    isPrime = false;
                                    break;
                            }
                    }
                    if (isPrime)
                    {
                        count++;
                        if (count % Primes_Per_Line != 0)//need to make sure that it will print when its not 
                                                                                                // equal to 0
                        {
     
    			    System.out.print(number + " ");
                        }
                        else
                                System.out.println(number + ".");
                    }
                    number++;
                }
            }
    }

  11. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need help...Missing Return Statement

    Try this. Only problem is it shows each prime number one by one..

    import javax.swing.JOptionPane;
     
    public class PrimeNumber
    {
        public static void main (String[] args)
            {
     
                final int Maximum_Primes = 1000;//Number of primes to display
     
                final int Primes_Per_Line = 10; //Display 10 per line
     
                int count = 0;//Count number of prime numbers
     
                int number = 2;
     
                System.out.println("The first 1000 prime numbers are \n");
     
                while (count < Maximum_Primes)
                {
                    boolean isPrime = true;
     
                    int temp = number/2; // had to declare new variable since you cannot do math in a for loop
     
                    for (int divisor = 2;divisor<=temp;divisor++) //you cannot declare divisor 3 times here and it             
     
                                                                  //cannot be split on multiple lines 
                    {                  
                if (number % divisor == 0){
                                    isPrime = false;
                                    break;
                            }
                    }
                    if (isPrime)
                    {
                        count++;
                        if (count % Primes_Per_Line != 0)//need to make sure that it will print when its not 
                                                                                                // equal to 0
                        {
     
                    System.out.print(number + " ");
                    [B]JOptionPane.showMessageDialog(null, number + " ");[/B]
                        }
                        else
                                System.out.println(number + ".");
                                //JOptionPane.showMessageDialog(null, number + " ");
                    }
                    number++;
                }
            }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  12. #12
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need help...Missing Return Statement

    I've used an ArrayList here to store the results and then display them with the JOptionPane.

    The JOptionPane is one massive box at the moment. You'll need to figure out how to add a new line after every 10 results or something.

    package guiclient;
     
    import javax.swing.JOptionPane;
    import java.util.ArrayList;
     
    public class PrimeNumber
    {
        public static void main (String[] args)
            {
     
                [B]ArrayList<String> al = new ArrayList<String>();[/B]
     
                final int Maximum_Primes = 1000;//Number of primes to display
     
                final int Primes_Per_Line = 10; //Display 10 per line
     
                int count = 0;//Count number of prime numbers
     
                int number = 2;
     
                System.out.println("The first 1000 prime numbers are \n");
     
                while (count < Maximum_Primes)
                {
                    boolean isPrime = true;
     
                    int temp = number/2; // had to declare new variable since you cannot do math in a for loop
     
                    for (int divisor = 2;divisor<=temp;divisor++) //you cannot declare divisor 3 times here and it             
     
                                                                  //cannot be split on multiple lines 
                    {                  
                if (number % divisor == 0){
                                    isPrime = false;
                                    break;
                            }
                    }
                    if (isPrime)
                    {
                        count++;
                        if (count % Primes_Per_Line != 0)//need to make sure that it will print when its not 
                                                                                                // equal to 0
                        {
     
                    System.out.print(number + " ");
                   [B] al.add(number + " ");[/B]
     
                        }
                        else
                                System.out.println(number + ".");
                               [B] al.add(number + " ");[/B]
                    }
                    number++;
                }
     
              [B]JOptionPane.showMessageDialog(null, al);[/B]
     
            }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  13. #13
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need help...Missing Return Statement

    Your lucky i'm bored today

    Try this:

    import javax.swing.JOptionPane;
    import java.util.ArrayList;
     
    public class PrimeNumber
    {
        public static void main (String[] args)
            {
     
                ArrayList<String> al = new ArrayList<String>();
                [B]int alCount = 0;[/B]
     
                final int Maximum_Primes = 100;//Number of primes to display
     
                final int Primes_Per_Line = 10; //Display 10 per line
     
                int count = 0;//Count number of prime numbers
     
                int number = 2;
     
                System.out.println("The first 1000 prime numbers are \n");
     
                while (count < Maximum_Primes)
                {
                    boolean isPrime = true;
     
                    int temp = number/2; // had to declare new variable since you cannot do math in a for loop
     
                    for (int divisor = 2;divisor<=temp;divisor++) //you cannot declare divisor 3 times here and it             
     
                                                                  //cannot be split on multiple lines 
                    {                  
                if (number % divisor == 0){
                                    isPrime = false;
                                    break;
                            }
                    }
                    if (isPrime)
                    {
                        count++;
                        if (count % Primes_Per_Line != 0)//need to make sure that it will print when its not 
                                                                                                // equal to 0
                        {
     
                    System.out.print(number + " ");
     
           [B]         alCount++;
     
                    if(alCount == 10){
                        al.add(number + "\n");
                        alCount = 0;
                    }else{
                    al.add(number + " ");
                    }[/B]
     
     
                        }
                        else
                                System.out.println(number + ".");
     
                    }
                    number++;
                }
     
              JOptionPane.showMessageDialog(null, al);
     
            }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  14. #14
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Need help...Missing Return Statement

    lol. JavaPF you make me laugh

Similar Threads

  1. If statement
    By Dave in forum Loops & Control Statements
    Replies: 2
    Last Post: August 27th, 2009, 10:11 AM
  2. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 12:00 PM
  3. Java error in password generator program
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 16th, 2009, 07:49 PM
  4. [SOLVED] Java program to generate 10 random integers and then sum computed
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 14th, 2009, 12:33 PM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM