Sometime a pause in your program is needed and the process of implementing this is easier then you would think. It involves one simple method that we will call into our main method when we need to pause.
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.Scanner; public class appletTest1{ public static void main(String[] args) { pauseProg(); } public static void pauseProg(){ System.out.println("Press enter to continue..."); Scanner keyboard = new Scanner(System.in); keyboard.nextLine(); } } |
The only important piece of code here that you will need to go over is the ‘keyboard.nextLine();’ This will take the object we created earlier in the program and set the next line breaker which is enter. When the program runs up to this line it waits for the user to import a string. By pressing enter you then process that string which does nothing because it does not get stored into a variable. The line breaks and the program continues.