* AddArguments :-
public class AddArguments {
public static void main (String args[]) {
int sum = 0;
for ( String arg : args ) {
sum += Integer.parseInt(arg);
}
System.out.println("Sum = " + sum);
}
}
public class AddArguments {
public static void main (String args[]) {
int sum = 0;
for ( String arg : args ) {
sum += Integer.parseInt(arg);
}
System.out.println("Sum = " + sum);
}
}
* AddArguments 2 :-
public class AddArguments2 {
public static void main (String args[]) {
try {
int sum = 0;
for ( String arg : args ) {
sum += Integer.parseInt(arg);
}
System.out.println("Sum = " + sum);
} catch (NumberFormatException nfe) {
System.err.println("One of the command-line "
+ "arguments is not an integer.");
}
}
}
* AddArguments 3 :-
public class AddArguments3 {
public static void main (String args[]) {
int sum = 0;
for ( String arg : args ) {
try {
sum += Integer.parseInt(arg);
} catch (NumberFormatException nfe) {
System.err.println("[" + arg + "] is not an integer"
+ " and will not be included in the sum.");
}
}
System.out.println("Sum = " + sum);
}
}
* HelloWorld Code :-
public class HelloWorld {
public static void main (String args[]) {
int i = 0;
String greetings [] = {
"Hello world!",
"No, I mean it!",
"HELLO WORLD!!"
};
while (i < 4) {
try {
System.out.println (greetings[i]);
i++;
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Re-setting Index Value");
i = 0;
} finally {
System.out.println("This is always printed");
}
}
}
}
No comments:
Post a Comment