Help with system.out.println (Java)

  • Thread starter Bardok609
  • 2 comments
  • 502 views
Hey I'm new to the forum I was wondering if someone could help me with a programming assignment.
I'm suppose to print out the following results in the following format and could use a little bit of help on the system.out.prinln. The numbers under the heading GPA, Total Hours, etc are all examples, i already completed the calculations and variable definitions.

Name: John Doe <---already know this, just example
SSN: 123456789 <----already know this, just example
<blank line>
Institution: GPA Total Hours Quality Points
3.23 30 97
<blank line>
Current: Course ID Hours Grade Points
-----------------------------------------------
cs1113 3 4 12
-----------------------------------------------
<blank line>
Cumulative: GPA Total Hours Quality Points
3.30 33 109


The numbers have to line up exactly underneath the headings and I have no idea on how to do this. I don't know how to print the "----" lines either, if someone knows and can help me with this i would greatly appreciate it.
 
Yes the tab character would be good for this "\t"
Try...

public static void main(String[] args)
{

System.out.println("Institution:\tGPA\tTotal Hours\tQuality Points");
System.out.println("\t\t3.23\t30\t\t 97");​

}

Of course you would want to replace the numbers with your variables like
System.out.println("\t\t"+var1+"\t" + var2 + "\t\t" + var3);
This works fine unless the numbers are unexpected sizes, you may have to put in some more tabs to get spacing right.

The line "------" can just be printed like println("-----------------") but if you want to be slightly cleaner you could write a method to print a line like...

public static void printLine(int length)
{
for(int i=0;i<length;i++)
{
System.out.print("-");​
}

System.out.print("\n");​
}

and call it from method main like
printLine(20) to print a line 20 characters long.
You could also replace the print("-") with a print("Ä") for a solid line :)
 
Back