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

Thread: Wanting to repeat my program...

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Wanting to repeat my program...

    So I have created a program to tell whether a number is a prime number or not. I basically did this by finding the modulus of the number with all numbers less than it. If the modulus is 0 for any number then isPrime = true. It runs fine up until I ask for another number to be tested. It won't let me type anything into the command line to re-run it...
    import java.util.Scanner;
     
    public class primeNum {
    	public static void main(String[] args){
     
    	boolean repeatPrime = true;
     
    	while (repeatPrime){
     
    	Scanner in = new Scanner(System.in);
    	boolean isPrime = false;
     
    	System.out.println("Please enter a number");
     
            //Sets the number to be tested for primality
    	int testNumber = in.nextInt();
     
            //Finds the modulus of all numbers less than the number being tested (except 1).
    	for(int index = 2; index < testNumber; index++){
    		if ((testNumber % index) == 0){
    			isPrime = true;
    		}
    	}
     
    	if (isPrime){
    		System.out.println("This is a prime number!");
    	} else{
    		System.out.println("This is not a prime number.");
    	}
     
            //Repeat the test?
    	System.out.println("Do you want to test another number? (yes/no)");
    	String inputRepeat = in.nextLine();
     
    	if (inputRepeat == "yes"){
    		repeatPrime = true;
    	} else { repeatPrime = false; }
     
     
     
          }	
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Wanting to repeat my program...

    It won't let me type anything into the command line to re-run it...
    Can you copy here the console the shows this problem.

    The problem is probably with the Scanner class. You need to call nextLine after calling nextInt to clear the new line character left in the Scanner's buffer.
    To see what is read by nextLine() add a println to print out its value:
    System.out.println(" inputRepeat=" + inputRepeat + "<");

    (inputRepeat == "yes"){
    Use the equals method when comparing Strings. == is for primitives.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Wanting to repeat my program...

    Thanks for the tip. When I run the program it prints inputRepeat as nothing.. I just get an output of:

    inputRepeat=<

    So are you saying when the previous line is printed in console, it takes the \n as my input for inputRepeat? I dont see why I need to call nextInt() first.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Wanting to repeat my program...

    You need to call nextLine after calling nextInt to clear the new line character so that the next read will be from the next line that you input.

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    25
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Wanting to repeat my program...

    call next() instead of nextLine()

Similar Threads

  1. Created simple game applet but wanting it to access a DSN on the server
    By hirsty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 21st, 2011, 03:11 AM
  2. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM
  3. repeat creation of text fields
    By kurt-hardy in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 11:05 AM
  4. wanting to convert printout to UPPER case
    By welikedogs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 3rd, 2010, 01:22 PM
  5. Um, ya, wanting to do a little more than my program can take
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2010, 04:36 PM

Tags for this Thread