Results 1 to 17 of 17
  1. #1
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default [SOLVED] while loop

    I built this program but when I run it the while loop doesn't allow you to pick a new shape it just keeps asking for the new enteries for the same shape how do i get the while loop to go back to the beginning

    import javax.swing.JOptionPane;

    public class ShapesCalcLoop {
    public static void main(String[] args) {

    //Prompt the user to Enter S, C, T, or Q to quit

    char shape = JOptionPane.showInputDialog("Enter S, C, T, or Q to quit: ").charAt(0);
    while (shape != 'Q'){
    switch (shape){
    case 's':;
    case 'S': {
    String lengthString = JOptionPane.showInputDialog("Enter length of side: ");
    double length = Double.parseDouble(lengthString);
    //Calculate perimeter and area of square
    double perimeter= ( 4 * length);
    double area=(length*length);
    String output = "Perimeter is equal to: " + perimeter + "\n" + "Area is equal to: " + area;
    JOptionPane.showMessageDialog(null,output);
    }; break;
    case 'c':;
    case 'C': {
    String radiusString = JOptionPane.showInputDialog("Enter radius: ");
    double radius = Double.parseDouble(radiusString);
    //Calculate circumference and area of circle
    double circumference= (2*3.14*radius);
    double area = (Math.PI * (radius * radius));
    String output2 ="Circumference is equal to: " + circumference + "\n" + "Area is equal to: " + area;
    JOptionPane.showMessageDialog(null,output2);
    }; break;
    case 't':;
    case 'T': {
    String baseString = JOptionPane.showInputDialog("Enter length of base: ");
    double base = Double.parseDouble(baseString);
    String heightString = JOptionPane.showInputDialog("Enter height of triange: ");
    double height = Double.parseDouble(heightString);
    //Calculate area of triangle
    double area = (base * height) / 2;
    String output3 = "Area is equal to: " + area;
    JOptionPane.showMessageDialog(null,output3);
    }; break;
    default: {
    JOptionPane.showMessageDialog(null,"Incorrect variable please enter S,C, or T only");
    System.exit(1);

    };


    JOptionPane.showMessageDialog(null,"This program was written by Justeena Leonard");
    }
    }

    }
    }

  2. #2
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Your code has a lot of things it should not have, like semi-colons in places they shouldn't be (i.e. "case 'c':;")

    Are you transferring over from C/C++? If you are, here are some tips/facts:

    - You don't need to group variables in braces, the variables won't be removed at the end of the braces. Memory management in Java is entirely automated, so you don't need to worry about it.

    - You shouldn't have semi-colons at the end of your cases in your switch statement, these might be the cause of your error.

    - You have "double area" defined twice, probably with the idea that they would be removed at the end of the brace groups you made. You should define all variables outside any while, do-while, switch, or for loop.

    I hope this helps. Try fixing your code and posting back. Also, the code is easier to read when you put it inside of code tags (<code> [/code]), I purposely gave the first one the wrong brackets, because otherwise, they would just do this:

    Java Code:
    This, for example, is what code tags do.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I use HTML escape sequences to describe how to use code tags, like so:

    When posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

  4. #4
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    the program itself works fine it was built seperatly the problem occured when i added the loop the problem is where the loop comes back ...

    i did fix the fact that i defined double area three times, and when i deleted the semi-colons there was a compiling error so i added them back in

  5. #5
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Java Code:
     import javax.swing.JOptionPane;
    
    public class ShapesCalcLoop {
        public static void main(String[] args) {
       
            //Prompt the user to Enter S, C, T, or Q to quit
            char shape = JOptionPane.showInputDialog("Enter S, C, T, or Q to quit: ").charAt(0);
            while (shape != 'Q'){
                switch (shape){
                case 's':;
                case 'S': {
                    String lengthString = JOptionPane.showInputDialog("Enter length of side: ");
                    double length = Double.parseDouble(lengthString);
                    //Calculate perimeter and area of square
                    double perimeter= ( 4 * length);
                    double area=(length*length);
                    String output = "Perimeter is equal to: " + perimeter + "\n" + "Area is equal to: " + area;
                    JOptionPane.showMessageDialog(null,output);
                }; break;
                case 'c':;
                case 'C': {
                    String radiusString = JOptionPane.showInputDialog("Enter radius: ");
                    double radius = Double.parseDouble(radiusString);
                    //Calculate circumference and area of circle
                    double circumference= (2*3.14*radius);
                    double area2 = (Math.PI * (radius * radius));
                    String output2 ="Circumference is equal to: " + circumference + "\n" + "Area is equal to: " + area2;
                    JOptionPane.showMessageDialog(null,output2);
                }; break;
                case 't':;
                case 'T': {
                   String baseString = JOptionPane.showInputDialog("Enter length of base: ");
                    double base = Double.parseDouble(baseString);
                    String heightString = JOptionPane.showInputDialog("Enter height of triange: ");
                    double height = Double.parseDouble(heightString);
                    //Calculate area of triangle
                    double area3 = (base * height) / 2;
                    String output3 = "Area is equal to: " + area3;
                    JOptionPane.showMessageDialog(null,output3);
                }; break;
                default: {
                    JOptionPane.showMessageDialog(null,"Incorrect variable please enter S,C, or T only");
                    System.exit(1);
                    shape=shape++;
                
             };
            
                }
            	}
              JOptionPane.showMessageDialog(null,"This program was written by Justeena Leonard");
        
                }
        }

  6. #6
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    and thank you for telling me about the code stuff i didn't know

  7. #7
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Did you remove the semicolons that come after break? Those should stay.

    From where are you learning Java? I recommend looking at The Java Tutorials by Sun.

    And to fix your problem, you are not getting a new character inside of your for loop, you are only doing it right before you begin the loop.

    Above link:
    The Java™ Tutorials

  8. #8
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    I have looked at so many tutorials I really just don't get it my teacher doesn't teach and he expects us to teach ourselves ... I have never done anything even close to java so this whole expierence is really confusing

    I don't really get what you mean by the last sentence

  9. #9
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    o and i deleted the ; i thought you meant the :

  10. #10
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Java Code:
    char shape = JOptionPane.showInputDialog("Enter S, C, T, or Q to quit: ").charAt(0);
    while (shape != 'Q'){
    switch (shape){
    That's a snippet of your code. Basically, you make the character 'shape' equal to whatever the user puts into the InputDialog created by JOptionPane. However, each time the while loop loops back through, you aren't getting any new character, shape will just keep increasing due to your statement "shape=shape++" which causes shape to eventually become equal to 'Q'. Also, "shape=shape++" could just be "shape++". For example, the following two statements do the exact same thing.
    Java Code:
    shape = shape+1;
    shape++;
    
    // and I guess this does too, although it's not the "correct" way to do it
    shape = shape++;
    I highly recommend Sun's tutorials if you haven't tried them, they really teach you Java well.

  11. #11
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Ok i see why it is looping that way do i need to define something other then shape to avoid that problem

  12. #12
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    No, I think you should do this with that code snippet:
    Java Code:
    char shape; // variables like this should usually be initialized
    while (shape != 'Q'){
    shape = JOptionPane.showInputDialog("Enter S, C, T, or Q to quit: ").charAt(0);
    switch (shape){
    And you should also get rid of "shape=shape++" or anything that increments it, because the user is just going to overwrite it, so this statement is really pointless.

    And then, get rid of the statement "System.exit(0)", it will make the program quit if the user enters an incorrect value, which I doubt is what you want. The program will just loop through if you take it out, warning the user that they used an invalid variable.

    And lastly, you should define all your variables outside the while loop, and reuse them where appropriate in each case of the switch statement. For example:

    Java Code:
    double area = 0.0;
    while (shape != 'Q') {
      switch (shape) {
      ...
      case 'S':
        area = ...;
        break;
      case 'C':
        area = ...;
        break;
      ...
      }
    }
    I left out a lot of code, but this should give you an idea of reusing variables.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by leonardjr View Post
    I have looked at so many tutorials I really just don't get it my teacher doesn't teach and he expects us to teach ourselves ...
    The sooner you adapt to this, the better. I hate to tell you but that's how it will be the rest of your life.

  14. #14
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    O.k. i put in place all your suggestions and i got this error

    "variable shape might not have been intialized"

    Java Code:
     import javax.swing.JOptionPane;
    
    public class ShapesCalcLoop {
        public static void main(String[] args) {
       
            //Prompt the user to Enter S, C, T, or Q to quit
            double length;
            double perimeter;
            double area;
            double radius;
            double circumference;
            double area2;
            double base;
            double height;
            double area3;
            char shape;
            while (shape != 'Q'){
            shape=JOptionPane.showInputDialog("Enter S, C, T, or Q to quit: ").charAt(0);
                switch (shape){
                case 's':
                case 'S': {
                    String lengthString = JOptionPane.showInputDialog("Enter length of side: ");
                    length = Double.parseDouble(lengthString);
                    //Calculate perimeter and area of square
                    perimeter= ( 4 * length);
                    area=(length*length);
                    String output = "Perimeter is equal to: " + perimeter + "\n" + "Area is equal to: " + area;
                    JOptionPane.showMessageDialog(null,output);
                }; break;
                case 'c':
                case 'C': {
                    String radiusString = JOptionPane.showInputDialog("Enter radius: ");
                    radius = Double.parseDouble(radiusString);
                    //Calculate circumference and area of circle
                    circumference= (2*3.14*radius);
                    area2 = (Math.PI * (radius * radius));
                    String output2 ="Circumference is equal to: " + circumference + "\n" + "Area is equal to: " + area2;
                    JOptionPane.showMessageDialog(null,output2);
                }; break;
                case 't':
                case 'T': {
                   String baseString = JOptionPane.showInputDialog("Enter length of base: ");
                    base = Double.parseDouble(baseString);
                    String heightString = JOptionPane.showInputDialog("Enter height of triange: ");
                    height = Double.parseDouble(heightString);
                    //Calculate area of triangle
                    area3 = (base * height) / 2;
                    String output3 = "Area is equal to: " + area3;
                    JOptionPane.showMessageDialog(null,output3);
                }; break;
                default: {
                    JOptionPane.showMessageDialog(null,"Incorrect variable please enter S,C, or T only");
                   
                
             };
            
                }
            	}
              JOptionPane.showMessageDialog(null,"This program was written by Justeena Leonard");
        
                }
        }

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    what do you think the error means?

    e.g., your code with comments:
    Java Code:
      char shape; // shape declared, but what does it equal at this point?
      while (shape != 'Q'){  // shape's value tested.  Again, 
               //what does it equal initially when tested 1st time?
    Last edited by Fubarable; 03-04-2009 at 05:23 AM.

  16. #16
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    I placed a comment in the code I posted that you should initialize variables that are outside of loops. This is because the compiler likes to know what the variable should be at first, and if the only time you assign a variable in inside a control-flow statement (if statement, while loop, etc.), then it doesn't know what to do, so it gives you an error.

    Just change "char shape;" to "char shape = 'A';", or anything you want. Same with the other variables, it's a good practice.

    Sorry if I wasn't clear when I said "variables like this" in my comment, that really is a vague statement.

    EDIT: Sorry, I have a bad practice of my own.

  17. #17
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

Similar Threads

  1. Help w/ loop
    By iWonder in forum New To Java
    Replies: 17
    Last Post: 11-20-2008, 01:39 AM
  2. While loop
    By sjhentges in forum New To Java
    Replies: 11
    Last Post: 11-04-2008, 05:26 PM
  3. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 07:44 AM
  4. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 01:56 AM
  5. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 11:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •