2015-04-04 97 views
1

我的ArrayList每次循环时都添加一个String,但它显示在一条大行上。如何在JOptionPane的新行中显示每个String,因为它会循环?阵列中的元素列表出现在一个新行上

这里是我的报告方法:

public String report() 
    { 
     return(name + "\t" + height + " inches\t" + weight + " pounds\tBMI: " 
       + df.format(bmiValue()) + "\t" + bmiStatus()); 
    } 

这里是与ArrayList中的代码:

ArrayList a = new ArrayList(); 

     if(JFileChooser.APPROVE_OPTION) 
     { 
      File f = choose.getSelectedFile(); 
      Scanner s = new Scanner(new FileInputStream(f)); 
      int l = scanner.nextInt(); 

      for(int i = 0; i < l; i++) 
      { 
       int height = s.nextInt(); 
       int weight = s.nextInt(); 
       String name = s.nextLine(); 

       BmiRecord r = new BmiRecord(name, height, weight); 
       a.add(r.report()); 
      } 

      confirm = JOptionPane.showConfirmDialog(null, a, "BMI Calc", 
      JOptionPane.YES_NO_OPTION); 

回答

1

您的报告()函数应该是:

public String report() 
{ 
    return(name + "\t" + height + " inches\t" + weight + " pounds\tBMI: " 
      + df.format(bmiValue()) + "\t" + bmiStatus() + "\n"); 
} 

通知我添加了\n,它在report()函数返回的字符串的末尾添加了一个新行。

而且ArrayList a = new ArrayList();应改为ArrayList<String> a = new ArrayList<String>();使用的ArrayList的保证类型安全的ArrayList中只能容纳String对象,其中作为你的ArrayList是一个原始类型,可以容纳任何对象,不是类型安全的字符串参数。

相关问题