Comparing Console input/out (Scanner) with GUI input/output (JOptionPane)





Basic Console Input to Screen (Scanner)
import java.util.Scanner;// Notice Import Statement for keyboard input public class ExampleScannerUse // Program name is ExampleScannerUse { public static void main(String[] args) // Main Method { Scanner input = new Scanner(System.in); // Scanner used for keyboard input System.out.print("Enter an integer for x: "); int x = input.nextInt(); // integer variable x stored input answer System.out.print("Enter an integer for y: "); Double y = input.nextDouble(); // Double variable y stored input answer Double sum = x + y; System.out.println(x + " + " + y + " = " + sum); // Output sum of x plus y } }            
    Basic GUI Input to Screen (JOptionPane)
import javax.swing.JOptionPane;// Notice Import Statement for keyboard input public class ExampleGUIUse // Name of Program is ExampleGUIUse { public static void main(String[] args) // Main Method { String inputIntegerFromUser = JOptionPane.showInputDialog(null, "Enter an integer for x: ", JOptionPane.QUESTION_MESSAGE); // Line above Creates a string input variable and have popup ask question int x = Integer.parseInt(inputIntegerFromUser); // Line above Creates integer variable and assign x to the input String inputIntegerFromUser String inputDecimalFromUser = JOptionPane.showInputDialog(null, "Enter an decimal for y: ", JOptionPane.QUESTION_MESSAGE); // Line above Creates a String input variable and have popup ask question Double y = Double.parseDouble(inputDecimalFromUser); // Line above Creates Double variable and assign y to the input String inputDecimalFromUser Double sum = x + y; // Create Double variable named sum and adds x and y JOptionPane.showMessageDialog(null, x + " + " + y + " = " + sum); // Output Popup of the addition of x and y showing sum } }