Download presentation
Presentation is loading. Please wait.
Published byGregory Anthony Modified over 6 years ago
1
String Input ICS 111: Introduction to Computer Science I
William Albritton Information and Computer Sciences Department at the University of Hawai‘i at Mānoa “If you study to remember, you will forget, but if you study to understand, you will remember.” Unknown 11/18/2018 © 2007 William Albritton
2
Input Problem Your friends are so impressed that their friends now want a program that displays their name in all lowercase & uppercase Understand the problem You are getting tired of rewriting programs, so you just want one program that can be used by anyone Write an algorithm Ask the user to enter name & store the name Print name in all lowercase & uppercase Write the computer program See InputName.java 11/18/2018 © 2007 William Albritton
3
Output in Java What is System.out?
An automatically created object that represents output to the monitor (screen, console) out is an object located within the System class out is an object of PrintStream class Sends a stream of data to the console window See Java API for class System, and look under “Field Summary” for object out Terminology The term “I/O” is short for “Input and Output” 11/18/2018 © 2007 William Albritton
4
Output in Java Class PrintStream 3 common methods of class PrintStream
System.out.print("Aloha!"); //Aloha! System.out.println(); //newline System.out.println("Ossu!"); //Ossu! + newline See Java API for class PrintStream 11/18/2018 © 2007 William Albritton
5
Input in Java What is System.in?
An automatically created object that represents input from the monitor (screen, console) in is an object located within the System class in is an object of InputStream class Receives a stream of data from the keyboard Cannot use System.in directly Have to use an object of the Scanner class to do input See Java API for class System, and look under “Field Summary” for object in and class InputStream 11/18/2018 © 2007 William Albritton
6
Class Scanner Class Scanner organizes the “raw” stream of data from the keyboard into more manageable units such as a String object To use a Scanner object Put import statement at top of file import java.util.Scanner; Create a Scanner object Scanner keyboard = new Scanner(System.in); Get one full line of input & store as a string String line = keyboard.nextLine(); 11/18/2018 © 2007 William Albritton
7
Scanner Methods nextLine() returns everything that was typed on a single line on the console All typed input until user presses “Enter” key These characters are stored in a String object next() returns the first word (also called a “token”) typed on a single line on the console All characters up to the next blank space (a space, newline, or tab) 11/18/2018 © 2007 William Albritton
8
Escape Sequence How can we output quotations within a string?
Use special characters that begin with a backslash character (\) double_quote = \" single_quote = \' newline = \n tab = \t backslash = \\ System.out.println("She said \"Hello\""); //She said "Hello" 11/18/2018
9
Class Exercise 1 Write a Java program that prompts the user to enter their first & last names. Display the user’s initials. Two choices for user input: Ask the user to enter the names separately & use Scanner method nextLine() Ask the user to enter BOTH names at once, & use the Scanner method next(), which returns all characters up to the next blank space (a space, newline, or tab) 11/18/2018 © 2007 William Albritton
10
A Fourth Step “The best laid plans of mice and men often go astray” – John Steinbeck, Of Mice & Men Often, your day does not go as planned May fall asleep on the train & miss correct stop May get lost underwater while scuba diving May forget something when taking a friend surfing We need to add a 4th step to problem solving Understand the problem Make a plan (algorithm) Implement the plan (computer program) Fix/redo/improve the plan (debugging) 11/18/2018 © 2007 William Albritton
11
Address Problem Let’s prompt the user for first & last name, and output their address Algorithm Ask the user for first & last name & store separately Store the first letter of first name Store the first 7 letters of last name Add the first two strings and Output the address Computer program See .java 11/18/2018 © 2007 William Albritton
12
Debugging The Email.java program has lots of bugs!
Where does the term “bug” originate? See “links” column on webpage for details So where do we start with debugging a program? Trace through the code in our heads 1st step to debugging, but often cannot see past our own “logic” 11/18/2018 © 2007 William Albritton
13
Debugging What is another way to debug a program?
Put lots of System.out.println() statements in code to get feedback System.out.println("variable:" variable); But must do a lot of typing… Use a Debugger A function of an IDE (Integrated Development Environment) that helps you to “step through” a program For detailed instructions, see jGRASP debugger tutorial under the links column 11/18/2018 © 2007 William Albritton
14
Using the Debugger Steps to using the debugger
Position mouse on the left side of the program window, somewhere within the main() method A red dot should appear Click on the red dot The red dot should become permanent Compile the program Click on the Ladybug icon This will “Run debugger on current file” Press top left blue arrow to step through code 11/18/2018 © 2007 William Albritton
15
Class Exercise 2 Identify the bugs & fix them See Email.java
11/18/2018 © 2007 William Albritton
16
String Methods replace(target, replacement)
Replaces target string with replacement string See program ReplaceExample.java String string1 = new String("abcd"); String string2 = string1.replace("bc", "HEA"); System.out.println(string1); //abcd System.out.println(string2); //aHEAd Note that String methods do not change the original string String methods return a different string 11/18/2018
17
String Methods substring(start, end) substring(start)
Returns a substring made up of the characters from start to end-1 in the original string String string = new String("abcde"); System.out.println( string.substring(1,3));//output "bc" substring(start) Returns a substring made up of the characters from start to the ending of the string System.out.println( string.substring(2));//output "cde" 11/18/2018 © 2007 William Albritton
18
Class Exercise 3 Write a Java program that asks the user to enter a sentence, then replace all the blank spaces in the sentence with the word “BLANK” For example, “This is a pen.” would change to “ThisBLANKisBLANKaBLANKpen.” 11/18/2018 © 2007 William Albritton
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.