显示Java中ArrayList中的数据,以列形式显示。

huangapple go评论57阅读模式
英文:

display data java from arraylist in column

问题

我想知道如何从一个数组列表中按列(依次排列)获取元素,就像这样:

BMI:    长度:     重量:
1.69       30     24.25
2.58       55     28.25

这是我的代码:

 @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);
    }

这段代码给我这个结果:
长度: 重量: BMI:
1.7 80.00 28.01, 长度: 重量: BMI:
1.6 55.00 22.03

我不希望长度、重量和BMI一直重复出现...
帮帮我!

英文:

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(&quot;%-17.1f%-15.2f%.2f&quot;,lengte, gewicht,bmi);        
        
    }


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

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

        JOptionPane.showMessageDialog(null,  String.format(&quot;%s&quot;, 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!!!

答案1

得分: 1

你的标题和数据在String.format掩码中合并在一起。这就是为什么标题会重复出现。
你需要将它们分开。所以在循环之前,使用String.format("length:%-9sgewicht:%-8sbmi:", "", "")打印一次标题。对于每行数据,使用String.format("%-17.1f%-15.2f%.2f",lengte, gewicht,bmi)。我认为这样你就会没问题。

但是,如果你想要将整个文本显示在同一个窗格中,可以像这样做:

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);
}
英文:

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(&quot;length:%-9sgewicht:%-8sbmi:&quot;,&quot;&quot;,&quot;&quot;) to print the header once, before the loop. And for each line of data use String.format(&quot;%-17.1f%-15.2f%.2f&quot;,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&lt;BMICalculator&gt; bmilist = new ArrayList&lt;&gt;();
    
    sb.append(String.format(&quot;length:%-9sgewicht:%-8sbmi:\n&quot;, &quot;&quot;, &quot;&quot;));

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

    } while (getUserAnswer() == &#39;J&#39;);
    
    for (BMICalculator item : bmilist) {
        sb.append( item.toString() );
    }
    
    JOptionPane.showMessageDialog(null, sb.toString());
    
    System.out.println(bmilist);
}

答案2

得分: 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"));
    }
}

输出:

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

尝试调整字符串格式和间距以获得所需的列间距。

英文:
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(&quot;%-15s %-4s %10s&quot;, name, &quot;&quot; + age, phone);
}
}
public static void main(String[] args) {
ArrayList&lt;Friend&gt; friends = new ArrayList();
friends.add(new Friend(&quot;F11&quot;, 12, &quot;phone11&quot;));
friends.add(new Friend(&quot;F21&quot;, 21, &quot;phone21&quot;));
friends.add(new Friend(&quot;F321&quot;, 123, &quot;phone321&quot;));
printFriends(friends);
}
private static void printFriends(ArrayList&lt;Friend&gt; friends) {
printHeader() ;
friends.forEach(friend -&gt; System.out.println(friend.toString()));
}
private static void printHeader() {
System.out.println(String.format(&quot;%-15s %-4s %10s&quot;,&quot;Name&quot;,&quot;Age&quot;, &quot;Phone&quot;));
}
}

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

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

huangapple
  • 本文由 发表于 2020年9月30日 03:07:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64126079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定