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 5 of 5

Thread: Java Do..while loop problem

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java Do..while loop problem

    I'm trying to put a do...while loop into this code
    import javax.swing.JOptionPane;
     
    public class UpperLowerCase2
    {
        public static void main(String[] args)
        {
            int choice;
            String someString,menu;
     
     
     
            menu = "Enter\n"
                 + "(1) Enter a string\n"
                 + "(2) Convert the string to all uppercase\n"
                 + "(3) Convert the String to all lowercase\n"
                 + "(4) Quit";
    do{
                 someString = JOptionPane.showInputDialog( menu );
     
                 choice = Integer.parseInt( someString );
     
     
                switch (choice)
     
                {
                    case 1: enterAString();
     
     
     
                    case 2: stringToUppercase();
     
     
                    case 3: convertStringLower();
     
     
                    case 4: System.out.println("Quit");
     
                }
     
    }while(choice!=4);
     
    }
    public static void enterAString()
    {
        String outputmessage,string;
        string = JOptionPane.showInputDialog("Enter Your String");
        outputmessage = "You entered " + string;
        JOptionPane.showMessageDialog( null, outputmessage );
     
    System.exit(0);
    }
     
    public static void stringToUppercase()
    {
        String outputMessage2,string2;
        string2 = JOptionPane.showInputDialog("Enter String to convert to UPPERCASE");
        string2 = string2.toUpperCase();
        outputMessage2 = " Here Ya Go " + string2;
        JOptionPane.showMessageDialog(null, outputMessage2);
        System.exit(0);
    }
     
    public static void convertStringLower()
    {
        String outputMessage3,string3;
        string3 = JOptionPane.showInputDialog("Enter String to convert to lowercase");
        string3 = string3.toLowerCase();
        outputMessage3 = "Here Ya Go " + string3;
        JOptionPane.showMessageDialog(null, outputMessage3);
        System.exit(0);
    }
     
    }

    However it isn't looping can somebody fix it for me or tell me what i need to do. The program runs, but it dosen't loop. Help Pleez


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Do..while loop problem

    What does it do instead? What do you think those System.exit() calls do?

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Java Do..while loop problem

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java Do..while loop problem

    You're forgetting to use breaks inside your switch statement.

    See: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

  5. #5
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: Java Do..while loop problem

    System.exit(0) terminates your program whether u are in a loop or not.

Similar Threads

  1. Problem with For Loop
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2010, 12:23 PM
  2. [SOLVED] While Loop Problem :(
    By faisalnet5 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2010, 11:23 AM
  3. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 04:40 AM
  4. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  5. [SOLVED] Java for loop problem and out put is not coming
    By thewonderdude in forum Loops & Control Statements
    Replies: 9
    Last Post: March 15th, 2009, 02:31 PM

Tags for this Thread