Introduction to Programming Java Fundamentals (Numbers, Characters, Strings) David Goldschmidt, Ph.D. Computer Science The College of Saint Rose
Common Programming Errors Examples of programming pitfalls: Don’t rush to writing code until you have sketched out an algorithm or solution Create backups of your code using flash drives, etc. Make sure every beginning has an end.... parentheses public static int void main(String[] args) { System.out.println("Quotes come in twos"); System.out.println("Answer: " + (9 + 5)); } braces quotes
Common Programming Errors More examples of programming pitfalls: Watch for misspelled Java keywords Java is a case-sensitive language, so be careful of uppercase and lowercase letters Don’t forget to end statements with a semicolon (;) Or use { to open a new block of code Watch for incompatible data types when you assign values to variables: int x; x = 928.502;
Common Programming Errors Even more examples of programming pitfalls: Watch out for integer division: Don’t use a variable unless it has been declared and assigned a value: int result; result = 1 / 2; q = 928.502; int x; System.out.println(x);
Representing Numeric Constants Use the final keyword when you declare a variable to ensure its value cannot change: Why use final? final int monthsInYear = 12; final double ourPI = 3.14159; final double rate = 0.0675; monthsInYear = 11; // ERROR! rate = 0.07; // ERROR!
Formatting Numeric Output When displaying floating-point numbers (i.e. data types float and double), you often want to format the output e.g. instead of displaying $42.934439, we would prefer to display $42.93 To do so: Use the DecimalFormat class, which is part of the Java library Create a DecimalFormat format pattern Apply the pattern to the number to be formatted
Formatting Numeric Output To use the DecimalFormat class, we first need to import the library via the import directive: import java.text.DecimalFormat; public class Name { ...
Formatting Numeric Output Next, we create a DecimalFormat pattern by adding the following code to the main method: Does formatting a number round the number? double dollarAmt = 42.92839; DecimalFormat df = new DecimalFormat( "0.00" ); System.out.print( "Cost is $" ); System.out.println( df.format( dollarAmt ) );
American Standard Code for Information Interchange ASCII & Unicode Characters Character data types are encoded as numbers Java uses two bytes to store characters as Unicode ASCII is the American Standard Code for Information Interchange
be enclosed in single quotes Representing Characters in Java Characters (e.g. letters, digits, symbols) are represented using the char data type a character value must be enclosed in single quotes char c, d, e; c = '4'; d = 'u'; e = '!'; System.out.print(c); System.out.print(d); System.out.print(e); single character … single quotes 4u! output
Character Strings A group of characters is called a character string String literals are enclosed in double quotation marks: The String class in Java allows you to create character string variables: System.out.print( "This is a string" ); String s1 = "This is a string";
(i.e. create a String object) Character Strings In Java, String objects are similar to variables: String s1 = "This is a string"; Declare variable s1 of type String (i.e. create a String object) Initialize variable s1
Character Strings We can use print and println to display String objects in Java: String firstName = "David"; String lastName = "Goldschmidt"; System.out.print( "Hello, " ); System.out.println( firstName ); System.out.print( "Bye Dr. " ); System.out.println( lastName );
The String Class & Methods Once we have a String object created, we can use various methods of the String class Determine the length of a String: String firstName = "David"; String lastName = "Goldschmidt"; int len = lastName.length(); System.out.print( "Last name has " ); System.out.println( len + " letters." );
The String Class & Methods Once we have a String object created, we can use various methods of the String class Convert a String to uppercase or lowercase: String s = "Java is so cool"; System.out.println( s.toUpperCase() ); String lower = s.toLowerCase(); System.out.println( lower );
The String Class & Methods Once we have a String object created, we can use various methods of the String class Obtain individual characters from the String: Position (or index) starts at zero String s = "Java is so cool"; char firstLetter = s.charAt( 0 ); char thirdLetter = s.charAt( 2 ); System.out.print( firstLetter ); System.out.println( thirdLetter );
Reading Keyboard Input A running Java program can have both output (System.out) and input (System.in): System.out System.in Java Program Execution
Reading Keyboard Input To read input from the keyboard, we use Java’s Scanner class Create a Scanner object associated with System.in: Scanner keyboard = new Scanner( System.in ); Declare variable keyboard of type Scanner Create a Scanner object and connect to System.in
Reading Keyboard Input Once created, use the Scanner object to read keyboard input: Scanner keyboard = new Scanner( System.in ); System.out.println( "What's your name?" ); String name = keyboard.nextLine(); System.out.println( "How old are you?" ); int age = keyboard.nextInt();
Reading Keyboard Input To use the Scanner class, we need to import in the class from the java.util package: import java.util.Scanner; public class NameGame { public static void main( String[] args ) ...
Dialog Boxes Use a dialog box to display a message graphically: import javax.swing.JOptionPane; public class DialogBoxes { public static void main( String[] args ) JOptionPane.showMessageDialog( null, "This is called a dialog box." );