Hello,

I am to write a program that is capable of taking an array of doubles from the user, then calculating each numbers square, cube etc. and then displaying it to the screen. The program itself is fine, but one aspect of the program is that if a user enters something other than an integer, no error message needs to be displayed but just a re-prompt box.

I tried putting in a try and catch blocks, but I notice that when I make the condition of the catch statement to reprompt for the number, when the user enters it, the program jumps outside the catch block (as it should) and the program ends.

Is there a way I can make it jump back to the top of the code if NumberFormatException is thrown?

Here is my code:

import java.text.DecimalFormat;
import java.math.*;
import javax.swing.*;
public class SquareRootCube
{
    public static void main (String[]args)
    {
         
        
        try
         {
         String indexAmount = JOptionPane.showInputDialog (null, "Please enter the amount of numbers you will enter");
         int pIndex = Integer.parseInt(indexAmount);
         
         
        
           
                while (pIndex <= 0)
                {
                JOptionPane.showMessageDialog (null, "That integer is not valid. Enter an integer greater than 0");
                indexAmount=JOptionPane.showInputDialog(null, "Enter the amount of numbers you will enter");
                pIndex = Integer.parseInt(indexAmount);
                }
                
                double numbers[] = new double[pIndex];
                int index = 0;
         
            for (index=0; index < pIndex; index++)
            {
                String sNumber = JOptionPane.showInputDialog (null, "Please enter an integer between 25 and 150");
                    
                numbers[index]= Integer.parseInt(sNumber);
                while (numbers[index] < 25 || numbers[index] > 150)
                {
                    JOptionPane.showMessageDialog (null, "That is not a valid number.");
                    sNumber = JOptionPane.showInputDialog (null, "Please enter an integer between 25 and 150");
                    numbers[index]= Integer.parseInt(sNumber);
                }
                    
            }
            
           double squared, squareRoot, cubed;
           
           for (index=0; index < pIndex; index++)
           {
           DecimalFormat formatter = new DecimalFormat(",###.00");
           
           
           
           squared = numbers[index] * numbers[index];
           squareRoot = Math.sqrt (numbers[index]);
           cubed = numbers[index] * numbers[index] * numbers[index];
           
           String fSquared = formatter.format(squared);
           String fSquareRoot = formatter.format(squareRoot);
           String fCubed = formatter.format(cubed);
          
           
           
        
           JOptionPane.showMessageDialog (null, numbers[index] + " squared is " + fSquared + "\n" + numbers[index] + " square root is " +fSquareRoot + "\n" + numbers[index] + " cubed is " + fCubed);
           } 
           System.exit(0);
        }
            catch (NumberFormatException e)
         {
            sNumber = JOptionPane.showInputDialog (null, "Please enter an integer between 25 and 150");
            numbers[index]= Integer.parseInt(sNumber);
         }
        }
   
      
     }

Thanks in advance :)

Recommended Answers

All 4 Replies

Try making a "smaller" try/catch block (i.e. only around the "pIndex =" line).

Edit: And, that way, you can, of course, simply do nothing in the catch block (although you would, normally, at least log the fact that the exception occurred, even if you don't do anything about it).

Hey thanks for the advice,

But when I put the try/catch block only around pIndex, the rest of my code that requires pIndex to evaluate conditional loops. doesn't recognise that pIndex has been defined.

Should I define pIndex outside the try/catch block?

Edit: nevermind, I can't do that because it is defined by parsing a string which is above the try block..

Basically, the requirement is that the catch block contains the message to re-prompt for the number. But what if the user then trys to type illegal characters again? It will cause another exception but there is no try/catch block there to stop it. Essentially you'd have to do an infinite number of try/catch blocks. Is there any way to just loop one?

here then

change

String indexAmount = JOptionPane.showInputDialog (null, "Please enter the amount of numbers you will enter");
         int pIndex = Integer.parseInt(indexAmount);

                while (pIndex <= 0)
                {
                JOptionPane.showMessageDialog (null, "That integer is not valid. Enter an integer greater than 0");
                indexAmount=JOptionPane.showInputDialog(null, "Enter the amount of numbers you will enter");
                pIndex = Integer.parseInt(indexAmount);
                }

to

int pIndex = 0;
         while (pIndex <= 0) {
             String indexAmount = JOptionPane.showInputDialog (null, "Please enter the amount of numbers you will enter");
             try {
                 pIndex = Integer.parseInt(indexAmount);
             } catch (NumberFormatException nfe) {
                 pIndex = 0;  // just to be sure
             }
         }

Thats done it!

Thanks alot Masijade :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.