Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: JOptionPane Stops working after user input

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Location
    Atlanta, Georgia
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JOptionPane Stops working after user input

    It works to the point that the user inputs "c" "s" or "r" then it does nothing else ? I can get it to run in my terminal window but when I try to get the JoptionPane windows to work it freezes up.



      import java.util.*; // For Scanner
    import java.lang.String.*; // For toUpperCase()
    import javax.swing.JOptionPane; // for cute lil java windows
     
    public class CaculateAreaOpt {
      // Declare all variables needed for program
      public static Scanner in = new Scanner(System.in);
      public final static double radius = 3.14;
      public static char input;
      public static double area;
      public static double circumference;
      public static double length;
      public static double height;
      public static String str, error;
     
      public static double getArea(double x, double y) {
      // Char inputted by user then each case evaluates area
    if (input == 'C') {
        JOptionPane.showInputDialog(
            null,"Enter circumference");
          circumference = in.nextDouble();
          area = radius * circumference;
    } // end input c
     
     
     else if (input == 'R'){
          JOptionPane.showInputDialog(
            null,"Enter length of rectangle");
          length = in.nextDouble();
     
       JOptionPane.showInputDialog( 
       null,"Enter height of rectangle");
          height = in.nextDouble();
          area = length * height;
      }// end input R
     
        else if (input == 'S') {
         JOptionPane.showInputDialog(
            null, "Enter length of side");
            length = in.nextDouble();
          area = length * length;
    // end input S
      }// end input S
        // return the area
        return area;
      }
     
      /*
       * Method takes two double arguments
       */
     
      public static void main(String[] args) {
        // Get User to choose what shape to calculate Area of
     
    JOptionPane.showInputDialog(
            null,"Calculate the Area of a Square, Rectangle or Circle-Press S for square, R for Rectangle, or C for Circle");
        // String str takes Scanner in (next() value
        str = in.next();
        /* String str is an array, so index 0 is first character
         * Thus index 0 is first character
         * Character.toUpperCase converts the String to uppercase
         * so you don't have to test for c, s, or r instead of C, S, R
         */
        input = Character.toUpperCase(str.charAt(0));
     
        /* if / else if 
         * call the getArea() 
         */
        if (input == 'C') {
          getArea(radius, circumference);
       JOptionPane.showInputDialog(
            null,"The area of your circle is " + area);
        } // end area c
        else if (input == 'S') {
          getArea(length, length);
       JOptionPane.showInputDialog(
            null,"The area of your square is " + area);
        } // end area s
        else if (input == 'R') {
          getArea(length, height);
     
       JOptionPane.showInputDialog(
            null,"The area of your rectangle is " + area);
        } // end area r
        else {
          System.out.println(error);
        } // end error
      } //end main()
    } // end class


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JOptionPane Stops working after user input

    Mixing dialog windows and console input must be very confusing for the user. It would be better to use one or the other.

    it freezes up
    Try adding lots of println() statements to the code to show its execution progress so you can find the statement in the code where it "freezes". Key places would be after each place input is being read from the user.



    You need to read the API doc for the JOPtionPane and its methods that you are using.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Location
    Atlanta, Georgia
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane Stops working after user input

    when i use the println() it works perfectly but I am supposed to use the JOptionPane

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JOptionPane Stops working after user input

    Have you read the API doc for the JOptionPane class and its methods?
    Many methods return what the user has entered.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Location
    Atlanta, Georgia
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane Stops working after user input

    It stops when the user enters the choice "c" "r" or "s"
    I put the JOptionPane on the error statement at the end.. still its stopping and not coming up with any kind of an error

    --- Update ---

    It stops when the user enters the choice "c" "r" or "s"
    I put the JOptionPane on the error statement at the end.. still its stopping and not coming up with any kind of an error

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JOptionPane Stops working after user input

    Can you post the new code that uses what the JOptionPane methods return and does not use Scanner methods?

    its stopping and not coming up with any kind of an error
    Try debugging the code by adding some println() statements to show the value of variables as they change and the execution flow.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Location
    Atlanta, Georgia
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Still stalling

    import java.util.*; // For Scanner
    import java.lang.String.*; // For toUpperCase()
    import javax.swing.JOptionPane; // for cute lil java windows
     
    public class CaculateAreaOpt {
      // Declare all variables needed for program
      public static Scanner in = new Scanner(System.in);
      public final static double radius = 3.14;
      public static char input;
      public static double area;
      public static double circumference;
      public static double length;
      public static double height;
      public static String str, error;
     
      public static double getArea(double x, double y) {
      // Char inputted by user then evaluates area
    if (input == 'C') {
        JOptionPane.showInputDialog(
            null,"Enter circumference");
          circumference = in.nextDouble();
          area = radius * circumference;
    } // end input c
     
     
     else if (input == 'R'){
         JOptionPane.showInputDialog(
            null,"Enter length of rectangle");
          length = in.nextDouble();
     
       JOptionPane.showInputDialog( 
       null,"Enter height of rectangle");
          height = in.nextDouble();
          area = length * height;
      }// end input R
     
        else if (input == 'S') {
        JOptionPane.showInputDialogg(
            null, "Enter length of side");
            length = in.nextDouble();
          area = length * length;
    // end input S
      }// end input S
        // return the area
        return area;
      }
     
      /*
       * Method takes two double arguments
       */
     
      public static void main(String[] args) {
        // Get User to choose what shape to calculate Area of
     
    JOptionPane.showInputDialog(
            null,"Calculate the Area of a Square, Rectangle or Circle-Press S for square, R for Rectangle, or C for Circle");
        // String str takes Scanner in (next() value
        str = in.next();
        /* String str is an array
         *  0 is first character
         * Character.toUpperCase converts the String to uppercase
         * so you don't have to test for c, s, or r instead of C, S, R
         */
        input = Character.toUpperCase(str.charAt(0));
     
        /* if / else if 
         * call the getArea() 
         */
        if (input == 'C') {
          getArea(radius, circumference);
     
      JOptionPane.showMessageDialog(
       null,"The area of your circle is " + area);
        } // end area c
        else if (input == 'S') {
          getArea(length, length);
     
       JOptionPane.showMessageDialog(
            null,"The area of your square is " + area);
        } // end area s
        else if (input == 'R') {
          getArea(length, height);
     
     JOptionPane.showMessageDialog(
            null,"The area of your rectangle is " + area);
        } // end area r
        else {
    JOptionPane.showMessageDialog(error);
        } // end error
      } //end main()
    } // end class

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JOptionPane Stops working after user input

    How is the code in post#7 different from the code in post#1?
    I see the Scanner class which needs to be removed.
    The values being returned by JOptionPane methods are not being assigned to variables so it can be used.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2014
    Location
    Atlanta, Georgia
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane Stops working after user input

    Thank you so much, I am going to take a break and look at this and see what I can come up with.

    --- Update ---

    import java.lang.String.*; // For toUpperCase()
    import javax.swing.JOptionPane; // for cute lil java windows
     
    public class CaculateAreaOpt {
      // Declare all variables needed for program
     
      public final static double radius = 3.14;
      public static char input;
      public static double area;
      public static double circumference;
      public static double length;
      public static double height;
      public static String str, error;
     
     
      public static double getArea(double x, double y) {
      // Char inputted by user then evaluates area
    if (input == 'C') {
        circumference = JOptionPane.showInputDialog(
            null,"Enter circumference");
     
          area = radius * circumference;
    } // end input c
     
     
     else if (input == 'R'){
         length =JOptionPane.showInputDialog(
            null,"Enter length of rectangle");
     
     
        height = JOptionPane.showInputDialog( 
       null,"Enter height of rectangle");
     
          area = length * height;
      }// end input R
     
        else if (input == 'S') {
           length = JOptionPane.showInputDialog(
            null, "Enter length of side");
     
          area = length * length;
    // end input S
      }// end input S
        // return the area
        return area;
      }
     
      /*
       * Method takes two double arguments
       */
     
      public static void main(String[] args) {
        // Get User to choose what shape to calculate Area of
     
     str = JOptionPane.showInputDialog(
            null,"Calculate the Area of a Square, Rectangle or Circle-Press S for square, R for Rectangle, or C for Circle");
        // String str takes Scanner in (next() value
     
        /* String str is an array
         *  0 is first character
         * Character.toUpperCase converts the String to uppercase
         * so you don't have to test for c, s, or r instead of C, S, R
         */
        input = Character.toUpperCase(str.charAt(0));
     
        /* if / else if 
         * call the getArea() 
         */
        if (input == 'C') {
          getArea(radius, circumference);
     
      JOptionPane.showMessageDialog(
       null,"The area of your circle is " + area);
        } // end area c
        else if (input == 'S') {
          getArea(length, length);
     
       JOptionPane.showMessageDialog(
            null,"The area of your square is " + area);
        } // end area s
        else if (input == 'R') {
          getArea(length, height);
     
     JOptionPane.showMessageDialog(
            null,"The area of your rectangle is " + area);
        } // end area r
        else {
     JOptionPane.showMessageDialog(error);
        } // end error
      } //end main()
    } // end class
    Last edited by Norm; February 9th, 2014 at 07:14 PM. Reason: Fixed code tag

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JOptionPane Stops working after user input

    Does it work now?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2014
    Location
    Atlanta, Georgia
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane Stops working after user input

    No I am still working on it ..Thank you ..
    It is wanting me to make the variables data type =string. so I have to convert to int.. this is quite difficult...
    public final static double radius = 3.14;
    public static char input;
    public static String inputStr;
    public static double area;
    public static String areaStr;
    public static double circumference;
    public static String circumferenceStr;
    public static String heightStr;
    public static String widthStr;
    public static String areaStr;
    public static double length;
    public static String lengthStr;
    public static double height;
    public static String str, error;

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JOptionPane Stops working after user input

    The Integer class has a method for converting a String to an int.
    Use it after getting the String from the user with the JOPtionPane method.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2014
    Location
    Atlanta, Georgia
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Gettin closer to the goal

    Exception in thread "main" java.lang.NumberFormatException: For input string: "S"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at Area.main(Area.java:14)
    import javax.swing.JOptionPane;
     
     
    public class Area
    {
      public static void main(String args[]) {
        String first,second;
        String choice;
        double radius,width,area,length;
        String value = " "; //intialize the string
        value = JOptionPane.showInputDialog("Please chose one of the options:"+"\n"+"a)Enter C to calculate the area of the Circle"+"\n"+"b)Enter S to calculate the area of the Square"+"\n"+"d)Enter R to calculate the area of the Rectangle"+"\n"+"e)Enter E to exit the program");
        int i;
     
        i = Integer.parseInt(value);
        while (i != 'E') {
     
    			 if (i == 'C') { //calculate the area of circle
            first = JOptionPane.showInputDialog("Enter the value of radius");
     
            radius = Double.parseDouble(first);
            area = Math.PI*radius*radius;
            //print out the result
            JOptionPane.showMessageDialog(null,"The area of the Circle:    "+area,"result",JOptionPane.INFORMATION_MESSAGE);
     
            } else
              if (i == 'S') { //calculate the area of square
                first = JOptionPane.showInputDialog("Enter the value of length");
                length = Double.parseDouble(first); //ge string into integer
                area = length*length;
                JOptionPane.showMessageDialog(null,"The area of the square:    "+area," result",JOptionPane.INFORMATION_MESSAGE);
              } else
                if (i =='R') { //calculate the area of rectangle
                  first = JOptionPane.showInputDialog("Enter the value of length");
                  second = JOptionPane.showInputDialog("Enter the value of width");
                  length = Double.parseDouble(first);
                  width = Double.parseDouble(second);
                  area = width*length;
                  JOptionPane.showMessageDialog(null,"The area of the rectangle:     "+area,"result",JOptionPane.INFORMATION_MESSAGE);
                } 
          value = JOptionPane.showInputDialog("Please chose one of the options:"+"\n"+"a)Enter C to calculate the area of the Circle"+"\n"+"b)Enter S to calculate the area of the Square"+"\n"+"d)Enter R to calculate the area of the Rectangle"+"\n"+"e)Enter E to exit the program");
          i = Integer.parseInt(value);
        } //end of while loop
        System.out.println("Program terminated\n");
        System.exit(0);
      } //end of main
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JOptionPane Stops working after user input

    Exception in thread "main" java.lang.NumberFormatException: For input string: "S"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at Area.main(Area.java:14)
    At line 14 the program tries to convert the letter "S" to an int value.

    Was the user supposed to enter a letter there ( just before line 14) or a number? If a letter, then the code should not try to make a number out of it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to input a decimal via JOptionPane and echo it as a Integer
    By quantamphysics in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 2nd, 2013, 07:55 PM
  2. JOptionPane not working?
    By Idiot_willing_to_learn in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 27th, 2013, 07:22 PM
  3. User Input help
    By lanmonster in forum What's Wrong With My Code?
    Replies: 20
    Last Post: January 20th, 2013, 10:44 AM
  4. Take user input
    By frabbi.cse in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2012, 12:48 PM
  5. Switching letter-cases from user input; why is not working?
    By Kimiko Sakaki in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 27th, 2011, 07:48 AM