Beep Java

Lets learn how to create beep using java. We can create a beep sound to indicate a warning or error.

Beep Java

Lets learn how to create beep using java. We can create a beep sound to indicate a warning or error.

Beep Java

Beep Java

     

Lets learn how to create beep using java.

We can create a beep sound to indicate a warning or error by using java. However, it is often better to give a warning dialog box with a description about  the problem using GUI interface.

There are  two ways to produce the sound beep:
1.Using Toolkit class: We can create a beep using Toolkit class. For this we first need to import java.awt.Toolkit. This class provides beep() method to create beep sound. The following syntax is used to create beep using Toolkit class:
import java.awt.Toolkit;
Use this statment in your code:
Toolkit.getDefaultToolkit().beep().
This is not always a sufficient way to alert the user as there may not be speakers on the user's computer.
2.Using ASCII value: ASCII refer as American Standard Code for Information Interchange. We use ASCII value "\007" to produce bee
p. As we are using in our example.

Here is the code of the program :

import java.util.*;
public class Beep{

 
  public static void main(String[] args) {

  if(args[0].equals("1"))
  System.out.println("\007");
  if(args[0].equals("2"))
  System.out.println("\007\007");
  if(args[0].equals("3"))
  System.out.println("\007\007\007");
  if(args[0].equals("4"))
  System.out.println("\007\007\007\007");
  if(args[0].equals("5"))
  System.out.println("\007\007\007\007\007");
  if(args[0].equals("6"))
  System.out.println("\007\007\007\007\007\007");
  if(args[0].equals("7"))
  System.out.println("\007\007\007\007\007\007\007");
  if(args[0].equals("8"))
  System.out.println("\007\007\007\007\007\007\007\007");
  }

}

Download this example.