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

Thread: Prim numbers

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Prim numbers

    I got a task to write a java program which can solve some mathematical actions. Every action has to be in separate methods. My problem is with a method which should sort prime numbers between 0 and a given number. The code works without the method but I have to implement into a method anyway and that's the problem. I tried many different ways with array etc but nothing works. I guess it's a very simple and easy task for many of you but I'm totally beginner so I would like to ask for you help. Thanks!

    public class prim4 {

    public static void main (String[] args) {

    int prim=50;
    int j;
    boolean control;

    for (j=1; j<=prim; j++)
    {
    control=primtal(j);

    if (control=false)
    {
    break;
    }
    if (control=true)
    {
    System.out.println(j + " ");
    }
    }
    }

    public static boolean primtal (int j)
    {
    int i;
    boolean result;
    for (i=2; i < j ;i++ ){
    int temp = j%i;
    if (temp==0)
    {
    result=false;
    }
    }
    if(i == j){
    {
    result=true;
    }
    return result;
    }
    }

    }


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    152
    Thanks
    0
    Thanked 22 Times in 22 Posts

    Default Re: Prim numbers

    It always helps to wrap your code in code tags: [ code][/code]
    Also, this part isn't done correctly.
    if (control=false)
    {
         break;
    }
    if (control=true)
    {
         System.out.println(j + " ");
    }

    You are assigning true or false to the control variable instead of checking for its value.

    Also, just a little helpful bit of advice: when checking for booleans, you don't need to check if they are equal to true or false as you did. An easier, nicer way of writing it would look like this.

    if (control) {
        //This will execute if control is true.
    }
     
    if (!control) {
        //This will execute if control is false.
    }

  3. The Following User Says Thank You to Parranoia For This Useful Post:

    krillov (May 31st, 2012)

  4. #3
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Prim numbers

    Thank you! I was trying as you said but it still writes an error message like: ...\prim4.java:42: error: missing return statement. I canīt find out what the problem is.

    public class prim4 {
     
    public static void main (String[] args) {
     
    int prim=50;
    int j;
    boolean control;
     
    for (j=1; j<=prim; j++)
    {
    control=primtal(j);
     
    if (!control)
    	{
    	break;
    	}
     
    if (control)
    	{
    	System.out.println(j + " ");
    	}
    }
    }
     
    public static boolean primtal (int j)
    {
      int i;
      boolean result;
      for (i=2; i < j ;i++ ){
      int temp = j%i;
    	if (temp==0)
    	{
     	result=false;
    	}
      }
      if(i == j){
    	{
    	result=true;
    	}
    return  result;
    }
    }
    }

  5. #4
    Member
    Join Date
    Apr 2012
    Posts
    152
    Thanks
    0
    Thanked 22 Times in 22 Posts

    Default Re: Prim numbers

    In your primtal method, the second if statement has an extra set of braces.

  6. The Following User Says Thank You to Parranoia For This Useful Post:

    krillov (May 31st, 2012)

  7. #5
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Prim numbers

    Thank you. I haven't seen it. I tried again but when i fix a fail there will be an other. It is now: "prim4.java:42: error: variable result might not have been initialized return result; " Do you think this solution/algorithm could work or should I find out an other one?

  8. #6
    Member
    Join Date
    Apr 2012
    Posts
    152
    Thanks
    0
    Thanked 22 Times in 22 Posts

    Default Re: Prim numbers

    That error just means that you never initialized "result". Set it to true or false to begin with.

  9. The Following User Says Thank You to Parranoia For This Useful Post:

    krillov (May 31st, 2012)

  10. #7
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Prim numbers

    Thanks again! Process completed now and it writes out numbers or not. It depends on if I set "result" to true or false. So I realized that my method doesn't do anything at all just sending back the "result" I set. I tried to change boolean to int instead and send back the number or 0 from the method but it doesn't work either. I guess the for-loop in the beginning of the program is also bad.
    Last edited by krillov; May 31st, 2012 at 06:06 PM.

  11. #8
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Prim numbers

    I fixed it.

    public class prim7 {
     
    public static void main (String[] args) {
     
    int prim=50;
    int j;
    boolean control;
     
    for (j=2; j<=prim; j++){
    control=primtal(j);
     
    if (control)
    	{
    	System.out.print(j + " ");
    	}
    }
    }
     
    static boolean primtal (int j)
    {
        for(int i=2;i<j;i++) {
            if(j%i==0)
                return false;
        }
        return true;
    }
     
    }

Similar Threads

  1. Concatenation of numbers
    By Ademola in forum Member Introductions
    Replies: 2
    Last Post: November 9th, 2011, 08:50 AM
  2. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  3. Binary Numbers
    By Bido in forum Java Theory & Questions
    Replies: 1
    Last Post: January 6th, 2011, 02:54 PM
  4. Adding Big Numbers
    By hoiberg in forum Java Theory & Questions
    Replies: 5
    Last Post: December 16th, 2010, 07:44 AM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM