i am trying to use trys and catches (exception handling), but it doesnt seem to work correctly. I got the following code:
Code:
import javax.swing.*;
public class brand
{
public static void main (String[] args)
{
String amount, input;
int number=0, total, again;
do
{
amount = JOptionPane.showInputDialog(null, "How many numbers do you want to enter");
try
{
number = Integer.parseInt(amount);
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Error: You did not enter a number");
}
int[] array = new int[number];
for(int index=0; index < array.length; index++)
{
input = JOptionPane.showInputDialog(null, "Enter a positive integer number" + (index +1));
try
{
array[index] = Integer.parseInt(input);
while(isPositive(array[index]) == false)
{
array[index] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a positive integer number" + (index +1)));
}
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Error: You did not enter a positive integer number");
}
}
for(int index=0; index<array.length; index++)
{
total = addNumbers(array[index]);
JOptionPane.showMessageDialog(null, "The total of integers between 1 and " + array[index] + " is " + total);
}
again = JOptionPane.showConfirmDialog(null, "Do you want to continue?");
}
while(again == JOptionPane.YES_OPTION);
}
public static boolean isPositive(int number)
{
if(number>0)
return true;
else
return false;
}
public static int addNumbers(int number)
{
int total=0;
int count=0;
while(count<number)
{
count = count + 1;
total = total + count;
}
return total;
}
}
Now the try and catches dont work. they catch the error yes but they dont like go back so i can repeat my entry. They just go to the next event to be done in ma program.
What am i doin wrong and what should i do to change it??
THANKS