Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java Notes

Console vs Dialog I/O

Dialog box I/O is useful. Normal programs use a Graphical User Interface (GUI) that creates windows, etc. Because writing a full GUI interface requires understanding a few things first, introductory examples are often limited to using only dialog boxes for I/O.

Console I/O is less useful. Console I/O was the only kind of I/O that was built into programming langauges from the beginning of programming until recently. Textbooks were written to use it, and instructors learned it. It no longer makes much sense to use it, but it will take a while before most textbook authors change.

Brief summary to aid in converting console I/O to dialog boxes

Comments
Console The problem with learning console I/O is that it's almost totally useless in a real GUI program. The Scanner class is only available in Java 5 and beyond, and doing standard console input without Scanner is not a pretty picture for a beginner. Scanner will be useful when you learn text file input, but that topic is often taught at the end of the second course.
Dialog It's useful to learn to use dialog boxes because they're part of the GUI system, and are something that you will use in real GUI programs.
Imports
Console import java.util.*; // For Scanner class.
Dialog import javax.swing.*; // For JOptionPane class.
Initialization
Console Scanner input = new Scanner(System.in);
Dialog None required.
Output
Console System.out.println("Display this");
Dialog JOptionPane.showMessageDialog(null, "Display this");
Line of text input
Console String name;
System.out.print("Enter your name: ");
name = input.nextLine();
Dialog String name;
name = JOptionPane.showInputDialog(null, "Enter your name");
Integer input
Console System.out.print("Enter your age: ");
int age = input.nextInt();
Dialog String aStr = JOptionPane.showInputDialog(null, "Enter your age");
int age = Integer.parseInt(aStr);
Or these could be combined on one line, eliminating the temporary variable:
int age = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter your age"));

Example program written two ways

Here's a simple program to average three ints, written once with JOptionPane and again with Scanner.

JOptionPane version reads and writes using dialog boxes. Because the showInputDialog method always returns a String, it has to be converted to a numeric type with, eg, Integer.parseInt().

Scanner was introduced in Java 5 (JDK 1.5) and is not available in older versions of Java. It can be used for input from the console and files, but not for GUI input. It has a large number of useful methods.

Average3 using JOptionPane

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
// File:   average3/Average3JOptionPane.java
// Description: Average three ints.  Use JOptionPane.
// Author: Fred Swartz
// Date:   2005-08-30

import javax.swing.*;

public class Average3JOptionPane {

    public static void main(String[] args) {

        //... Declare local variables;
        int a, b, c;  // No meaningful names are possible.
        int average;
        String temp;  // Temporary storage for JOptionPane input.

        //... Read three numbers from dialog boxes.
        temp = JOptionPane.showInputDialog(null, "First number");
        a = Integer.parseInt(temp);  // Convert String to int.
        temp = JOptionPane.showInputDialog(null, "Second number");
        b = Integer.parseInt(temp);
        temp = JOptionPane.showInputDialog(null, "Third number");
        c = Integer.parseInt(temp);

        //... Compute their average.
        average = (a + b + c) / 3;

        //... Display their average in a dialog box.
        JOptionPane.showMessageDialog(null, "Average is " + average);
    }
}

Average3 using Scanner and println

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
// File:   average3/Average3Scanner.java
// Description: Average three ints.  Use Scanner.
// Author: Fred Swartz
// Date:   2005-08-30

// Note: Scanner was added in Java 5 (JDK 1.5).

import java.util.Scanner;

public class Average3Scanner {

    public static void main(String[] args) {

        //... Declare local variables;
        int a, b, c;  // No meaningful names are possible.
        int average;

        //... Initialize Scanner to read from console.
        Scanner input = new Scanner(System.in);

        //... Read three numbers from the console.
        System.out.print("Enter first number : ");
        a = input.nextInt();
        System.out.print("Enter second number: ");
        b = input.nextInt();
        System.out.print("Enter last number  : ");
        c = input.nextInt();

        //... Compute their average.
        average = (a + b + c) / 3;

        //... Display their average on the console.
        System.out.println("Average is " + average);
    }
}

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.