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

Thread: Something is wrong? Please help.

  1. #1
    Junior Member
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Something is wrong? Please help.

    What did I do wrong with this code? I thought it compiled great.

    import java.util.Scanner;
     
    public class mycode_Sieve
    {
        private boolean primes[] = new boolean [50001];
     
        private int upper = 50000;
     
        private int lower = 1;
     
        public void processSieve()
        {
            primes[0] = false;
            primes[1] = false;
            for (int a = 2; a < 50000; a++)
            {
                primes[a] = true;
            }
     
            for (int a = 2; a <= 223; a++)
            {
                if (primes[a])
                {
            for (int b = a; b <= 223; b++)
                    {
                        int c = (a * b);
                        primes[c] = false;
                    }
                }
            }
        }
     
        public void getBoundaries()
        {
            System.out.println ("Please enter a lower boundary and an upper boundary and I will print sexy prime pairs between those boundaries.");
     
            Scanner i = new Scanner (System.in);
     
            System.out.print("Please enter the lower boundary (between 1 and 50000): ");
            lower = i.nextInt();
            while ((lower <= 0) || (lower > 50000))
            {
                System.out.print("Please enter the lower boundary (between 1 and 50000): ");
                lower = i.nextInt();
            }
     
            System.out.print("Please enter the upper boundary (between 1 and 50000): ");
     
                                        upper = i.nextInt();
            while ((upper <= 0) || (upper > 50000))
            {
                System.out.print("Please enter the upper boundary (between 1 and 50000): ");
                upper = i.nextInt();
            }
     
            while (lower > upper)
            {
                System.out.println ("Your upper boundary cannot be smaller than your lower boundary.");
     
                System.out.print("Please enter the lower boundary (between 1 and 50000): ");
                lower = i.nextInt();
                while ((lower <= 0) || (lower > 50000))
      System.out.print("Please enter the lower boundary (between 1 and 50000): ");
                    lower = i.nextInt();
                }
     
                             System.out.print("Please enter the upper boundary (between 1 and 50000): ");
                upper = i.nextInt();
                while ((upper <= 0) || (upper > 50000))
                {
                    System.out.print("Please enter the upper boundary (between 1 and 50000): ");
                    upper = i.nextInt();
                }
     
            }
     
        }
     
        public void showPrimes()
        {
            System.out.printf("Here are all of the sexy prime pairs in the range %d to %d, one pair per line:\n", lower, upper);
     
            int counter = 0;
            for (int a = lower; a <= upper; a++)
            {
                if (primes[a] == true)
     
                {
                    int b = (a + 6);
                    if (b <= 50000)
                    {
                        if (primes[b] == true)
                        {
                            System.out.printf("%d and %d\n", a, b);
                            counter++;
                        }
                    }
                }
            }
     
            System.out.printf("There were %d sexy prime pairs displayed between %d and %d\n", counter, lower, upper);
        }
    }


  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: Something is wrong? Please help.

    The problem is you had an extra curly bracket you didn't need. Please see the below code. I have highlighted the error..

    import java.util.Scanner;
     
    public class mycode_Sieve
    {
        private boolean primes[] = new boolean [50001];
     
        private int upper = 50000;
     
        private int lower = 1;
     
        public void processSieve()
        {
            primes[0] = false;
            primes[1] = false;
            for (int a = 2; a < 50000; a++)
            {
                primes[a] = true;
            }
     
            for (int a = 2; a <= 223; a++)
            {
                if (primes[a])
                {
            for (int b = a; b <= 223; b++)
                    {
                        int c = (a * b);
                        primes[c] = false;
                    }
                }
            }
        }
     
        public void getBoundaries()
        {
            System.out.println ("Please enter a lower boundary and an upper boundary and I will print sexy prime pairs between those boundaries.");
     
            Scanner i = new Scanner (System.in);
     
            System.out.print("Please enter the lower boundary (between 1 and 50000): ");
            lower = i.nextInt();
            while ((lower <= 0) || (lower > 50000))
            {
                System.out.print("Please enter the lower boundary (between 1 and 50000): ");
                lower = i.nextInt();
            }
     
            System.out.print("Please enter the upper boundary (between 1 and 50000): ");
     
                                        upper = i.nextInt();
            while ((upper <= 0) || (upper > 50000))
            {
                System.out.print("Please enter the upper boundary (between 1 and 50000): ");
                upper = i.nextInt();
            }
     
            while (lower > upper)
            {
                System.out.println ("Your upper boundary cannot be smaller than your lower boundary.");
     
                System.out.print("Please enter the lower boundary (between 1 and 50000): ");
                lower = i.nextInt();
                while ((lower <= 0) || (lower > 50000))
      System.out.print("Please enter the lower boundary (between 1 and 50000): ");
                    lower = i.nextInt();
                }
     
                             System.out.print("Please enter the upper boundary (between 1 and 50000): ");
                upper = i.nextInt();
                while ((upper <= 0) || (upper > 50000))
                {
                    System.out.print("Please enter the upper boundary (between 1 and 50000): ");
                    upper = i.nextInt();
                }
     
            }
     
    [B]//error here
    //    }
    [/B]
        public void showPrimes()
        {
            System.out.printf("Here are all of the sexy prime pairs in the range %d to %d, one pair per line:\n", lower, upper);
     
            int counter = 0;
            for (int a = lower; a <= upper; a++)
            {
                if (primes[a] == true)
     
                {
                    int b = (a + 6);
                    if (b <= 50000)
                    {
                        if (primes[b] == true)
                        {
                            System.out.printf("%d and %d\n", a, b);
                            counter++;
                        }
                    }
                }
            }
     
            System.out.printf("There were %d sexy prime pairs displayed between %d and %d\n", counter, lower, upper);
        }
    }

    This should compile fine now
    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
    Dec 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Something is wrong? Please help.

    So I dont need a constructor method as per UML spec?

  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: Something is wrong? Please help.

    Quote Originally Posted by DestinyChick1225 View Post
    So I dont need a constructor method as per UML spec?
    ?? All I have done is removed an extra bracket you added by mistake
    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.

Similar Threads

  1. not sure what im doing wrong with these if else statements
    By yrvd86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2010, 08:32 PM
  2. What's wrong?!
    By deeerek in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 22nd, 2010, 07:11 PM
  3. don't know what's wrong
    By james in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 15th, 2010, 07:37 PM
  4. Where am I going wrong? :)
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 18th, 2009, 12:03 AM
  5. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM