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.
Members have full access to the forums. Advertisements are removed for registered users.
Looks like you just need to add a space between each number and indent each line. Just think it through some more as to how to go about adding the indents and such.
Would I have to use another nested loop? or an if statement? I've been trying for 2 days, but I don't seem to get a right solution
Does anybody know how to explain where I would include the spaces?
...edited by moderator
Last edited by copeg; June 15th, 2012 at 03:12 PM. Reason: spoonfeeding
Step away from the compiler for a minute and pretend that you are going to write a letter to a friend with some instructions so that he/she can reproduce that table. Your friend has a text editor on his/her computer, but has no way to compile and run a Java program.
How would you describe the way to create the table using a text editor? (That's a rhetorical question, I don't necessarily need to see your natural language description.)
Now, you find that your friend doesn't have a Java compiler, and doesn't know the language, so you can't just mail your Java source. You know that your friend does have some kind of compiler, but you don't know what it is and pretend that you don't know any other computer languages anyhow.
So, you might send some pseudo-code that describes the process
Maybe something like the following;
First of all, forget the leading spaces for a minute. Suppose you want to
1 2 3 4 2 3 4 3 4 4
How might it be described in pseudo-code?
Maybe something like:
// Declare two variables: // numLines is the total number of lines to print. // This is fixed at the beginning of the program. // // numOnLine is the number of digits on each line // First line : numOnLine is equal to numLines // Second line: it is one less than on the previous line // Third line : it is one less than on the previousline // Etc. // // Here's the program in pseudo-code Set the value numLines equal to, say, 4 Set the value of numOnLine equal to numLines for (int i = 0; i < numLines; i++) BEGIN LOOP // Outer Loop for (int j = 0; j < numOnLine; j++) BEGIN LOOP // Inner Loop Print decimal number i+1 Print a space END LOOP // End Inner Loop Print a newline // // Put a statement here to adjust the value of numOnLine for the next time through the Outer Loop // END LOOP // End Outer Loop
Now, we get to the Good Stuff: You want to print some leading spaces for each line, as follows (at least that's what I discovered when I created the table using my trusty text editor):
First line : Number of leading spaces = 0 Second line: Number of leading spaces = 3 Third line : Number of leading spaces = 6 Etc.
How about something like this:
// Same as above, but now there is another variable // leadingSpaces is the number of space chars before the // first digit on each line // The first line: leadingSpaces is equal to 0 // The second line: leadingSpaces is three more than for the previous line // The third line: leadingSpaces is three more than for the previous line // // So, here is the enhanced program in pseudo-code // Set the value numLines equal to, say, 4 Set the value of numOnLine equal to numLines Set the value of leadingSpaces equal to zero for (int i = 0; i < numLines; i++) BEGIN LOOP // Outer loop for (int j = 0; j < leadingSpaces; j++) BEGIN LOOP // First Inner Loop Print a space character END LOOP // End First Inner Loop for (int j = 0; j < numOnLine; j++) BEGIN LOOP // Second Inner Loop Print number for i+1 and print a space END LOOP // End Second Inner Loop // Put a statement here to adjust the value of numOnLine // Put a statement here to adjust the value of leadingSpaces END LOOP // End of Outer Loop
Now, if that description doesn't make sense or it leads to something not consistent with your requirements, change it so that it does make sense and it does lead to a solution. Then (and only then) go back to your computer and whip out some Java code.
Anyhow, see how it goes? Get away from Java for a minute and think (yes think) about what you need to do.
Write it down! A program specification in a natural language might be appropriate. Pseudo-code can lead directly to implementation. Do whatever works best for you.
Then, the actual implementation is a simple. (Or, at least it will be, provided your description is of a reasonable process to begin with.)
The bottom line is this: How the heck can you write a program to carry out a sequence of operations if you haven't articulated the steps in a way that you have complete, detailed understanding of the process?
Once you have written a few (or a few dozen or a few hundred) programs, you may rely on your experience and your memory of previous success stories and skip the pseudo-code part, but it's always possible to go back and use something like that if you ever get stuck again.
I mean, it's always OK to ask, but...
Last edited by Zaphod_b; June 15th, 2012 at 11:30 PM.