This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello. I'm writing a program for school. I have the whole thing up and running properly except for the required nested for loop. I'm trying to print a table of double's and it prints out with the proper numbers, but it prints them all twice and I'm at a loss as to why this is. Any help or guidance in the correct direction would be greatly appreciated.
This is the way it is currently printing:
This is the way it's supposed to look:
This is the offensive code:
//loop for printing earings and time into the table
I am aware that there are better ways of actually accomplishing what I'm trying to do here, however the nested for loop is required for this portion of the program. Thanks in advance.
I'm having problems making this website make the tables line up properly, but minus the tab spacing, that is what the tables should look like.
Roger Weyersberg Ii wrote:I'm having problems making this website make the tables line up properly, but minus the tab spacing, that is what the tables should look like.
It's difficult to tell from what you've posted, but I'd bet dollars to doughnuts that it has to do with the fact that you're treating Strings as numbers. Java has several wonderful collections, and java.util.Set is what you want in this case. Take your pairs of numbers and plough them into a Set and output it; the Set takes care of all the business of duplicates. If you need them sorted, there is Collections.sort().
General Tip: try to use the stuff that's given to you for free. If you don't know what that is for Java, have a good look through the java.lang, java.math and java.util packages before you start writing classes that have already been invented. The designers were quite clever chaps.
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
You're not using your column value in the print statement. In your print statement you print out the values for the first and second column of your row and you do this for however many columns there are in that row.
Roger Weyersberg Ii
Joined: Jul 19, 2012
Stuart A. Burkett wrote:You're not using your column value in the print statement. In your print statement you print out the values for the first and second column of your row and you do this for however many columns there are in that row.
When I insert column in the print statement instead of statically assigning which column to print from it spits the same number out in each slot of the table.
so it's like:
which is why I had changed it to print from specific columns.
The inner loop is supposed to append one column at a time, however many columns you have. The problem is that every time through the for-loop, you are appending two columns at a time.
This is why you see the same values twice. If you had three columns, you'd see them thrice. Four columns, you'd see them four times. And so on. Get it?
What's leading you astray is probably a confusion of when you should append the tab and the newline. Hint: the newline should be appended only after the last column has been appended. Also, if you have an extra tab appended to the last column in a line, who cares? It probably won't affect the formatting much.