CS 1063, Fall 2005
Repeated Input in Java

If you don't have version 1.5 on your computer, or if you can't seem to configure JBuilder to work with 1.5, you can use the following approaches, which work with older versions of Java.

Repeated Input in Java (first version)
import java.io.*;

public class RepeatedInputTest {

   public static void main(String[] args) throws IOException {

      BufferedReader console = 
         new BufferedReader( new InputStreamReader(System.in));
      char again;
   
      do {
         System.out.print("Enter an integer ---> ");
         String input = console.readLine();
         int value = Integer.parseInt(input); // convert string to int
         
         System.out.println("You entered: " + value);
     
         System.out.print("Would you like to continue? (Y or y)  ");
         input = console.readLine();
         again = input.charAt(0);
      } while (again == 'y' || again == 'Y');
   }
}
Results of a run
    (boldface = typed input):
Enter an integer ---> 47
You entered: 47
Would you like to continue? (Y or y)  Y
Enter an integer ---> 996
You entered: 996
Would you like to continue? (Y or y)  y
Enter an integer ---> 444
You entered: 444
Would you like to continue? (Y or y)  n

Here is a second version of the program above, that uses a String, rather than a char for the reply:

Repeated Input in Java (second version)
import java.io.*;

public class RepeatedInputTest2 {

   public static void main(String[] args) throws IOException {

      BufferedReader console = 
         new BufferedReader( new InputStreamReader(System.in));
      String input;
      do {
         System.out.print("Enter an integer ---> ");
         input = console.readLine();
         int value = Integer.parseInt(input); // convert string to int
         
         System.out.println("You entered: " + value);
     
         System.out.print("Would you like to continue? (Y or y)  ");
         input = console.readLine();
      } while (input.equals("y") == true || input.equals("Y") == true);
      // can leave off the "== true" above if you like
   }
}
Results of a run
    (boldface = typed input):
Enter an integer ---> 47
You entered: 47
Would you like to continue? (Y or y)  Y
Enter an integer ---> 996
You entered: 996
Would you like to continue? (Y or y)  y
Enter an integer ---> 444
You entered: 444
Would you like to continue? (Y or y)  n

In case your version of JBuilder is using the 1.5 version of Java, you can use the following code to input integer over and over.

Java input if 1.5 is available
import java.util.Scanner;

public class RepeatedInputTest3 {

   public static void main(String[] args) {

      Scanner in = new Scanner(System.in);
      String input;
      do {
         System.out.print("Enter an integer ---> ");
         int value = in.nextInt();
         
         System.out.println("You entered: " + value);
     
         System.out.print("Would you like to continue? (Y or y)  ");
         input = in.next();
      } while (input.equals("Yes") == true || 
               input.equals("YES") == true || 
               input.equals("yes") == true );
      // can leave off the "== true" above if you like
   }
}
Results of a run (boldface = typed input):

Enter an integer ---> 47
You entered: 47
Would you like to continue? (Yes or YES or yes)  Yes
Enter an integer ---> 499
You entered: 499
Would you like to continue? (Yes or YES or yes)  YES
Enter an integer ---> 372
You entered: 372
Would you like to continue? (Yes or YES or yes)  yes
Enter an integer ---> 333
You entered: 333
Would you like to continue? (Yes or YES or yes)  Y