c ommon m istakes csc 111 - java program structure string methods

Post on 19-Jan-2018

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

COMMON MISTAKES

CSC 111 - JavaProgram Structure String Methods

By: Arwa Alturki 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 Alturki 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 Alturki 4

Variables

• Preserve naming convention• Constant variablepublic 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 Alturki 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 AThe number is 68.5

By: Arwa Alturki 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 Alturki 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 Alturki 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 Alturki 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 Alturki 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 Alturki 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 Alturki 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 Alturki 13

Output

• Use comma (,) NOT plus (+) in printfpublic 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 Alturki 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 Alturki 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 Alturki 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 Alturki 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 Alturki 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 Alturki 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 Alturki 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 Alturki 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 Alturki 22

QUESTIONS ?!

Company Logo

top related