input box java code example

Example 1: input java

Scanner in = new Scanner(System.in);
      System.out.print("Please enter hour 1: ");
      int hour1 = in.nextInt();
      System.out.print("Please enter hour 2: ");
      int hour2 = in.nextInt();
      System.out.print("Please enter minute 1: ");
      int min1 = in.nextInt();
      System.out.print("Please enter minute 2: ");
      int min2 = in.nextInt();

Example 2: how to input in java

import java.util.Scanner;
...
  Scanner console = new Scanner(System.in);
  int num = console.nextInt();
  console.nextLine() // to take in the enter after the nextInt() 
  String str = console.nextLine();

Example 3: java joptionpane

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ShowMessageDialogExample1
{
  public static void main(String[] args)
  {
    String backupDir = "/Users/al/backups";
    
    // create a jframe
    JFrame frame = new JFrame("JOptionPane showMessageDialog example");
    
    // show a joptionpane dialog using showMessageDialog
    JOptionPane.showMessageDialog(frame,
        "Problem writing to backup directory: '" + backupDir + "'.");
    System.exit(0);
  }
}

Example 4: java joptionpane

JOptionPane.showMessageDialog(frame, "A basic JOptionPane message dialog");

Tags:

Cpp Example