Computer Science Canada How to make Table in Java And clear Screen |
Author: | Slnj [ Tue Sep 28, 2010 4:57 pm ] |
Post subject: | How to make Table in Java And clear Screen |
OK so i have a Major assignment i know how to code it and stuff because it's a question i got from last year when i was working with Turing, but now I'm using java so i don't know how to make a table ( a proper one ) and also need to know if there's a way to clear screen in java i heard some someone in my class there isn't but i wanna make sure. The table has to look something like this : Quote: ---------------- Sales Summary for Maura
--------------------------------------------------------- ------ Item-------Price($)-------Tax($)---------Total($) ------ ---------------------------------------------------------- ------ Socks ----- 2.00 ----------0.30-----------2.30 ------ CD---------10.00----------1.50-----------11.50 ------ Car--------30,000.00---- 4,500.00------34,500.00 ------ Navel-lint--0.05 --------- 0.01-----------0.06 ------ -------------------------------------------------------- ------ Grand Total 30,012.05 4,501.81 34,513.86 And when i tired making it the table was messed up everything wasn't in order and i have to use variables so like socks is basically a string and so on so that's what causes my table to look messed up cuz the letters are sometimes to long , sometimes 2 short and stuff also for the number are to long / short. So can someone help me with this ;o? |
Author: | DemonWasp [ Tue Sep 28, 2010 6:38 pm ] |
Post subject: | RE:How to make Table in Java And clear Screen |
First, there's no clear way of clearing the screen in Java. Java's output stream can be sent to a terminal (like what you're using), a file, a network card, whatever. Most of these don't support any kind of "clear" operation. C and C++ programs use the ncurses utility in most cases; there is a Java equivalent, JavaCurses but that's massive overkill here. There are several ways to format numbers in Java. The simplest is probably to use the printf method. You can find the full documentation here: http://download.oracle.com/javase/6/docs/api/java/io/PrintStream.html#printf%28java.lang.String,%20java.lang.Object...%29 and http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax As an example, you might try: System.out.printf ( "%20s %10.2d %10.2d %10.2d", itemName, price, tax, total ); For more information, refer to the links above. The second one explains the formatting magic used. Basically, printf looks at the string you gave it as the first argument, then looks for anything starting with a % sign. What follows that is a placeholder for something you'll later give it. The first such placeholder corresponds to the first argument to printf after the format string, the second to the second, etc. Try some simple examples in a separate program to get started. |
Author: | Slnj [ Tue Sep 28, 2010 6:46 pm ] |
Post subject: | RE:How to make Table in Java And clear Screen |
Ok thanks it really helped i got one more question i forgot to add and i just thought of it right now . How do i round a number to 2 decimal places and i'm using "Ready to program java " idl i tried other rounding methods but doesn't work on it |
Author: | Slnj [ Tue Sep 28, 2010 6:48 pm ] |
Post subject: | RE:How to make Table in Java And clear Screen |
Oh no :/ the System.out.printf(); is not working on my idl :/ |
Author: | TerranceN [ Tue Sep 28, 2010 6:59 pm ] |
Post subject: | RE:How to make Table in Java And clear Screen |
Are you by any chance using the hsa.Console? The hsa.Console does not use the terminal i/o, instead it has its own methods for i/o. |
Author: | DemonWasp [ Wed Sep 29, 2010 2:14 pm ] |
Post subject: | RE:How to make Table in Java And clear Screen |
If you're using the Console, your best bet is to stop immediately. Never, ever ever use the Console; it's non-standard, not available outside RTP, clumsy and buggy. Ideally, stop using RTP. That's a bit beyond the scope of this thread. Maybe I should make a tutorial about graduating from RTP to any other development environment... |