Download presentation
Presentation is loading. Please wait.
Published byMartha Warner Modified over 8 years ago
1
C OMMON M ISTAKES CSC 111 - Java Program Structure String Methods
2
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
3
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
4
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
5
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
6
Variables Complete division public class Revision{ public static void main (String [] args){ int init_price = 150; int sale = 15; double final_price = 150 - (150 * 15 / 100.00); System.out.println("The price is " + final_price); } } OUTPUT: The price is 127.5 SR By: Arwa Alturki6
7
Variables Integer division public class Revision{ public static void main (String [] args){ int init_price = 150; int sale = 15; double final_price = 150 - (150 * 15 / 100); System.out.println("The price is " + final_price); } } OUTPUT: The price is 128 SR By: Arwa Alturki7
8
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
9
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
10
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
11
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
12
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
13
Output Use comma (,) NOT plus (+) in printf public class Revision{ public static void main (String [] args){ int year = 2014; double days = 365.25; 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 = 365.25; System.out.printf("We are on %d where there are %.2f days" + year + days);} } ✓ ✗ By: Arwa Alturki13
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
Q UESTIONS ?! By: Arwa Alturki22
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.