d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Intro To Java Try/catch Exceptions
Add Reply New Topic New Poll
Member
Posts: 22,153
Joined: May 30 2007
Gold: 6,662.22
Apr 9 2014 03:52am
I'm having trouble finding where I should place my try/catch/finally blocks when I'm trying to catch an exception from the program's parameters. I would appreciate any help.

Code
import java.util.*;
import java.io.*;

class hw8410 {

public static void main(String args[]) {

Scanner in = new Scanner(System.in);
int numElements = Integer.parseInt(args[0]);

try { //this is the try block that I don't know where to place

if (numElements <= 0) {
System.out.println("Array size must be positive");
System.exit(0);
}

} catch (NumberFormatException e) { //and the catch that follows
System.out.println("Please enter an integer for the Command Line Parameter!");
System.exit(0);
}

double[] array1 = new double[numElements];
double[] array2 = new double[numElements];

try {

for (int i = 0; i < array1.length; i++) {
System.out.println("Please enter a double for array1[" + i + "] and one for array2[" + i + "]:");
array1[i] = in.nextInt();
array2[i] = in.nextInt();
}

} catch (InputMismatchException e) {
System.out.println("Please enter double values!");
System.exit(0);
}

System.out.println("Array1 max: <" + maxArray(array1) + ">, Array2 max: <" + maxArray(array2) + ">.");
sumDiffArrays(array1, array2);

}

static double maxArray(double array[]) {
double max = 0;
for (int i = 0; i < array.length; i++) {
if (max < array[i]) {
max = array[i];
}
}
return max;
}

static void sumDiffArrays(double array3[], double array4[]) {
double diff = 0;
double sum = 0;
for (int i = 0; i < array3.length; i++) {
diff = ((array3[i]) - (array4[i]));
sum += diff;
}
System.out.println(sum);
}
}


The try/catch block for the array user input works properly and catches the InputMismatchException, but I can't get the try block to catch an exception for the program's parameter input. When I try to place the try block before I initialize numElements, I get errors while compiling that saying it cannot find numElements.

This post was edited by tehmilk on Apr 9 2014 03:56am
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 9 2014 04:46am
Integer.parseInt(String) parses a string into an integer. If something goes wrong there, then you have a NumberFormatException because the string has an ill-formatted integer in it.

Knowing that, where would the exception be thrown from?

/e Also, you might want to change in.nextInt() to in.nextDouble()

This post was edited by poodaH on Apr 9 2014 04:50am
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Apr 9 2014 04:48am
declare your numElements int before the try / catch and put numElements = Integer.parseInt(args[0]); in the try/catch
Member
Posts: 22,153
Joined: May 30 2007
Gold: 6,662.22
Apr 9 2014 05:04am
Quote (poodaH @ Apr 9 2014 05:46am)
Integer.parseInt(String) parses a string into an integer. If something goes wrong there, then you have a NumberFormatException because the string has an ill-formatted integer in it.

Knowing that, where would the exception be thrown from?

/e Also, you might want to change in.nextInt() to in.nextDouble()


Quote (m0hawk @ Apr 9 2014 05:48am)
declare your numElements int before the try / catch and put numElements = Integer.parseInt(args[0]); in the try/catch


thank you both for the help, I've fixed my issue =].
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll