Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Goals for this problem set:
Scanner
to create interactive programs that read user inputif/else
statements for conditional executionScanner
Method name | Description |
---|---|
nextInt()
|
reads and returns the next token as an int , if possible
|
nextDouble()
|
reads and returns the next token as double , if possible
|
next()
|
reads and returns a single word as a String
|
nextLine()
|
reads and returns an entire line as a String
|
Example:
import java.util.*; // so you can use Scanner ... Scanner console = new Scanner(System.in); System.out.print("How old are you? "); // prompt int age = console.nextInt(); System.out.println("You typed " + age);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class StringOops { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Type your name: "); String name = console.nextString(); process(name); } public static void process(string "name") { if (name == Whitaker) { System.out.println("You must be really awesome."); } replace("a", "e"); toUppercase(name); name.substring(0, 3); System.out.println(name + " has " + name.length + " letters"); } } |
nextString
should be next
string
should be String
name
should not be in quotesWhitaker
should be in quotes==
; must
use .equals
replace
without specifying a string
object (name
)toUppercase
should be
toUpperCase
name.
should come
before toUpperCase
, not passed as a parameter to itname =
to store the result
of toUpperCase
name =
to store the result
of substring
()
when
calling length
public class StringOops { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Type your name: "); String name = console.next(); process(name); } public static void process(String"name") { if (name.equals("Whitaker")) { System.out.println("You must be really awesome."); } name = name.replace("a", "e"); name = name.toUpperCase(); name = name.substring(0, 3); System.out.println(name + " has " + name.length() + " letters"); } }
Write a complete program CollegeAdmit
with the behavior shown below.
Use the Scanner
to read user input for a student's grade point average and SAT exam score.
A GPA below 1.8 will cause the student to be rejected; an SAT score below 900 will also cause a rejection. Otherwise the student is accepted.
University admission program What is your GPA? 3.2 What is your SAT score? 1280 You were accepted!
Check your answer using Practice-It from the check-mark icon above.
Scanner
sumCopy and paste the following code into jGrasp.
public class SumNumbers { public static void main(String[] args) { int low = 1; int high = 1000; int sum = 0; for (int i = low; i <= high; i++) { sum += i; } System.out.println("sum = " + sum); } }
continued on next slide...
Scanner
sum
Modify the code to use a Scanner
to prompt the user for the values of low
and high
. Below is a sample execution in which the user asks for the same values as in the original program (1 through 1000):
low? 1 high? 1000 sum = 500500
Below is an execution with different values for low
and high
:
low? 300 high? 5297 sum = 13986903
You should exactly reproduce this format.
Write a method named longestName
that reads
names typed by the user and prints the longest name (the name that contains
the most characters) in the format shown below. Your method should accept a
console Scanner
and an integer n as parameters and
should then prompt for n names.
A sample execution of the call longestName(console, 4)
might
look like the following:
name #1? roy name #2? DANE name #3? sTeFaNiE name #4? Erik Stefanie's name is longest
Try to solve this problem in Practice-It: click on the check-mark above!