String Input ICS 111: Introduction to Computer Science I

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
An Introduction to Textual Programming
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
C Formatted Input/Output
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Input/Output.
Chapter 2 More on Math More on Input
What Actions Do We Have Part 1
Input and Output Upsorn Praphamontripong CS 1110
Introduction to Java part 2
CSCI 161 – Introduction to Programming I William Killian
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Basic operations in Matlab
I/O Basics.
Intro to PHP & Variables
Java Programming: From Problem Analysis to Program Design, 4e
INPUT STATEMENTS GC 201.
Comp Sci 302 Introduction to Programming
String Output ICS 111: Introduction to Computer Science I
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Introduction to C++ Programming
Introduction to Java part 2
A+ Computer Science INPUT.
Command Line Arguments
File I/O ICS 111: Introduction to Computer Science I
MSIS 655 Advanced Business Applications Programming
Introduction to Computing Using Java
Object Oriented Programming in java
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
A+ Computer Science INPUT.
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Java part 2
12th Computer Science – Unit 5
CSC 1051 – Data Structures and Algorithms I
Building Java Programs
Optional Topic: User Input with Scanner
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

Email Address Problem Let’s prompt the user for first & last name, and output their email 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 “@hawaii.edu” Output the email address Computer program See Email.java 11/18/2018 © 2007 William Albritton

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

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

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

Class Exercise 2 Identify the bugs & fix them See Email.java 11/18/2018 © 2007 William Albritton

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

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

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