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

Thread: please help me with the Code below

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default please help me with the Code below

    Hello guys,

    I have got a task to print all prime numbers in a certain range. Could you please tell me why the code does not work? The data validation works correct but the main task to print the prime numbers does not. Thank you.

    import java.io.*;
     
    public class Prime {
     
    	BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
     
    	public static int lowerBound;
    	public static int higherBound;
     
    	public void getInput() throws IOException
    	{
    		String line1 = input.readLine();
    		String line2 = input.readLine();
    		int lowInput = Integer.parseInt(line1);
    		int highInput = Integer.parseInt(line2);		
    		lowerBound = lowInput;
    		higherBound= highInput;
     
    	}
     
     
    	public void validatedata() throws IOException
    	{
     
    	do{
    	    if (higherBound <= lowerBound)
    	    {
    	    	System.out.println("The upper bound should be at least as big as the lower bound. " + "Please try again");
    	        getInput();
    	    }     
              else 
        	     System.out.println("the lower bount is: " + lowerBound);
    		     System.out.println("the high bound is: " + higherBound);
          } while (higherBound <= lowerBound);
    	}	
     
    	public void prime_calculation() throws IOException
    	{
     
    		while (lowerBound > 2)
    		{
    			int k = 0;
    			for (k = lowerBound; k <= higherBound; k++)
    			{
    			  if ((higherBound % k) != 0)
    			  {
    			    System.out.println(k);
    			    getInput();
    			  }
    			}	  
    		}
    	}
     
    }


    User class:

    import java.io.*;
     
    public class PrimeUser extends Prime {
     
     
    	public static void main(String argv[]) throws IOException
    	{
     
    		Prime isprime = new Prime();
    		isprime.getInput();
    		if (lowerBound < 2)
    			System.out.println("You typed in number less than two. Please try again.");
    		else if (higherBound <= lowerBound)
    			isprime.validatedata();
    		else
    			isprime.prime_calculation();
    	}
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: please help me with the Code below

    Please copy the program's output and paste it here. Add some comments showing what it wrong and what the output should be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please help me with the Code below

    If I type in:

    lower bound = 1
    upper bound = 5

    Output - "You typed in number less than two. Please try again." ---- This works fine

    2)
    lower bound = 6
    upper bound = 3

    Output - "The upper bound should be at least as big as the lower bound." - Works fine

    3)
    lower bound = 2
    upper bound = 100

    Output - nothing ----- doesn't work fine

    The output should be - 2, 3, 5, 7 etc. In other words it should list all prime numbers

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: please help me with the Code below

    Giving the user a blank screen and expecting him/her to know what to do with it is unfriendly and unreasonable. Improve your prompts so that the user knows what to do.

    Adding prompts may also show you what the program is doing, allowing you to see where you're making logic errors.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please help me with the Code below

    Will do. Thanks. However, this does not solve my problem as main code does not work.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: please help me with the Code below

    main code does not work.
    Try debugging the code by adding some calls to the println() method to print out the values of variables as the code executes so you can see what the code is doing. Be sure to put ID Strings in the print()s so you know where the number was printed instead of just printing a number without an ID:
    System.out.println("an ID for the variable "+ <the variable here>);
    System.out.println("i="+i);

    When you see what the code is doing, it will help you fix the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please help me with the Code below

    I am looking for someone to tell me the solution as I cannot fix it.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: please help me with the Code below

    At least you're honest. We're trying to give you basic debugging techniques that should help you find the source of the problem(s) and then fix them with or without our help. You can travel down that path with us, or you can move on. We're not simply going to fix it for you.

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

    Norm (October 20th, 2013)

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: please help me with the Code below

    Break the project up into separate simple steps:
    1) get user input
    2)validate the user's input
    3)calculate primes

    If step 1) works, change the code to automate the testing of step 2 by changing the code:
    //		String line1 = input.readLine();
    //		String line2 = input.readLine();
    //		int lowInput = Integer.parseInt(line1);
    //		int highInput = Integer.parseInt(line2);		
    		lowerBound = 3; //lowInput;   //  Hard code values for easier testing
    		higherBound= 20; //highInput;
    This will make the testing of steps 2 and 3 much faster & easier.

    Add some println statements to the calculate method to print out the values of the variables as their values are changed.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please help me with the Code below

    Ok guys,

    I fixed it and found my error. Please see below. I used boolean true/false for the purpose. However, now I am struggling with another requirement. I want a message to be printed if there are no prime numbers in the range at all. I just want a single message " No primes found". Can you please help me?

    This is my main method:

     	public void prime_calculation() throws IOException
    	{	
     
    		while ((lowerBound >= 2) && (higherBound > lowerBound))
    		{
    			for (int k = lowerBound; k < higherBound; k++)
    			{
    				boolean primecheck = true;
     
    					for (int j=2; j < k; j++)
    					{
    						if (k % j == 0)
    						{
    							primecheck = false;
    						     break;
    						}
    					}
    				if (primecheck)
    					System.out.println(k);
    			}
    		validatedata();
    		}	
    	}

  12. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: please help me with the Code below

    if ( noPrimesFound )
    {
        System.out.println( "No primes found in the specified range." );
    }

  13. #12
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please help me with the Code below

    Yes I know that. Already tried it before. However, the issue comes from the fact that it is posted more than once. I want a single statement if there are no matches. For example in my code it would be:

    if (primecheck == false)
     System.out.println("No primes found");

    And for example when I input -

    lower bound - 2
    upper b - 10

    It displays
    No primes found
    No primes found
    No primes found

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM