CS 0401 Lecture Notes - Lecture 5: Traffic Ticket

71 views2 pages
19 Jan 2017
School
Course
Professor
CS 401
1/19/17 Lecture 5
project emulating getting a traffic ticket
review of last week:
anytime you are using a scanner to read from keyboard or file: you must use”
throws Exception “ after the method
example:
public static voic main( String[] args) throws Exception
{
}
using kbd.next(); will take the next token. If the user enters more than one token,
the next time kbd.next() is called, it will take the second word the user inputs.
Using kbd.nextLine(); - it will clear the buffer, but it will return the entire string the
user inputs, so the user can use a phrase, first and last name, etc.
How to capture a Boolean value?
Ex:
{
boolean flag;
System.out.print(“Are you having fun?”);
flag = kbd.nextboolean();
System.out.println(“You entered “ + flag);
}
The user must enter either “True” or “false” or it will throw a user input mismatch error
can be any combination of case
If you want to use “yes or no” – use an if statement
flag toUpperCase(); // This will allow the ignore case
If (flag.equals(“YES”))
System.out.println(“very good.”);
If you want to include more than “yes” (like yea..)
You can accept all strings that begin with the letter ‘y.’
flag.toUpperCase(); - will just create a temporary copy of the string. You have to stop
and create another reference or else you will lose the capital copy (useless statement).
To get to the first letter: if (flag.charAt( 0 ) == ‘Y’ )
Single quotations for char!!
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Anytime you are using a scanner to read from keyboard or file: you must use . 1/19/17 lecture 5 project emulating getting a traffic ticket review of last week: throws exception after the method example: public static voic main( string[] args) throws exception. Using kbd. next(); will take the next token. If the user enters more than one token, the next time kbd. next() is called, it will take the second word the user inputs. Using kbd. nextline(); - it will clear the buffer, but it will return the entire string the user inputs, so the user can use a phrase, first and last name, etc. The user must enter either true or false or it will throw a user input mismatch error can be any combination of case. If you want to use yes or no use an if statement. If you want to include more than yes (like yea) flag. touppercase(); - will just create a temporary copy of the string.

Get access

Grade+20% OFF
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers