C OMMON M ISTAKES CSC 111 - Java Program Structure  String Methods.

Slides:



Advertisements
Similar presentations
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
Advertisements

Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Java Programming Strings Chapter 7.
String StringBuffer. class StringExample { public static void main (String[] args) { String str1 = "Seize the day"; String str2 = new String(); String.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
CS 180 Recitation 8 / {30, 31} / Announcements Project 1 is out –Due 10:00pm –Start early! –Use the newsgroup for your questions –LWSN B146.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
LAB 10.
Computer Programming Lab(4).
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Integer Division What is x? int x = 1729 / 100; Answer: 17.
Java for Beginners University Greenwich Computing At School DASCO
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Objects and Classes; Strings. 2 Classes and objects class: A program entity that represents either 1.A program / module, or 2.A type of objects* –A class.
Chapter 2 Elementary Programming
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
EXAM 1 REVIEW. days until the AP Computer Science test.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. CHAPTER 3: SELECTIONS 1.
Chapter 2 – Continued Basic Elements of Java. Chapter Objectives Type Conversion String Class Commonly Used String Methods Parsing Numeric Strings Commonly.
Chapter 2: Java Fundamentals Type conversion,String.
Java Fundamentals 5. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
ROUND 1 Name a method associated with class String 1.) 15 compareTo() 26 indexOf() 34 length() 2.) 3.) 4.) 3 toUpper() 7 substring() 11 charAt() 5.)
CS1101: Programming Methodology Aaron Tan.
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
1 CS 177 Week 3 Recitation Slides Basic Math Operations, Booleans, and Character Operations.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Dialog Boxes.
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.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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.
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
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Introduction to programming in java
(Dreaded) Quiz 2 Next Monday.
Java Fundamentals 4.
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 2 Clarifications
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
Elementary Programming
Conditional Execution
Interactive Standard Input/output
Computer Programming Methodology Input and While Loop
Data types, Expressions and assignment, Input from User
CS 177 Week 3 Recitation Slides
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Java Variables, Types, and Math Getting Started
Introduction to Computer Science and Object-Oriented Programming
CS2011 Introduction to Programming I Strings
Strings CSE 1310 – Introduction to Computers and Programming
Optional Topic: User Input with Scanner
Presentation transcript:

C OMMON M ISTAKES CSC Java Program Structure  String Methods

Program Structure Read and understand the problem In all programs their should be: – Named class – Main method import java.util.*; public class Revision{ public static void main (String [] args){ Scanner input = new Scanner(System.in); } } By: Arwa Alturki2

Program Structure Brackets after both of class and main method import java.util.*; public class Revision{ public static void main (String [] args){ Scanner input = new Scanner(System.in); } } import java.util.*; public class Revision; public static void main (String [] args); Scanner input = new Scanner(System.in); } } ✓ ✗ By: Arwa Alturki3

Variables Preserve naming convention Constant variable public class Revision{ public static void main (String [] args){ final double DOLLAR_PRICE = 3.75; } } public class Revision; public static void main (String [] args); double DOLLAR_PRICE = 3.75; } } ✓ ✗ ? By: Arwa Alturki4

Variables Casting variable DO NOT change its type public class Revision{ public static void main (String [] args){ char ch = 'A'; double num = (double) ch + 3.5; System.out.println("The charachter is " + ch); System.out.println("The number is " + num); } } OUTPUT: The character is A The number is 68.5 By: Arwa Alturki5

Variables Complete division public class Revision{ public static void main (String [] args){ int init_price = 150; int sale = 15; double final_price = (150 * 15 / ); System.out.println("The price is " + final_price); } } OUTPUT: The price is SR By: Arwa Alturki6

Variables Integer division public class Revision{ public static void main (String [] args){ int init_price = 150; int sale = 15; double final_price = (150 * 15 / 100); System.out.println("The price is " + final_price); } } OUTPUT: The price is 128 SR By: Arwa Alturki7

Input Display message to the user before reading import java.util.*; public class Revision{ public static void main (String [] args){ Scanner input = new Scanner(System.in); int ID; String name; System.out.print("Enter your name: "); name = input.next(); System.out.print("Enter your ID: "); ID = input.nextInt(); } } ✓ By: Arwa Alturki8

Input Display message to the user before reading import java.util.*; public class Revision{ public static void main (String [] args){ Scanner input = new Scanner(System.in); int ID; String name; name = input.next(); System.out.print("Enter your name: "); ID = input.nextInt(); } } ✗ ? By: Arwa Alturki9

Input One Scanner object for all readings import java.util.*; public class Revision{ public static void main (String [] args){ Scanner input = new Scanner(System.in); int ID; String name; System.out.print("Enter your name: "); name = input.next(); System.out.print("Enter your ID: "); ID = input.nextInt(); } } ✓ By: Arwa Alturki10

Input One Scanner object for all readings import java.util.*; public class Revision{ public static void main (String [] args){ Scanner input1 = new Scanner(System.in); Scanner input2 = new Scanner(System.in); int ID; String name; System.out.print("Enter your name: "); name = input1.next(); System.out.print("Enter your ID: "); ID = input2.nextInt(); } } ✗ By: Arwa Alturki11

Input Use nextLine method to read a line of strings – next method read until the first space in the line import java.util.*; public class Revision{ public static void main (String [] args){ Scanner input = new Scanner(System.in); System.out.print("Enter your full name: "); String str = input.nextLine(); } } By: Arwa Alturki12

Output Use comma (,) NOT plus (+) in printf public class Revision{ public static void main (String [] args){ int year = 2014; double days = ; System.out.printf("We are on %d where there are %.2f days", year, days);} } public class Revision{ public static void main (String [] args){ int year = 2014; double days = ; System.out.printf("We are on %d where there are %.2f days" + year + days);} } ✓ ✗ By: Arwa Alturki13

String Operations Make your solution general as possible as you can – Use the given format import java.util.*; public class Revision{ public static void main (String [] args){ Scanner input = new Scanner(System.in); System.out.print("Enter the time as hh:mm: "); String str = input.next(); int index = str.indexOf(':'); String h = str.substring(0,index); String m = str.substring(index+1); System.out.print("The hour now is “+h+" and “+m); } } ✓ By: Arwa Alturki14

String Operations Make your solution general as possible as you can – Use the given format import java.util.*; public class Revision{ public static void main (String [] args){ Scanner input = new Scanner(System.in); System.out.print("Enter the time as hh:mm: "); String str = input.next(); String h = str.substring(0,2); String m = str.substring(3); System.out.print("The hour now is “+h+" and “+m); } } ✗ By: Arwa Alturki15

String Operations Str.IndexOf(char) public class Revision{ public static void main (String [] args){ String str = "1:15"; int index = str.indexOf(':'); } } public class Revision{ public static void main (String [] args){ String str = "1:15"; int index = str.indexOf(:); } } ✓ ✗ By: Arwa Alturki16

String Operations Str.IndexOf(char, index to start from) public class Revision{ public static void main (String [] args){ String str = "1:15:30"; int index1 = str.indexOf(':'); int index2 = str.indexOf(':', index1+1); } } public class Revision{ public static void main (String [] args){ String str = "1:15:30"; int len = str.length(); int index1 = str.indexOf(':'); int index2 = str.indexOf(':', len); } } ✓ ✗ By: Arwa Alturki17

String Operation Parsing (convert string to number) public class Revision{ public static void main (String [] args){ String str = "135"; int num1 = Integer.parseInt(str); double num2 = Double.parseDouble("125.75"); } } public class Revision{ public static void main (String [] args){ String str = "135"; int num1 = Integer.parseInt("str"); double num2 = Double.parseDouble(125.75); } } ✓ ✗ By: Arwa Alturki18

String Operations Remember: – indexOf method returns int value, which is the index of the given character – charAt method returns char value, which is the character at the given index – Substring method return string NOT numbers By: Arwa Alturki19

Control Structure (If/Else) Use compareTo or equals method to compare two strings public class Revision{ public static void main (String [] args){ String str = "Test String Comparison"; if (str.compareTo("Test String") == 0) System.out.println("Similar strings");} } public class Revision{ public static void main (String [] args){ String str = "Test String Comparison"; if (str.equals("Test String")) System.out.println("Similar strings");} } ✓ ✓ By: Arwa Alturki20

Control Structure (If/Else) Use compareTo or equals method to compare two strings public class Revision{ public static void main (String [] args){ String str = "Test String Comarision"; if (str == "Test String") System.out.println("Similar strings"); } } ✗ By: Arwa Alturki21

Q UESTIONS ?! By: Arwa Alturki22