Results 1 to 7 of 7
  1. #1
    Selah is offline Member
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0

    Default Creating a user inputted rectangle, having trouble reprompting? *First Post*

    Hello, I'm currently working to create a rectangle of asterisks using user inputted code, but if the value entered is less than 2, the user has to re-enter it. Everything in my code is working fine until the repromting, where there is one asterisk missing from the top right corner when the values are entered. For example, after my code asks you to reprompt for width and length of the rectangle, (say you enter 5 for both), the rectangle should look like this:
    *****
    *****
    *****
    *****
    *****

    But instead looks like this:
    ****
    *****
    *****
    *****
    *****
    It's bothering me, and I'm not sure how to fix it! Here's my code below:



    Java Code:
    public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		int width; 
    	    int length;
    
    	    
    	    System.out.println("How long should the width of the Rectangle be?");
    	    width = in.nextInt();
    
    	    System.out.println("How long should the length of the Rectangle be?");
    	    length = in.nextInt(); 
    	   
    	    {	
    	    for (int y = 0; y < length; y++)
    	    {  
    	      for (int x = 0; x < width; x++)
    	      { 
    	    	  if (x == 0 || y == 0)
    		        {
    		          System.out.print("*");
    		        }
    	    	  else if (x != width && y == length-1)
    		        {
    		          System.out.print("*");
    		        }
    	    	  else if (y != length && x == width-1)
    		        {
    		          System.out.print("*");
    		        }
    		        else
    		        {
    		           System.out.print("*");
    		        }
    	    	  while( (length) < 2 || (width) < 2){
    	    		    System.out.println("\nLength/Width has to be greater than/equal to 2, please try again: ");
    	    		    System.out.println("How long should the width of the Rectangle be?");
    	    		    width = in.nextInt();
    
    	    		    System.out.println("How long should the length of the Rectangle be?");
    	    		    length= in.nextInt();
    	    	  }
    	    	  }
    		      
    		    System.out.println(" ");
    	    }
    	}
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Creating a user inputted rectangle, having trouble reprompting? *First Post*

    Why are you reprompting inside the loop that displays the '*'?

    I would expect the flow to be something like:
    Java Code:
    While width < 2
        prompt for width
    While length < 2
        prompt for length
    draw rectangle
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Creating a user inputted rectangle, having trouble reprompting? *First Post*

    Your problem is also line 32. That will always execute before you check the width and or length. But check the width and length before you start to build the rectangle.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Selah is offline Member
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0

    Default Re: Creating a user inputted rectangle, having trouble reprompting? *First Post*

    This ordering does make sense, I think my problem was with waiting to create parameters for repromting the rectangle after I had already coded the initial drawing of it. Thanks!

  5. #5
    Selah is offline Member
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0

    Default Re: Creating a user inputted rectangle, having trouble reprompting? *First Post*

    I actually just figured that out, haha. All I had to do was move my while loop above the first if statement in the second for-loop and it worked perfectly! Thank you for the quick reply.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Creating a user inputted rectangle, having trouble reprompting? *First Post*

    Why prompt inside the for loops?

    Tell you what, to give an example of why not, try entering 0 in for either parameter.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: Creating a user inputted rectangle, having trouble reprompting? *First Post*

    Your code is insanely complicated. It should be a simple double loop:

    Java Code:
    import java.util.Scanner;
    
    public class Rectangle2{
            public static void main(String[] args) {
                    Scanner in = new Scanner(System.in);
                    int width; 
                    int length;
    
                    System.out.println("How wide should the Rectangle be?");
                    width = in.nextInt();
    
                    System.out.println("How long should Rectangle be?");
                    length = in.nextInt(); 
    
                    for (int y = 0; y < length; y++) {  
                            for (int x = 0; x < width; x++) { 
                                 System.out.print("*");
                            }
                            System.out.println();
                    }
            }
    }

Similar Threads

  1. User can't post
    By SurfMan in forum Suggestions & Feedback
    Replies: 2
    Last Post: 06-01-2015, 02:53 PM
  2. Replies: 7
    Last Post: 05-19-2014, 04:57 PM
  3. Replies: 5
    Last Post: 12-27-2011, 09:09 PM
  4. Sorting printed ArrayList of user inputted strings.
    By movsesinator in forum New To Java
    Replies: 3
    Last Post: 04-03-2010, 10:27 PM
  5. Help Creating A Graph From Inputted Data
    By adlb1300 in forum New To Java
    Replies: 6
    Last Post: 10-28-2007, 05:45 AM

Tags for this Thread

Posting Permissions

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