0

I'm looking for a way to create a table that looks like this: Table to be created.

This is what my code looks like so far:

public class YourName {
    public static void main(String[] args) {

        System.out.println("Tecken \tGrundamne \tAtomnummer \tAtommassa \tMasstal");
        System.out.println("==================================================================");
        System.out.print("Ag \tSilver");
        int agNumber = 47;
        float agMass = 107.8682f;
        float agMasstal = Math.round (agMass - agNumber);
        System.out.print(agNumber);
        System.out.print(agMass);
        System.out.print(agMasstal);

        System.out.print("Au \tGuld");
        int auNumber = 79;
        float auMass = 196.96654f;
        float auMasstal = Math.round (auMass - auNumber);
        System.out.print(auNumber);
        System.out.print(auMass);
        System.out.print(auMasstal);

        System.out.print("C \tKol");
        int cNumber = 6;
        float cMass = 12.01f;
        float cMasstal = Math.round (cMass - cNumber);
        System.out.print(cNumber);
        System.out.print(cMass);
        System.out.print(cMasstal);
    }
}

3 questions:

  1. How can I get each print on a new line? As you can tell from the image, I want to have the last line as C [tab] Kol [tab] 6 [tab] 12.01 [tab] 6, for instance.
  2. How can I make tabs between each print?
  3. When I compile and run, it seems that the Math.round includes a single decimal, and I would like it to be without, as in the attached image.

Please do not mind the Swedish headings and words.

Thanks a lot!

hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
wiro
  • 138
  • 2
  • 17
  • Ok so I realized my own mistake and changed this: `System.out.print(agMasstal); System.out.print(auMasstal); System.out.print(cMasstal);` to `System.out.println(agMasstal); System.out.println(auMasstal); System.out.println(cMasstal);` which solved question 1 – wiro Feb 03 '16 at 12:51
  • 2
    Check out `String.format`, will lead to prettier code in this case.. – Tobb Feb 03 '16 at 12:53
  • Duplicate http://stackoverflow.com/questions/2745206/output-in-a-table-format-in-javas-system-out – yuria Feb 03 '16 at 13:03

3 Answers3

1

You can pretty up your code quite a bit using String.format.

public class MyClass {
    private static final String HEADER_LINE = "Tecken \tGrundamne \tAtomnummer \tAtommassa \tMasstal"
    private static final final String SEPARATOR = "==================================================================";
    final String LINE_TEMPLATE = "%s \t%s \t %d\t %f\t %d";

    public static void main(String... args) {
        System.out.println(HEADER_LINE);
        System.out.println(SEPARATOR);
        System.out.println(String.format(LINE_TEMPLATE, "Ag", "Silver", agNumber, agMass, agMasstall));
        System.out.println(String.format(LINE_TEMPLATE, "Au", "Gold", auNumber, auMass, auMasstall));
     }
}
Tobb
  • 11,850
  • 6
  • 52
  • 77
0
  1. either use `System.out.println("text"); which inserts a linebreak after the text, or insert the linebreak, where you want it with "\n".
  2. insert "\t", e.g. System.out.print(cNumber+"\t");
  3. use long instead of float for the rounded values, since float is a floating point number.
ctst
  • 1,610
  • 1
  • 13
  • 28
0
  1. How can I get each print on a new line? As you can tell from the image, I want to have the last line as C [tab] Kol [tab] 6 [tab] 12.01 [tab] 6, for instance.

    Answer: Use System.out.println(agNumber);

  2. How can I make tabs between each print?

    Answer: Use System.out.println(agNumber+"\t")

  3. When I compile and run, it seems that the Math.round includes a single decimal, and I would like it to be without, as in the attached image.

    Answer: use long instead of float.

Matt
  • 74,352
  • 26
  • 153
  • 180
Ranjeet
  • 636
  • 6
  • 12