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: Help me get started

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me get started

    Hello guys below is the program that i need to create. I know how to do the prime number but got stuck in displaying a error message.
    Plz help




    A prime number is a positive integer that is evenly divisible only by 1 and itself. Thus, e.g. 2,3,5,7,11,13,17,19 are all prime numbers. A pair of numbers x and y are twin primes if x and y are prime and |x-y| = 2. In this assignment you are to write a program that generates the twin primes between 3 and some number entered by the user. Thus, your code should prompt the user for a positive integer greater than or equal to 3. If the user enters something that is not a number or not a positive integer greater than or equal to 3, then the user should be shown an error message and should be again prompted to enter a positive integer greater than or equal to 3. If, after 3 tries, the user fails to enter a positive integer greater than or equal to 3, your code should display an error message and exit. Your code should then report the twin primes between 3 and the number entered by the user. It should also report the number of twin primes between 3 and the number entered by the user. The following represents a possible run of your code:

    Please enter a positive integer greater than or equal to 3
    20
    <3,5> <5,7> <11,13> <17,19> are twin primes between 3 and 20.
    There are 4 twin prime pairs between 3 and 20.

    In order to get your program to exit when the user fails to enter a correct value after 3 tries, use the follow piece of code:

    System.exit(1);

    At a minimum, you will need to have two methods - a main() method and an isPrime() method that has the following signature:

    public static boolean isPrime(int num)


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Help me get started

    Please help us help you by telling/showing us: What have you tried, and how is it not working? What specifically confuses you?

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me get started

    package Twinprime;

    import java.util.Scanner;
    public class primenumber {
    public static void main (String args []){

    String primeNo = "";
    int a;
    int j = 0;
    int LastPrime =1;
    Scanner in = new Scanner(System.in);
    System.out.println( "Enter first integer: " ); // prompt
    a=in.nextInt(); // read first number from user

    for (int i = 1; i < a;i++) {
    for (j = 2; j < i; j++) {
    if (i % j == 0) {
    break;
    }
    }
    if (i == j) {
    primeNo += i + " ";
    if ((i - LastPrime) == 2) {
    System.out.print("("+(i-2)+","+i+")");
    }
    LastPrime = i;
    }
    }

    }
    }


    According to question I can get twin prime...but can not figure it out where to put boolean...
    any help will be appreciated

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Help me get started

    It seems that you need to implement the boolean isPrime(int number) method which I don't see in your code attempt above. Why not give a go at making this method? Also, since you're new here you don't know how to use code tags yet, which if used help your posted code retain its formatting and be readable. Please place [code][/code] tags above and below your code blocks that you post here. Note that the top tag is different from the bottom one.

Similar Threads

  1. Can anyone help get me started?
    By linkazoid in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 14th, 2012, 04:02 PM
  2. Getting Started
    By Visual3volution in forum Java Theory & Questions
    Replies: 1
    Last Post: August 15th, 2011, 05:49 PM
  3. help to get started
    By novice_in_java in forum Member Introductions
    Replies: 5
    Last Post: March 10th, 2011, 05:15 AM
  4. Just getting started!
    By frogfury in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 18th, 2010, 11:05 AM
  5. How do i get started?????
    By deepthought in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: June 11th, 2010, 01:43 PM