格式化:字符串无法转换为String []

时间:2016-02-29 15:10:11

标签: java arrays formatting joptionpane

  

使用数据数组,创建书籍实例并将其存储在Book的一维数组中,称为bookArray。使用数量数组,浏览书籍数组并计算每本书销售的总费用。在对话框中打印书名,书籍以及每本书的总费用。然后,打印出售的所有书籍的总数。

到目前为止,我已经完成了此操作,但我遇到了JOptionPane和格式问题。

错误

$ javac BookTest.java
BookTest.java:41: error: incompatible types: String cannot be converted to String[][]
        dataArray +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());
                                 ^
1 error

代码:

import javax.swing.JOptionPane;

public class BookTest { 
   public static void main(String args[]) {
      String dataArray  [][]=
         {{ "Fiction", "Abraham Lincoln Vampire Hunter", "Grahame-Smith", "Wiley", "NY", "978-0446563079", "13.99", "222"},
          {"Fiction", "Frankenstein", "Shelley", "Prescott", "GA", "978-0486282114", "7.99", "321"},
          {"NonFiction", "Life of Kennedy", "Jones", "Pearson", "MT", "758-29304566", "12.90", "biography"},
          {"Fiction", "Dracula", "Stoker", "Addison", "CA", "978-0486411095","5.99", "145"},
          {"Fiction", "Curse of the Wolfman", "Hageman", "Wesley", "MA", "B00381AKHG", "10.59", "876"},
          {"NonFiction", "How to Pass Java", "Willis", "Wiley"," NY", "444-395869790", "1.99", "technology"},
          {"Fiction", "The Mummy", "Rice", "Addision", "CA", "978-0345369949", "7.99", "954"},
          {"NonFiction", "History of Texas", "Smith", "Prescott", "CA", "123-683947687", "9.75", "history"}
         };

      Book bookArray[] = new Book [8];

      for (int i = 0; i < bookArray.length; i++) {
         if (dataArray[i][0].equals("Fiction")) {
            Publisher p = new Publisher(dataArray[i][3], dataArray[i][4]);
            bookArray[i] = new Fiction(dataArray[i][1], dataArray[i][2], dataArray[i][5],
               p, Double.parseDouble(dataArray[i][6]), dataArray[i][7]);
         } else {
            Publisher p = new Publisher(dataArray[i][3], dataArray[i][4]);
            bookArray[i] = new NonFiction(dataArray[i][1], dataArray[i][2], dataArray[i][5],
               p, Double.parseDouble(dataArray[i][6]), dataArray[i][7]);
         }
      }

      for (Book b:bookArray) {
        dataArray +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());
        JOptionPane.showMessageDialog(null, dataArray);     
      }
   }        
}

2 个答案:

答案 0 :(得分:1)

String.format根据文档返回String。您正在尝试将String连接到2d数组。

dataArray +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());

在上面的行中,您可能希望从2d数组访问某个位置,例如dataArray[0][0],然后进行连接。类似的东西:

dataArray[0][0] +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());

答案 1 :(得分:1)

请勿使用dataArray输出消息框中的内容。 使用 String 变量

for (Book b : bookArray) {
    String bookOutput = String.format(" %s, %s ", b.getTitle(), b.getIsbn());
    JOptionPane.showMessageDialog(null, bookOutput);
}

更好,将toString()添加到返回Book的{​​{1}}类中,然后按进行循环:

String.format(" %s, %s ", getTitle(), getIsbn())

然而,如果您想输出每本书的价格,然后输出总价,那么您的循环将更像 this

for (Book b : bookArray) {
    JOptionPane.showMessageDialog(null, b.toString());
}