# Wanting to repeat my program...

• August 8th, 2011, 06:10 AM
Shaybay92
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...
Code Java:

```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; }       } } }```
• August 8th, 2011, 06:56 AM
Norm
Re: Wanting to repeat my program...
Quote:

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 + "<");

Code :

`(inputRepeat == "yes"){`
Use the equals method when comparing Strings. == is for primitives.
• August 8th, 2011, 07:18 AM
Shaybay92
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.
• August 8th, 2011, 07:27 AM
Norm
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.
• August 8th, 2011, 08:29 AM
serdar
Re: Wanting to repeat my program...