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

Thread: Not Looping? (do - while) bad execution!

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Not Looping? (do - while) bad execution!

    this program only asks me the first input
    Enter The Max Value
    then the whole program stops

    public static void main(String[] args) {
     
            int count = 0;
     
            System.out.print("Enter The Max Value: ");
            int max = sc.nextInt();
     
            System.out.print("Which Value Do You Want To Display Even Or Odd?");
            String answer = sc.nextLine();
     
            do {
     
                count++;
     
                if (answer.equalsIgnoreCase("even")) {
     
                    if (count % 2 == 0) {
     
                        System.out.println(count);
                    }
                }
            }
            while (count != max);     
        }
    }
    Last edited by chronoz13; November 23rd, 2009 at 11:00 AM.


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

    Default Re: Not Looping? (do - while) bad execution!

    There's no case to handle the odd numbers. Also, because of the funny implementation of nextInt(), it's not consuming the next line character when you input the max value.

    Also, there is a much better method to print out odd/even numbers: use an if statement for even/odd, then iterate through all even/odd numbers up to max.

            System.out.print("Enter The Max Value: ");
            int max = sc.nextInt();
     
            System.out.print("Which Value Do You Want To Display Even Or Odd?");
            sc.nextLine(); // consume \n from above
            String answer = sc.nextLine();
     
            if ("even".equalsIgnoreCase(sc))
            {
                    for (int i = 0; i < max; i+=2)
                    {
                            System.out.println(i);
                    }
            }
            else if ("odd".equalsIgnoreCase(sc))
            {
                    for (int i = 1; i < max; i+= 2)
                    {
                            System.out.println(i);
                    }
            }
            else
            {
                    System.out.println("You didn't enter even or odd. quiting.");
            }

Similar Threads

  1. Looping through a multidimensional array
    By MysticDeath in forum Loops & Control Statements
    Replies: 2
    Last Post: October 11th, 2009, 05:41 PM
  2. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM
  3. Replies: 24
    Last Post: April 14th, 2009, 03:43 PM