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

Thread: Non-existing loop

  1. #1
    Junior Member Neo's Avatar
    Join Date
    Feb 2011
    Location
    Ohio
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Non-existing loop

    ok guys, here is my deal. I'm writing a program for my Comp Programming I class. My assignment says "you are to write a short program that will input a positive integer number in the range 1 - 16. If the input number is not in the correct range a message should be sent to the user until a correct input is submitted. A correct input number is used to print a square which has sides of the input length. The square is made up of an input character." i got that part done. the second part of the lab is "You are to extend the program you wrote in 5A to work for a square, right triangle, or an isosceles triangle. Your program should work regardless of the case input of figure type. You are also to check for incorrect figure type. As in part A above the request for good input is repeated until a valid number or figure type is entered."

    I have all of it down, but for some reason the loop i set up to check the validity of the figure shape input wont exit. You enter a valid input, and it just asks you again what figure you would like printed. without the loop the program runs great, as long as you aren't retarded and don't enter an invalid input for the shape of the figure. I get no compile errors from the IDE saying the loop is not set up right....i have no clue whats going on. any ideas?

    Scanner keyboard = new Scanner(System.in);
            int size, p = 1, q = 1;
            char symbol = 0;
            String character, character2, shape;
            final String RIGHT_TRIANGLE = "Right Triangle";
            final String ISOSCELES_TRIANGLE = "Isosceles Triangle";
            final String SQUARE = "Square";
            do {
                System.out.printf("Enter the size (1 - 16) of the square side: ");
                size = keyboard.nextInt();
                if (size > 16 || size < 1) {
                    System.out.println("Your input was out of range");
                }
            } while (size < 1 || size > 16);
     
     
            System.out.printf("What character is to be used: ");
            character = keyboard.next();
            character2 = character.toUpperCase();
            symbol = character2.charAt(0);
            keyboard.nextLine();
            do {
                System.out.printf("Enter the figure type: Square"
                        + ", Right Triangle, or Isosceles Triangle: ");
                shape = keyboard.nextLine();
     
            } while (!shape.equalsIgnoreCase(SQUARE) || !shape.equalsIgnoreCase(RIGHT_TRIANGLE)
                    || !shape.equalsIgnoreCase(ISOSCELES_TRIANGLE));
     
            if (shape.equalsIgnoreCase(SQUARE)) {
                for (int square = 0; square < size; square++) {
                    for (int square2 = 0; square2 < size; square2++) {
                        System.out.print(symbol);
                    }
                    System.out.println();
                }
            } else if (shape.equalsIgnoreCase(ISOSCELES_TRIANGLE)) {
                for (int yy = 1; yy < size; yy++) {
                    for (int xy = 1; xy < size - yy; xy++) {
                        System.out.print(" ");
                    }
                    for (int z = 1; z <= p; z++) {
                        System.out.print(symbol);
                    }
                    p += 2;
                    System.out.println("");
                }
     
            } else if (shape.equalsIgnoreCase(RIGHT_TRIANGLE)) {
                for (int qq = 0; qq < size; qq++) {
                    for (int xq = 0; xq < size - qq; xq++) {
                        System.out.print(" ");
                    }
                    for (int z = 1; z <= q; z++) {
                        System.out.print(symbol);
                    }
                    q += 1;
                    System.out.println("");
                }
     
            }

    any help will be greatly appreciated.
    Last edited by Neo; February 12th, 2011 at 06:35 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Non-existing loop

    Change OR to AND.
    Using or operation,

    !shape.equalsIgnoreCase(SQUARE)
    would return true if you entered Triangle and it would re-enter the loop.

    while (!shape.equalsIgnoreCase(SQUARE) && !shape.equalsIgnoreCase(RIGHT_TRIANGLE)
                    && !shape.equalsIgnoreCase(ISOSCELES_TRIANGLE));

    This means than if shape does equal SQUARE, condition will not hold and it will exit.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    javapenguin (February 14th, 2011)

  4. #3
    Junior Member Neo's Avatar
    Join Date
    Feb 2011
    Location
    Ohio
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Non-existing loop

    Newbie you are awesome, thank you so much!!!!!

Similar Threads

  1. Need help implenting arrays and method call into existing code
    By hoven in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 31st, 2011, 01:36 AM
  2. Add code to an existing project
    By atul.mathur31 in forum Java IDEs
    Replies: 1
    Last Post: January 5th, 2011, 06:50 PM
  3. Replies: 3
    Last Post: January 5th, 2011, 08:12 AM
  4. Replies: 2
    Last Post: August 1st, 2010, 06:29 AM
  5. How to append data to an already existing file?
    By siteregsam in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 3rd, 2010, 02:31 PM