-3
String b, h;
char b1, h1;
    do
    {
        b = JOptionPane.showInputDialog(null, "Enter the base of the triangle.");
        h = JOptionPane.showInputDialog(null, "Enter the height of the triangle.");
        b1 = b.charAt(0);
        h1 = h.charAt(0);
        if (b1 >= '0' && b1 <= '9' && h1 >= '0' && h1 <= '9') //This if statement will only execute the program if a number is entered
        {
        double base = Double.parseDouble(b);
        double height = Double.parseDouble(h);
        double area = (base*height)/2;
        JOptionPane.showMessageDialog(null, "The area of a triangle with a base of " + base + " inches and a height of " 
        + height + " inches is " + area + " inches.");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "One or more of the values you entered is not a number. Please enter a number.");
        }
        }
        while (!(b1 >= '0' && b1 <= '9' && h1 >= '0' && h1 <= '9')); /*Causes the statement to keep asking for a value until a number
                                                                        is entered*/
        break;

I have to write a program that will give the user the area or volume of a shape, depending on what they input/choose. The section above is for the area of a triangle. The program will keep asking for the value of the base and height until both values inputted are numbers. The only time an error comes is when the user clicks "OK" on the dialog box, without entering any value. The source of the error seems to be when the program attempts to convert the string to a char. How can I make it to where clicking "OK" without entering a value will not result in an error?

Here is the error itself

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String 
index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at Shapes.area(Shapes.java:40)
    at Shapes.main(Shapes.java:11)
  • 1
    Hi welcome to Stackoverflow! I think you can improve you're question by adding the output from the runtime error you are getting, so folks viewing can see which part of the code is throwing which exception. – strider Nov 11 '17 at 18:42
  • 1
    Hint: look into your naming. A name should say what the thing it names is about. Names such as h, b, h1... Are plain horrible. – GhostCat Nov 11 '17 at 18:55
  • @bitstrider I appreciate the feedback! My apologies for anything I did wrong (ie. duplicate posts, etc.) I'm very new to programming and am still trying to learn how a lot of it works. – Robert Green Nov 11 '17 at 19:11
  • @GhostCat I will take your advice and make some better names – Robert Green Nov 11 '17 at 19:11

2 Answers2

0

You test the input of the user, i mean should be sure that the string entered by the user isn't empty. I think that you should add this test

0

Check the length of the String before doing computation. Throw an exception if it does not meet your requirements.

import javax.swing.JOptionPane;

public class F {

  public static void main(String[] args) throws Exception {

    try {

      String b = JOptionPane.showInputDialog(null, "Enter base");

      // Check that the String has at least 1 character
      if (b.length() < 1 ) {

        // If it doesnt, throw an error
        throw new Exception("Base cannot be null value");
      }

      // Otherwise we continue
      JOptionPane.showMessageDialog(null, "Base is " + b);

      // Catch the exception
    } catch (Exception exception) {

      // Notify the use about the issues
      JOptionPane.showMessageDialog(null, exception.getMessage());

      // TODO - implement more logic, like asking user for input again
    }
  }
}