Home Java Java-tips Examples-introductory Dialog Dialog and Console Input-Output

Ask Questions?

View Latest Questions

Advertisement


 
 

Dialog and Console Input-Output
Posted on: July 26, 2006 at 12:00 AM
This is similar to the previous program, but it also gets input from the user.

Prev: Dialog Box Output | Next: Example: Captitalize

Java Notes

Dialog and Console Input-Output 

This is similar to the previous program, but it also gets input from the user.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
// Description: This program gets a string from a dialog box.
// File:   dialogInputOutput/ThirdProgram.java
// Author: Michael Maus
// Date:   29 Jan 2005

import javax.swing.*;

public class ThirdProgram {

    public static void main(String[] args) {
        String humanName;  // A local variable to hold the name.

        humanName = JOptionPane.showInputDialog(null, "What's your name, Earthling");

        JOptionPane.showMessageDialog(null, "Take me to your leader, " + humanName);
    }

}
Line 11 - Declaring a local variable.
This tells the compiler to reserve some memory to hold a String. It's going to hold a name, so we called the variable (a place in the computer's memory) "humanName". The syntax for a simple declaration is to write the type of thing that a variable will hold (String in this case), followed by the variable name (humanName in this case).
Line 13 - Asking the user for a String.
JOptionPane's showInputDialog method displays a message in a dialog box that also accepts user input. It returns a string that can be stored into a variable.

This is an assignment statement. The part to the right of the "=" must produce a value, and this value is then stored in the variable on the left (humanName).

Line 15 - Putting two strings together (concatenation)
Concantenation, indicated by the plus sign (+), puts two strings together to build a bigger string, which is then passed as a parameter. The plus sign (+) is also used for addition of numbers.

Console Input: Scanner

The java.util.Scanner class (added in Java 5) allows simple console and file input. Of course, your program should eventually have a GUI user interface, but Scanner is very useful for reading data files.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
// File   : introductory/IntroScanner.java
// Purpose: Write to and read from the console.
// Author : Michael Maus
// Date   : 2006-01-20

import java.util.*;                //Note 1

public class IntroScanner {

    public static void main(String[] args) {
        //... Initialization
        String name;               // Declare a variable to hold the name.
        Scanner in = new Scanner(System.in);

        //... Prompt and read input.
        System.out.println("What's your name, Earthling?");
        name = in.nextLine();      // Read one line from the console.
        in.close();                //Note 2

        //... Display output
        System.out.println("Take me to your leader, " + name);
    }
}

Notes

  1. Altho we only need the Scanner class from the java.util package, the most common programming style is to make all classes (*) visible.
  2. Closing the console isn't really necessary, but it's a good habit. If we had been reading a file, which is common with Scanner, closing it would be important.
Copyleft 2006 Fred Swartz MIT License
Advertisement


DMCA.com