Code With Design

Web development, code, design, css, all around programming blog

Create a press enter to continue with java

Create a press enter to continue with java

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.

Related posts:

  1. Creating user input with menus in java
  2. Java: Pass as many strings as you want through a method
  3. Writing and reading objects and arrays to a binary file with java
  4. Writing information to a binary file in java
  5. Reading information from a binary file with java
By: Caleb. Java, 0 responses

Thoughts?

Please fill your data and comment below.
Name
Email
Website

Your comment