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!

3
  • 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 Commented Feb 3, 2016 at 12:51
  • 2
    Check out String.format, will lead to prettier code in this case.. Commented Feb 3, 2016 at 12:53
  • Duplicate stackoverflow.com/questions/2745206/… Commented Feb 3, 2016 at 13:03

3 Answers 3

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));
     }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.