0

I would like to know how am I supposed to get the elements from a arraylist in a column(under each other) like this:

BMI:   Length:   gewicht:
1.69       30     24.25
2.58       55     28.25

This is my code:

 @Override
    public String toString() {
        return String.format("%-17.1f%-15.2f%.2f",lengte, gewicht,bmi);        
        
    }


    public static void main(String[]args){
        ArrayList<BMICalculator> bmilist = new ArrayList<>();
JOptionPane.showMessageDialog(null, String.format("length:%-9sgewicht:%-8sbmi:","","") );

        do {
            double lengte = getLength("Geef de lengte in Meters:");
            double gewicht = getGewicht("Geef het gewicht in Kg:");
            BMICalculator bmi = new BMICalculator(lengte, gewicht);
            bmi.setBmi(bmi.calculateBMI());
            bmilist.add(bmi);           
            
            JOptionPane.showMessageDialog(null, String.format("%s", bmi.toString()));
        } while(getUserAnswer() == 'J');

        JOptionPane.showMessageDialog(null,  String.format("%s", bmilist.toString()));
        System.out.println(bmilist);

    }

This code gives me this: length: gewicht: Bmi: 1,7 80,00 28,01, length: gewicht: bmi: 1,6 55,00 22,03

I dont want that length, gewicht and bmi keeps repeating... help!!!

3
  • have you tried \t? Commented Sep 29, 2020 at 19:13
  • yes, I get the data under each other but the words length, gewicht and BMI keeps repeating... Commented Sep 29, 2020 at 19:15
  • Does this answer your question? Java String Formatting (Columns) Commented Sep 30, 2020 at 13:50

2 Answers 2

1

Your header and data are merged together in that String.format mask. That's why the header keeps repeating. You'll need to separate them. So use String.format("length:%-9sgewicht:%-8sbmi:","","") to print the header once, before the loop. And for each line of data use String.format("%-17.1f%-15.2f%.2f",lengte, gewicht,bmi). That way I think you will be fine.

But, if what you want is the whole text into the same pane, it would be something like this:

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    ArrayList<BMICalculator> bmilist = new ArrayList<>();
    
    sb.append(String.format("length:%-9sgewicht:%-8sbmi:\n", "", ""));

    do {
        double lengte = getLength("Geef de lengte in Meters:");
        double gewicht = getGewicht("Geef het gewicht in Kg:");
        BMICalculator bmi = new BMICalculator(lengte, gewicht);
        bmi.setBmi(bmi.calculateBMI());
        bmilist.add(bmi);

    } while (getUserAnswer() == 'J');
    
    for (BMICalculator item : bmilist) {
        sb.append( item.toString() );
    }
    
    JOptionPane.showMessageDialog(null, sb.toString());
    
    System.out.println(bmilist);
}
Sign up to request clarification or add additional context in comments.

6 Comments

"String.format("%-17.1f%-15.2f%.2f",lengte, gewicht,bmi)" should be the one in your "toString()" method. "String.format("length:%-9sgewicht:%-8sbmi:","","")" inside the "main" method in your example, before the "do {" line
Please, keep in mind that, for your case, the "toString" method cannot solve everything. The header line must be printed outside the "toString" method. The System.out.println( String.format("length:%-9sgewicht:%-8sbmi:","","") ); will certainly print the header. Place it any where before start the loop.
I putted: String.format("length:%-9sgewicht:%-8sbmi:","","") inside the "Tostring" and one line before the "Do", I added...JOptionPane(null, String.format("length:%-9sgewicht:%-8sbmi:","","") );.... But it still doesnt work...
I cant send the whole code through here... Can I send it to you?
Maurizio, just edited my question with those 2 lines you told me to add.
|
1
import java.util.*;

public class Main {


static class Friend {
String name;
int age;
String phone;

public Friend(String name, int age, String phone) {
  this.name = name;
  this.age = age;
  this.phone = phone;
}

@Override
public String toString() {
return String.format("%-15s %-4s %10s", name, "" + age, phone);
}
}

public static void main(String[] args) {

ArrayList<Friend> friends = new ArrayList();
friends.add(new Friend("F11", 12, "phone11"));
friends.add(new Friend("F21", 21, "phone21"));
friends.add(new Friend("F321", 123, "phone321"));
printFriends(friends);
}

private static void printFriends(ArrayList<Friend> friends) {
printHeader() ;
friends.forEach(friend -> System.out.println(friend.toString()));
}

private static void printHeader() {

System.out.println(String.format("%-15s %-4s %10s","Name","Age", "Phone"));

}

}

Output:

Name            Age       Phone
F11             12      phone11
F21             21      phone21
F321            123    phone321

Play with String format and spacing to get desired column spacing.

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.