public class Demo { public static void main(String[] args) { String[] name = {"John Doe", "Mary Jane", "Peter Pan"}; /* ---------------------------------------------- Print the String array name ---------------------------------------------- */ for ( int i = 0; i < name.length; i++ ) { System.out.println( name[i] ); } } } |
Notes:
|
|
|
|
![]() |
public class Argument1 { public static void main(String[] args) { for ( int i = 0; i < args.length; i++ ) System.out.println( args[i] ); } } |
How to run the program:
|
Sample outputs:
UNIX Prompt>> java Argument1 a b c a b c UNIX Prompt>> java Argument1 1 2 3 4 1 2 3 4 |
Explanation:
|
|
Pseudo code:
sum = 0; for ( every element in args[i] ) do { sum = sum + args[0]; } Print sum; |
public class Sum1 { public static void main(String[] args) { int sum; int i; sum = 0; // Initialize the running sum for ( i = 0; i < args.length; i++ ) { sum = sum + args[i]; // Add args[i] to running sum } System.out.println( sum ); } } |
Question:
|
How to run the program:
|
What is the compiler trying to tell you:
public class Sum1 { public static void main(String[] args) { int sum; int i; sum = 0; // Initialize the running sum for ( i = 0; i < args.length; i++ ) { sum = sum + args[i]; // Add args[i] to running sum ^^^ ^^^ ^^^^^^^ int int String ** The Java compiler found: int + String ** The compiler requires: int + int } System.out.println( sum ); } } |
|
The parseInt method is invoked as follows:
Integer.parseInt( NumericString ) |
The parseInt method returns an integer value of the numeric string
public class ParseInt { public static void main(String[] args) { String a = "12"; String b = "34"; System.out.println( a + b ); // a + b = "12" + "34" = "1234" // Because + = concatenate when used on strings System.out.println( Integer.parseInt(a) + Integer.parseInt(b) ); // Integer.parseInt(a) + Integer.parseInt(b) // = Integer.parseInt("12") + Integer.parseInt("34") // = 12 + 34 = 46 // Because + = ADD when used on integers } } |
Output:
1234 46 |
How to run the program:
|
public class Sum1 { public static void main(String[] args) { int sum; int i; sum = 0; // Initialize the running sum for ( i = 0; i < args.length; i++ ) { sum = sum + args[i]; // Add args[i] to running sum ^^^^^^^ We need to convert the String to an integer } System.out.println( sum ); } } |
public class Sum2 { public static void main(String[] args) { int sum; int i; sum = 0; // Initialize the running sum for ( i = 0; i < args.length; i++ ) { sum = sum + Integer.parseInt(args[i]); // Add args[i] to running sum } System.out.println( sum ); } } |
How to run the program:
|
Example outputs:
cheung@HOME2(17)> java Sum2 1 2 3 cheung@HOME2(18)> java Sum2 1 2 3 4 5 6 7 28 cheung@HOME2(20)> java Sum2 3 4 7 14 |
The parseDouble method is invoked as follows:
Double.parseDouble( NumericString ) |
The parseDouble method returns an double precision value of the numeric string
public class ParseDouble { public static void main(String[] args) { String a = "12.34"; String b = "56.78"; System.out.println( a + b ); // a + b = "12.34" + "56.78" = "12.3456.78" // Because + = concatenate when used on strings System.out.println( Double.parseDouble(a) + Double.parseDouble(b) ); // Double.parseDouble(a) + Double.parseDouble(b) // = Double.parseDouble("12.34") + Double.parseDouble("56.78") // = 12.34 + 56.78 = 69.12 // Because + = ADD when used on doubles } } |
Output:
12.3456.78 69.12 |
How to run the program:
|
|
|
public class ParseInt2 { public static void main(String[] args) { String a = "12x"; // NOT an integer !!! String b = "34"; System.out.println( a + b ); System.out.println( Integer.parseInt(a) + Integer.parseInt(b) ); // CRASH !!! } } |
How to run the program:
|
Output:
cheung@HOME2(26)> java ParseInt2 12x34 Exception in thread "main" java.lang.NumberFormatException: For input string: "12x" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:456) at java.lang.Integer.parseInt(Integer.java:497) at ParseInt2.main(ParseInt2.java:12) |