Validation We have covered one way to get user input, using Scanner to get it from the console We will soon cover at least one additional way to get.

Slides:



Advertisements
Similar presentations
Lecture 3 Java Basics Lecture3.ppt.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: , 5.3 self-check: Ch. 6 #1-6.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
Computer Programming Lab(4).
Computer Programming Lab(5).
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
John Hurley Spring 2011 Cal State LA CS 201 Lecture 6:
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
John Hurley Cal State LA CS 201 Lecture 5. 2 Loops A loop performs a set of instructions repeatedly as long as some condition is met while (x != 0) {
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
1 BUILDING JAVA PROGRAMS CHAPTER 6 DETAILS OF TOKEN-BASED PROCESSING.
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
John Hurley Spring 2011 Cal State LA CS 201 Lecture 6:
Introduction to programming in java
John Hurley Spring 2011 Cal State LA CS 201 Lecture 5:
CompSci 230 S Programming Techniques
Introduction to programming in java
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
CS 201 Lecture 7: John Hurley Summer 2012 Cal State LA.
John Hurley CS201 Cal State LA
John Hurley Cal State LA
Exceptions and User Input Validation
Building Java Programs
Building Java Programs
Chapter 5: Control Structures II
Building Java Programs
Computer Programming Methodology Input and While Loop
User input We’ve seen how to use the standard output buffer
Introduction to Methods in java
Repetition.
Chapter 5: Control Structures II
TK1114 Computer Programming
Topic 11 Scanner object, conditional execution
Something about Java Introduction to Problem Solving and Programming 1.
Topic 20 more file processing
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 6
Building Java Programs Chapter 6
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Computer Programming
Building Java Programs
Introduction to Java Brief history of Java Sample Java Program
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Input/output (I/O) import java.io.*;
CSE Module 1 A Programming Primer
Building Java Programs
CSC1401 Input and Output (with Files)
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Building Java Programs
Building Java Programs
Indefinite loop variations
Input/output (I/O) import java.io.*;
Random Numbers while loop
Computer Science Club 1st November 2019.
Optional Topic: User Input with Scanner
Presentation transcript:

Validation We have covered one way to get user input, using Scanner to get it from the console We will soon cover at least one additional way to get input We have noted that users are human beings and accordingly are impossible to predict Input is not always usable

Validation Screen input for problems before using it while and do… while loops are very handy for this Example: Scanner sc = new Scanner(System.in); int age; do { System.out.println("How old are you?"); age = sc.nextInt(); } while (age < 0 || age > 100); System.out.println("You entered " + age);

Validation We could still easily make the program above break enter “a” enter <esc>

Validating Scanner Input So far, our programs have crashed if casts to numeric types failed: Try this with input “two point five”: import java.util.Scanner; public class ReadDouble2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); double stuff = 0.0; do { System.out.print("Input a double. Enter 0 to quit:"); stuff = input.nextDouble(); System.out.println("\nYou entered: " + stuff); } while (stuff != 0.0); }

Validating Scanner Input Here is the simplest way to validate Scanner input for data format (that is, to make sure you get input that can be cast to double, integer, etc.) Scanner has hasNext…() methods that see if there is a parseable token hasNextInt() hasNextDouble() hasNextBoolean() Also need next() to skip over bad input

Validating Scanner Input Try this one with input “nine.three”, then with input “nine point three: import java.util.Scanner; public class ReadDouble3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); Double inpDouble = 10.0; // bad code ahead!! do { System.out.print("Input a double. Enter 0 to quit:"); if(input.hasNextDouble()){ inpDouble = input.nextDouble(); System.out.println("\nYou entered: " + inpDouble); } else input.next(); } while (inpDouble != 0.0);

Validating Scanner Input In order to get good output, we need to arrange things in a slightly more complex way: Scanner sc = new Scanner(System.in); String badInputString = null; System.out.println("Enter an integer"); while (!sc.hasNextInt()) { badInputString = sc.nextLine(); System.out.println(badInputString + " isn't an integer! Please try again."); } int x = sc.nextInt(); System.out.println(x);

Validating Both Format and Value int value = 0; String badInputString = null; do { System.out.println("Enter an integer greater than 100"); while (!sc.hasNextInt()) { badInputString = sc.nextLine(); System.out.println(badInputString + " isn't an integer! Please try again."); } value = sc.nextInt(); sc.nextLine(); // throw away linefeed } while (value <= 100); System.out.println(value);