String Comparison, Scanner

Slides:



Advertisements
Similar presentations
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Advertisements

Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
Chapter 2: Java Fundamentals Input and Output statements.
1 TCSS 143, Autumn 2004 Lecture Notes Review. 2 Computer programming computers manipulate data data is often categorized into types numbers (integers,
© 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.
CS0007: Introduction to Computer Programming Scope, Comments, Code Style, Keyboard Input.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Expressions, Data Conversion, and Input
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
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.
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.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
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.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
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.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
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.
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Foundations of Programming: Java
Dept of Computer Science University of Maryland College Park
Conditionals, Block Statements, Style
Dept of Computer Science University of Maryland College Park
Input/Output.
Chapter 4 Assignment Statement
Chapter 2 More on Math More on Input
Computer Organization, Eclipse Intro
Dept of Computer Science University of Maryland College Park
Java Variables and Types
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Elementary Programming
Dept of Computer Science University of Maryland College Park
Debugging and Random Numbers
Chapter 3 Assignment Statement
Chapter 3 Java Input/Output.
Switch, Rounding Errors, Libraries
Java Programming: From Problem Analysis to Program Design, 4e
Scope, Comments, Code Style, Keyboard Input
Program Style Console Input and Output
String Input ICS 111: Introduction to Computer Science I
Starting JavaProgramming
الكلية الجامعية للعلوم التطبيقية
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Computers & Programming Languages
Introduction to Classes and Methods
Chapter 2: Basic Elements of Java
A+ Computer Science INPUT.
Fundamentals 2.
Chapter 3 Input/Output.
Chapter 2: Java Fundamentals
A+ Computer Science INPUT.
CS2011 Introduction to Programming I Elementary Programming
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Chapter 1 c++ structure C++ Input / Output
Reading Input and Scanner Class
Presentation transcript:

String Comparison, Scanner CMSC 131 Object-Oriented Programming I String Comparison, Scanner Dept of Computer Science University of Maryland College Park This material is based on material provided by Ben Bederson, Bonnie Dorr, Fawzi Emad, David Mount, Jan Plane

Overview String Comparison Scanner Programming errors Debugging Conditionals Logical Operators

To Import Class Examples Do not extract the zip file In Eclipse select FileImport…General(Expand by clicking on triangle)Existing Projects into WorkspaceNext NOTICE that “Archive File” is NOT the option you want Select the option "Select archive file" and browse for the zip file Select Open and Finish

String Comparison String Comparison: Strings should not be compared using the above operators (==, <=, <, etc). Let s and t be strings. s.equals(t)  returns true if s equals t s.length()  returns length s.compareTo(t)  compares strings lexicographically (dictionary order) result < 0 if s is less than t result == 0 if s is equal to t result > 0 if s is greater than t Example: StringComparison1.java Important: In some cases you may get the expected results when comparing strings using == Do not use == to compare strings. Use equals or compareTo Example: StringComparison2.java

User Input in Java We've done output (System.out); what about input? Java includes the Scanner class feature Can use Scanner to create “scanner objects” Scanner objects convert user input into data To use Scannner need to import a library: import java.util.Scanner;

Scanner Class Details To create a scanner object: new Scanner(input_source); Input source can be keyboard (System.in), files, etc. Object must be assigned to a variable (e.g. sc) Operations nextBoolean() nextByte() nextDouble() nextFloat() nextInt() nextLong() nextShort() next() Returns sequence of characters up to next whitespace (space, carriage return, tab, etc.) nextLine() Returns sequence of characters up to next carriage return Example: Scannerl1.java, Scanner2.java Returns value of indicated type (reports error if type mismatch)

Objects About Scanner Scanner is a class defined in java.util.Scanner System.in is a predefined object for keyboard input sc = new Scanner(System.in) creates a new object in the Scanner class and assigns it to sc Object? A bundle of data (instance variables) and operations (methods) A class defines both instance variables and methods for objects A class is also a type for objects new creates new objects in the given class We will learn (much) more about objects later

Programming Errors Syntax error Violates language’s grammar Compiler will catch them Eclipse makes red squiggles under your code Run-time errors (exceptions in Java) Something unexpected happens during program execution (e.g., dividing by 0, file not found) Semantical/logical errors Program does not generate expected results Can be challenging to uncover 1. Worst error in semantical domain  Data races

Debugging Process of finding and fixing problems Important rule while writing programs and to avoid debugging: Incremental Code Development Incremental code development (write a little bit, test thoroughly and continue) Suggestions about writing computer programs can be found at: http://www.cs.umd.edu/~nelson/documents/SuggestionsForWritingComp uterPrograms.htm