My loop, println problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: My loop, println problem

  1. #1
    Join Date
    Oct 2005
    Posts
    1

    My loop, println problem

    Hi guys, this is most definately so easy for the pro's around here, or even the novice. Thing is I just started learning Java at uni and I'm really baffled by this peice of coding. See, I wrote the following.
    What its meant to do is the user inputs a number and depending on that number, thats how many lines will appear in the Command box.

    See, main problem is I can't get the println coding arranged properly.

    Here is the code I wrote...

    Code:
    import javax.swing.JOptionPane;
    
    public class TimesTable
    {
    	public static void main (String [] args)
    	{
    		String numberEntry;
    		int inputtedNo;
    		numberEntry = JOptionPane.showInputDialog("What number should the table go up to?");
    		inputtedNo = Integer.parseInt(numberEntry);
    		{
    			System.out.println("number " + '\t' + '\t' + "times 5" + '\t' + '\t' + "cubed");
    			// for loop
    			for(int i = 0; i<inputtedNo; i++)
    			System.out.println(i*5);
    		}
    	}
    }
    I want 3 rows to appear in the Command box like this:

    number times 5 cubed
    xx xx xx
    xx xx xx
    xx xx xx
    xx xx xx
    xx xx xx

    xx being the calculation... my problem is that I can't make it so the calculation (xx's) appear in the rows like above. I add \t to the code but if I do that, it'll reprint number, times 5, and cubed over and over as well .. hope that makes sense.

    Anyway I've waffled on long enough, if anyone is still confused let me know. I'd LOVE some help on this as I've just about given up.

    Thanks guys!

    Antony

  2. #2
    Join Date
    Oct 2005
    Posts
    2
    If all you're trying to do it move down what you're printing out use /n. that will move the next line you're printing out down a line.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Would you like to try my ColumnMaker

    Here is a test for it
    Code:
        ColumnMaker cm = new ColumnMaker();
        try {
          cm.addLine("this \t is\t a \t test \t case");
          cm.addLine(dF.format(Math.PI)+"\there is\tline\ttwo\talso");
          cm.printContent(System.out);
          
          cm.setColumnGap(7);
          cm.printContent(System.out);
         
          cm.setColumnAlignment(1,ColumnMaker.ALIGN_RIGHT);
          cm.setColumnGap(2);
          System.out.println("\ncol 1 right aligned");
          cm.printContent(System.out);
          
    
          cm.setColumnSizes(new int[]{14,9,7,9,9});
          cm.setColumnGap(2);
          cm.printContent(System.out);
          
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
    It will make aligned column output in a dos shell (i posted the output here
    first and it looked awful)...

    The Class code is attatched
    Attached Files Attached Files
    Last edited by sjalle; 10-20-2005 at 03:07 PM.
    eschew obfuscation

Similar Threads

  1. Problem with Search
    By Irina in forum ASP.NET
    Replies: 0
    Last Post: 11-29-2002, 11:47 PM
  2. Reliability Problem
    By elise in forum Java
    Replies: 0
    Last Post: 10-30-2002, 05:40 AM
  3. Replies: 0
    Last Post: 12-13-2001, 01:06 PM
  4. Arabic problem view
    By Ayman in forum VB Classic
    Replies: 8
    Last Post: 04-03-2000, 09:13 PM
  5. Problem with CryptoAPI and JCE
    By Jason Bock in forum VB Classic
    Replies: 0
    Last Post: 03-21-2000, 07:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center