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: print a shape a variable number of times across and down (for-loop hell)

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Location
    somewhere
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question print a shape a variable number of times across and down (for-loop hell)

    I have a program where I am attempting to using asterisks to print a shape with a variable size a certain number of times across and down the screen, and the borders of the shape need to overlap.

    For example:

    to print a 3x3 square across the screen 4 times and down 2 times, the input would be

    S, 3, 4, 2

    and the output would be

    *********
    *********
    *********
    *********
    *********
    (that should has 9 * across and 5 * down--I know that asterisks tend to blur together when you try to count/type them)

    I actually have completed the code to produce the overlapping squares, but I need to have a right-side-up right triangle, and up-side-down right triangle, a box, and a tic-tac-toe grid.

    The code I have thus far is:

    import java.util.Scanner;
     
    public class AsteriskSenior {
     
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
     
    			String inputLine = input.nextLine();
    			String shapeType;
    			int firstNumber;
    			int secondNumber;
    			int thirdNumber;
    			int fourthNumber;
    			shapeType = obtainLetter(inputLine);
     
    			if (shapeType.equals("S")) //square
    			{
    				firstNumber = obtainFirstNumber(inputLine);
    				secondNumber = obtainSecondNumber(inputLine);
    				thirdNumber = obtainThirdNumber(inputLine);
     
    				square(firstNumber, secondNumber, thirdNumber);
    			}
    			else if (shapeType.equals("H")) //up side down triangle
    			{
    				firstNumber = obtainFirstNumber(inputLine);
    				secondNumber = obtainSecondNumber(inputLine);
    				thirdNumber = obtainThirdNumber(inputLine);
     
    				UDTriangle(firstNumber, secondNumber, thirdNumber);
    			}
    			else if (shapeType.equals("L")) //right side up triangle
    			{
    				firstNumber = obtainFirstNumber(inputLine);
    				secondNumber = obtainSecondNumber(inputLine);
    				thirdNumber = obtainThirdNumber(inputLine);
     
    				RUTriangle(firstNumber, secondNumber, thirdNumber);
    			}
    			else if (shapeType.equals("B")) //box
    			{
    				firstNumber = obtainFirstNumber(inputLine);
    				secondNumber = obtainSecondNumber(inputLine);
    				thirdNumber = obtainThirdNumberOfBox(inputLine);
    				fourthNumber = obtainFourthNumber(inputLine);
     
    				box(firstNumber, secondNumber, thirdNumber, fourthNumber);
    			}
    			else if (shapeType.equals("T")) //tic-tac-toe grid
    			{
    				firstNumber = obtainFirstNumber(inputLine);
    				secondNumber = obtainSecondNumber(inputLine);
    				thirdNumber = obtainThirdNumber(inputLine);
     
    				TTT(firstNumber, secondNumber, thirdNumber);
    			}
     
    	}
     
    	public static void square(int firstNumber, int secondNumber, int thirdNumber)//S
    	{	
    		for (int a=0; a<(firstNumber*(thirdNumber)-thirdNumber+1); a++)
    		{
    			for (int b=0; b<(firstNumber*(secondNumber)-secondNumber+1);b++)
    			{
    				System.out.print("*");
    			}
    			System.out.println("");
    		}		
    	}
     
     
    	public static void UDTriangle(int firstNumber, int secondNumber, int thirdNumber)//H
    	{
    		for (int i=0; i<firstNumber; i++)
    		{
    			for (int j=0; j<i; j++)
    			{
    				System.out.print(" ");
    			}
    			for (int j=i; j<(firstNumber*(secondNumber)-secondNumber+1); j++)
    			{
    				System.out.print("*");               
    			}
    			System.out.println("");
    		}
    	}
     
     
    	public static void RUTriangle(int firstNumber, int secondNumber, int thirdNumber)//L
    	{
     
    	}
     
     
    	public static void box(int firstNumber, int secondNumber, int thirdNumber, int fourthNumber)//B
    	{
     
    	}
     
     
    	public static void TTT(int firstNumber, int secondNumber, int thirdNumber)//T
    	{
     
    	}
     
     
    	public static String obtainLetter(String inputLine)
    	{
    		//Assume input.nextLine() was used to obtain input
    		int locationOfComma = inputLine.indexOf(",");
    		String theLetter = inputLine.substring(0, locationOfComma);
    		return theLetter;
    	}
     
    	public static int obtainFirstNumber(String inputLine)
    	{
    		//Assume input.nextInt() was used to obtain input
    		int locationOfFirstComma = inputLine.indexOf(",");
    		//Start of First Way (using 2 parameters .indexOf())
    		int locationOfSecondComma = inputLine.indexOf(",", locationOfFirstComma+1);
    		String firstNumberString = inputLine.substring(locationOfFirstComma+2,
                            locationOfSecondComma);
    		//Convert from String to int
    		int firstNumber = Integer.valueOf(firstNumberString);
     
    		return firstNumber;
    	}
     
    	public static int obtainSecondNumber(String inputLine)
    	{
    		//Assume input.nextInt() was used to obtain input
    		int locationOfFirstComma = inputLine.indexOf(",");
    		//using 2 parameters for .indexOf())
    		int locationOfSecondComma = inputLine.indexOf(",", locationOfFirstComma+1);
    		int locationOfThirdComma = inputLine.indexOf(",", locationOfSecondComma+1);
    		String secondNumberString = inputLine.substring(locationOfSecondComma+2, 
                            locationOfThirdComma);
    		//convert from String to int
    		int secondNumber = Integer.valueOf(secondNumberString);
     
    		return secondNumber;
    	}
     
    	public static int obtainThirdNumber(String inputLine)
    	{
    		//Assume input.nextInt() was used to obtain input
    		int locationOfFirstComma = inputLine.indexOf(",");
    		//using 2 parameters for .indexOf())
    		int locationOfSecondComma = inputLine.indexOf(",", locationOfFirstComma+1);
    		int locationOfThirdComma = inputLine.indexOf(",", locationOfSecondComma+1);
    		//using 1 parameter for .indexOf())
    		String thirdNumberString = inputLine.substring(locationOfThirdComma+2);
    		//convert from String to int
    		int thirdNumber = Integer.valueOf(thirdNumberString);
     
    		return thirdNumber;
    	}
     
    	public static int obtainThirdNumberOfBox(String inputLine)
    	{
    		//Assume input.nextInt() was used to obtain input
    		int locationOfFirstComma = inputLine.indexOf(",");
    		//using 2 parameters for .indexOf())
    		int locationOfSecondComma = inputLine.indexOf(",", locationOfFirstComma+1);
    		int locationOfThirdComma = inputLine.indexOf(",", locationOfSecondComma+1);
    		int locationOfFourthComma = inputLine.indexOf(",", locationOfThirdComma+1);
    		//convert from String to int
    		String thirdNumberString = inputLine.substring(locationOfThirdComma+2,
                            locationOfFourthComma);
    		int thirdNumber = Integer.valueOf(thirdNumberString);
     
    		return thirdNumber;
    	}
     
    	public static int obtainFourthNumber(String inputLine)
    	{
    		//Assume input.nextInt() was used to obtain input
    		int locationOfFirstComma = inputLine.indexOf(",");
    		//using 2 parameters for .indexOf())
    		int locationOfSecondComma = inputLine.indexOf(",", locationOfFirstComma+1);
    		int locationOfThirdComma = inputLine.indexOf(",", locationOfSecondComma+1);
    		//using 1 parameter for .indexOf())
    		int locationOfFourthComma = inputLine.indexOf(",", locationOfThirdComma+1);
    		String fourthNumberString = inputLine.substring(locationOfFourthComma+2);
    		//convert from String to int
    		int fourthNumber = Integer.valueOf(fourthNumberString);
     
    		return fourthNumber;
    	}
     
    }
    Last edited by GE^2K; October 24th, 2010 at 10:15 AM.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: print a shape a variable number of times across and down (for-loop hell)

    GE^2K, I'm not sure how to answer this as there didn't seem to be a question in there. I'd encourage you to clarify by posting an explanation as to what you expect and what you get, as well as to search the forums for "triangle asterisk', as this task has been addressed quite a few times in the past.

Similar Threads

  1. help with loop to print an equliateral asterisk triangle
    By everyone0 in forum Loops & Control Statements
    Replies: 13
    Last Post: October 11th, 2012, 12:37 PM
  2. [SOLVED] how to print for-loop output to JTextArea
    By voltaire in forum AWT / Java Swing
    Replies: 2
    Last Post: May 13th, 2010, 02:43 PM
  3. Can a for loop work with random number?
    By humdinger in forum Loops & Control Statements
    Replies: 7
    Last Post: January 10th, 2010, 09:06 PM
  4. GuessWhat- same errors repeated 4 times?
    By iank in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2009, 07:32 PM
  5. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM