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.
Code :
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
Re: Need help...Missing Return Statement
Hello 03EVOAWD.
You are getting this error because of this method:
Code :
public static boolean isPrime(int num) {
}
You need to return a result of type boolean.
For example:
Code :
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.
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
Re: Need help...Missing Return Statement
Ummmm try adding:
Code :
public static void main(String[] args){
}
You will need to call your boolean method from within this main method.
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.
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
Code :
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
Code :
public class Tester{
public static void main(String[] args){
PrimeNumber method = new PrimeNumber();
}//End method main
}//End Class Tester
Re: Need help...Missing Return Statement
What are you expecting to happen?
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
Re: Need help...Missing Return Statement
Quote:
Originally Posted by
03EVOAWD
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
Code :
System.out.println("Enter a value: ");
Scanner input = new Scanner(System.in);
int Odd = input.nextInt();
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
Code :
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++;
}
}
}
Re: Need help...Missing Return Statement
Try this. Only problem is it shows each prime number one by one..
Code :
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++;
}
}
}
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.
Code :
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]
}
}
Re: Need help...Missing Return Statement
Your lucky i'm bored today ;)
Try this:
Code :
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);
}
}
Re: Need help...Missing Return Statement
lol. JavaPF you make me laugh :)