格式化:String 无法转换为 String[]
Formatting: String cannot be converted to String[]
Using the data array, create instances of books and store them in a one-dimensional array of Book
, called bookArray
. Using the quantity array, walk through the book array and calculate the total charge per book sold. Print in a dialog box the book title, the book isbn, and the total charge for each book. Then, print the grand total of all books sold.
到目前为止我已经这样做了,但是我在 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);
}
}
}
String.format
returns a String
根据文档。您正在尝试将 String
连接到二维数组。
dataArray +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());
在上面的行中,您可能想从二维数组中访问一个位置,例如 dataArray[0][0]
,然后进行连接。类似于:
dataArray[0][0] +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());
不要用你的dataArray
在消息框中输出东西。 使用一个 String
变量.
for (Book b : bookArray) {
String bookOutput = String.format(" %s, %s ", b.getTitle(), b.getIsbn());
JOptionPane.showMessageDialog(null, bookOutput);
}
更好,在你的 Book
class 中添加一个 toString()
那 returns String.format(" %s, %s ", getTitle(), getIsbn())
然后像 this:
这样循环
for (Book b : bookArray) {
JOptionPane.showMessageDialog(null, b.toString());
}
但是,如果你想输出每本书的价格然后是总价,你的循环看起来更像this:
double totalPrice = 0.0;
for (int i = 0; i < bookArray.length; i++) { //can't use foreach because of use of parallel arrays bookArray & quantityArray
Book b = bookArray[i];
double price = b.getPrice() * quantityArray[i];
totalPrice += price;
String bookStr = String.format(" %s, %s %s", b.getTitle(), b.getIsbn(),
DecimalFormat.getCurrencyInstance().format(price));
JOptionPane.showMessageDialog(null, bookStr);
}
String totalStr = String.format("Total Charge: %s",
DecimalFormat.getCurrencyInstance().format(totalPrice)));
JOptionPane.showMessageDialog(null, totalStr);
Using the data array, create instances of books and store them in a one-dimensional array of
Book
, calledbookArray
. Using the quantity array, walk through the book array and calculate the total charge per book sold. Print in a dialog box the book title, the book isbn, and the total charge for each book. Then, print the grand total of all books sold.
到目前为止我已经这样做了,但是我在 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);
}
}
}
String.format
returns a String
根据文档。您正在尝试将 String
连接到二维数组。
dataArray +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());
在上面的行中,您可能想从二维数组中访问一个位置,例如 dataArray[0][0]
,然后进行连接。类似于:
dataArray[0][0] +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());
不要用你的dataArray
在消息框中输出东西。 使用一个 String
变量.
for (Book b : bookArray) {
String bookOutput = String.format(" %s, %s ", b.getTitle(), b.getIsbn());
JOptionPane.showMessageDialog(null, bookOutput);
}
更好,在你的 Book
class 中添加一个 toString()
那 returns String.format(" %s, %s ", getTitle(), getIsbn())
然后像 this:
for (Book b : bookArray) {
JOptionPane.showMessageDialog(null, b.toString());
}
但是,如果你想输出每本书的价格然后是总价,你的循环看起来更像this:
double totalPrice = 0.0;
for (int i = 0; i < bookArray.length; i++) { //can't use foreach because of use of parallel arrays bookArray & quantityArray
Book b = bookArray[i];
double price = b.getPrice() * quantityArray[i];
totalPrice += price;
String bookStr = String.format(" %s, %s %s", b.getTitle(), b.getIsbn(),
DecimalFormat.getCurrencyInstance().format(price));
JOptionPane.showMessageDialog(null, bookStr);
}
String totalStr = String.format("Total Charge: %s",
DecimalFormat.getCurrencyInstance().format(totalPrice)));
JOptionPane.showMessageDialog(null, totalStr);