String - The College of Saint Rose

Download Report

Transcript String - The College of Saint Rose

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....
public static int void main(String[] args)
{
System.out.println("Quotes come in twos");
System.out.println("Answer: " + (9 + 5));
}
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:
int result;
result = 1 / 2;

Don’t use a variable unless it has been declared and
assigned a value:
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:
final int monthsInYear = 12;
final double ourPI = 3.14159;
final double rate = 0.0675;
monthsInYear = 11; // ERROR!
rate = 0.07; // ERROR!

Why use final?
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:
double dollarAmt = 42.92839;
DecimalFormat df = new DecimalFormat( "0.00" );
System.out.print( "Cost is $" );
System.out.println( df.format( dollarAmt ) );

Does formatting a number round the number?
ASCII & Unicode Characters

Character data types are encoded as numbers
ASCII is the
American Standard Code
for Information Interchange

Java uses two
bytes to store
characters as
Unicode
Representing Characters in Java

Characters (e.g. letters, digits, symbols) are
represented using the char data type
char c, d, e;
c = '4';
d = 'u';
e = '!';
a character value must
be enclosed in single quotes
single character … single quotes
System.out.print(c);
System.out.print(d);
System.out.print(e);
output
4u!
Character Strings

A group of characters is called a character string

String literals are enclosed in double quotation marks:
System.out.print( "This is a string" );

The String class in Java allows you to create character
string variables:
String s1 = "This is a string";
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):
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." );