Download presentation
Presentation is loading. Please wait.
1
Unit 1: Intro Lesson 3: Input
2
Methods Input Input methods are statements which give the user an interface through which to modify variables within a program Input methods must be imported Input method 1: Scanner Input method 2: JOptionPane.showInputDialog(“ “);
3
Scanner Simple Input can be saved directly to any type of variable (int, float, double, etc.) Can only be used in an IDE or the Command Line
4
JOptionPane.showInputDialog(“ “);
Slightly more complicated Works outside of the IDE or Command Line User – friendly interface Combines an Output dialog with an Input field (input box) Input can only be saved as Strings and must be converted (parsed) to a separate variable to be used as any other data type
5
Scanner Usage Import statement
Variable to store Input of desired data type Variable to create the Scanner object in memory Statement to assign Input to the storage variable Import java.util.Scanner; int myVariable; Scanner myKeyboard = new Scanner(System.in); myVariable = scanner.nextInt();
6
variable = scanner.nextDatatype();
int myVariable; myVariable = scanner.nextInt(); int .nextInt(); float myVariable; myVariable = scanner.nextFloat(); float .nextFloat(); String myVariable; myVariable = scanner.nextLine(); String .nextLine(); Boolean myVariable; myVariable = scanner.nextBoolean (); Boolean .nextBoolean();
7
JOptionPane Input Usage
Import statement String variable to store Input (optional) Variable of desired data type (for after parsing) Statement to assign Input to the storage variable (optional) Statement to parse (convert) Input to desired data type Import javax.swing.JOptionPane; String myVariable; float myParsedVar; (Optional) JOptionPane.showInputDialog(“Please input some data”); myVariable = Float.parseFloat(myParsedVar); (Optional)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.