Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   My output does not look right (http://www.programmingforums.org/showthread.php?t=13481)

357mag Jul 5th, 2007 7:50 PM

My output does not look right
 
I've got this program(I wrote it in C++ now transferring it to Java) that has three columns. Under the first column is a number(1, 2, 3, 4, 5) and under the second column is the square, and under the third column is the cube. But for some reason, the numbers under the Cube column don't look right. This is what the output looks like:

:

Value    Square    Cube
  1          1          1
  2          4          8
  3          9          27
  4        16          64
  5        25          125


I want the numbers under the Cube column to print more like they are under the Square column, so they will look more like:

:

Cube
  1
  8
  27
  64
  128


Beats me why it's doing that. Here is my program:

:

public static void main(String args[]) {
       
        System.out.println("Value    Square    Cube");
       
        for (int n = 1; n <= 5; n++)
        {
            System.out.print("  ");
            System.out.print(n);
            System.out.print("        ");
            System.out.print(square(n));
            System.out.print("          ");
            System.out.print(cube(n));
            System.out.println();
        }
             
    }
   
    static int square(int x)
    {
        return x * x;
    }
   
    static int cube(int y)
    {
        return y * y * y;
    }
}


Ben.Dougall Jul 5th, 2007 7:55 PM

because of the space seperators... i would use a \t instead of " ". Once the squares hit 2 digits, it makes another space in the line of output.

Heres what could pose a better solution

:

public static void main(String args[])
{
       
    System.out.println("Value\tSquare\tCube");
     
    for (int n = 1; n <= 5; n++)
    {
        System.out.println( " " + n + "\t" + square(n) + "\t" + cube(n) );
    }
             
}
   
    static int square(int x)
    {
        return x * x;
    }
   
    static int cube(int y)
    {
        return y * y * y;
    }
}


357mag Jul 5th, 2007 8:08 PM

I'll try your suggestion and hopefully it will work. But it doesn't explain why when we get to the Cube column, specifically when the number is 64 and then 125 that the output shifts over to the right even more. There are some numbers under the Square column that also have two digits in them, yet they print just fine.

Ben.Dougall Jul 5th, 2007 8:13 PM

Heres a quick C++ version of your code as i don;t have a Java SDK installed. The \t character can be used in either language.

:

#include <iostream>

int Square ( int &num  )
{
        return num * num;
}

int Cube ( int &num )
{
        return num * num * num;
}

int main ( void )
{
        std::cout << "Num\tSquare\tCube" << std::endl;

        for ( int i = 0; i < 6; ++i )
                std::cout << i << "\t" << Square( i ) << "\t" << Cube( i ) << std::endl;
       
        std::cout << std::endl;
        return 0;
}


Which when run produces:

:

Num    Square  Cube
0      0      0
1      1      1
2      4      8
3      9      27
4      16      64
5      25      125

Press any key to continue . . .


::::::::::::::

Quote:

I'll try your suggestion and hopefully it will work. But it doesn't explain why when we get to the Cube column, specifically when the number is 64 and then 125 that the output shifts over to the right even more. There are some numbers under the Square column that also have two digits in them, yet they print just fine.
AS for that, I have no clue, because indeed the square column should be bumped over as well.

357mag Jul 5th, 2007 8:20 PM

Well here is what I did. I wanted the numbers to print under the centers of the words Value, Square, and Cube, instead of at the left:

:

System.out.println( "  " + n + "\t" + "  " + square(n) + "\t" + " " + cube(n) );

But when I look at the headings Value, Square, and Cube, the word Cube is spaced closer to the word Square, than Square is to Value. I don't know why that is. I want it to look evenly spaced between the words:

:

Value    Square    Cube

But instead it's looking more like this:
:

Value    Square  Cube

I got to try and fix it so it looks evenly spaced.

Ben.Dougall Jul 5th, 2007 8:45 PM

A quick fix would be add one or two spaces after the second \t... You could use if's and do checks to make it better, but for a minor program, I would use the quick fix.

357mag Jul 5th, 2007 10:34 PM

Yeah I guess that would work. Does anyone know why the words are not evenly spaced just by using the "\t" character? Why does the word Cube appear closer to the word Square, than Square does to Value? Seems to me all three words should be evenly spaced.

DaWei Jul 5th, 2007 10:38 PM

Use formatted output, space it as you like.
:

    public static void main (String[] args)
    {
        System.out.println("Value    Square    Cube");
       
        for (int n = 1; n <= 5; n++)
        {
            System.out.format ("%3d%10d%10d\n", n, n*n, n*n*n);
        }
             
    }

Output:
Value    Square    Cube
  1        1        1
  2        4        8
  3        9        27
  4        16        64
  5        25      125


357mag Jul 6th, 2007 12:03 AM

What if I really like the idea of calling the methods to do the work? Is there a way to combine calling the Square and the Cube method, and still using the format function?

357mag Jul 6th, 2007 12:23 AM

I got it figured out. I did this:

:

System.out.format("%3d", n);
                    System.out.format ("%10d", square(n));
                    System.out.format("%10d", cube(n));
                    System.out.println()


My way is more work, but I really wanted to use the methods.


All times are GMT -5. The time now is 2:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC