Simple assignment - create a java program to print j a v v a
j a a v v a a
j j aaaaa vv aaaaa
jj a a v a a

I'm using a 2d array rather than escape characters and print sequences but can anyone thing of a more efficient way of coding this? As it is I'm manually setting the non-null characters by hand with the odd for loop where I can, for example the 3 j's in a row downwards

EDIT - pattern isn't being kept, basically I'm to spell out java 4 lines high creating the larger characters with the character they represent, think ascii art.

Recommended Answers

All 3 Replies

There are other ways of doing it. Since I'm not sure exactly what you're doing, I'm not sure if they're more efficient, but there are always other ways to do anything.

If you want it to be locked in to just printing that one string, that one way, probably it's easiest to just plot it out and make four Stringsone for each line, then print them.
That's about as efficient as you'll get there.

If you wanted to be more ambitious, and make it so that it could take an arbitrary string of characters and print them in this form, you could use arrays of Strings to do that. Each character would get four String arrays, one for each line that composes the character. On printing, you'd look up the correct character and print the current line - the lookup could be in an array, then you'd have an array of arrays of String, or probably better to use a map, so you can lookup by chars without converting to ints first. Up to you.

It would take you a bit of work to generate the characters, but the actual coding would not require any very esoteric techniques. Basically, you'd have an outer loop of (i=0; i<4; i++). In each iteration, you'd walk down the input string (another loop) and get the charAt() each location. Use that char to look up the correct array of Strings, and print array. That is, you're going to just print
one line of Strings, then the next line, and so on, for four lines.

Assuming 80 column display, you'll be limited to 80/5 = 16 characters at a time, unless you get fancy and do some line breaking, but save that for version 2.0

Thanks Jon, it's just a lab assignment but I wanted to be a bit smart about it. In the end I went with

public class Exercise01_03
    {
	char[][] java = new char[4][31];
	
	Exercise01_03()
	{
	    populateJavaArray();
	}
	
	public void populateJavaArray()
	    {
	    for(int i = 0; i < 4; i++)
	    {
	        for(int j = 0; j < 31; j++)
	        {
	            java[i][j] ='\u0020';
	        }
	    }
	           
	        
		java[2][0] = 'J';
		java[3][1] = 'J';
		java[3][2] = 'J';
		
		for(int i = 0; i < 3; i++)
		    {
			java[i][3] = 'J';
		    }
		
		java[3][6] = 'A';
		java[1][8] = 'A';
		java[0][9] = 'A';
		java[1][10] = 'A';
		java [3][12] = 'A';
		
		for(int i = 7;i < 12;i++)
		    {
			java[2][i] = 'A';
		    }
		
		java[0][15] = 'V';
		java[1][16] = 'V';
		java[2][17] = 'V';
		java[3][18] = 'V';
		java[2][19] = 'V';
		java[1][20] = 'V';
		java[0][21] = 'V';
		
		java[3][24] = 'A';
		java[1][26] = 'A';
		java[0][27] = 'A';
		java[1][28] = 'A';
		java [3][30] = 'A';
		
		for(int i = 25;i < 30;i++)
		    {
			java[2][i] = 'A';
		    }
		
		
		
		
	    }
	
	public void printArray()
	    {
		for(int i = 0; i < 4;i++)
		    {
			for(int j = 0; j < 31; j++)
			    {
				System.out.print(java[i][j]);
			    }
			System.out.println();
		    }
	    }

	
	public static void main(String[] args)
	    {
		Exercise01_03 print = new Exercise01_03();
		print.printArray();

	    }

    }

Certainly not efficient, but you'll never lose your place in an array again!

Now that you're finished with that one, you may as well go ahead and write the other - it'll be good practice. :) (Seriously, it's worth writing lots of little toy programs now, while you're learning - even if they're pointless, you'll learn something from them.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.