Java - 一个 parseInt 显示错误,另一个不显示
Java - One parseInt shows error, the other one doesn't
我一直在做作业,但我 运行 遇到了一个问题。我的代码从 txt 文件中扫描有关书籍的一些信息(作者、标题等),然后将其打印出来。首先,我尝试打印作者、标题、ISBN 代码、页数,然后是 15*页数的书籍价格。它到目前为止有效,但是我想为每本书添加一个数字,所以它就像一个列表。但是当我修改代码以添加这些数字时,代码不想扫描它们。它说
int cannot be converted to String
出于好奇,我试图将数字扫描为字符串,但随后错误消息说
String cannot be converted to int
我的书class:
public class Book {
private int number;
private String author;
private String title;
private String code;
private int pages;
private static int multiplier = 15;
public Book(String author, String title, String code, int pages, int number) {
this.number = number;
this.author = author;
this.title = title;
this.code = code;
this.pages = pages;
}
@Override
public String toString() {
return author + " - " + title + ", ISBN:" + code + ", " + pages + " pages, Price: " + price() + "Ft";
}
public int price() {
return multiplier * pages;
}
public static int getMultiplier() {
return multiplier;
}
public static void setMultiplier(int multiplier) {
Book.multiplier = multiplier;
}
public int getNumber() {
return number;
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public String getCode() {
return code;
}
public int getPages() {
return pages;
}
}
还有我的"Controller"class:
public class Controller {
void start() {
scanning();
printing("Avilable books: ");
}
private List<Book> books = new ArrayList<>();
private void scanning() {
try {
Scanner fajlScanner = new Scanner(new File("books.txt"));
String row;
String data[];
while (fajlScanner.hasNextLine()) {
row = fajlScanner.nextLine();
data = row.split(";");
//1;J.K. Rowling;Harry Potter and the Philosopher's Stone;1782637594826;342
//this line below gives the error for the data[0]
books.add(new Book(Integer.parseInt(data[0]), data[1], data[2], data[3], Integer.parseInt(data[4])));
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void printing(String title) {
System.out.println(title);
for (Book book : books) {
System.out.println(book);
}
}
}
我的txt内容:
1;J.K. Rowling;Harry Potter and the Philosopher's Stone;1782637594826;342
2;J.R.R. Tolkien;The Fellowship of the Ring;1827493762573;431
3;Stephen King;Needful Things;8274653821647;411
4;Eric Knight;Lassie Come-Home;7263845618293;138
5;Molnár Ferenc;A pál utcai fiúk;9283746192846;194
6;Winston Groom;Forrest Gump;0385231342;228
7;Antoine de Saint-Exupéry;The Little Prince;8362748172649;69
8;Stephen King;Cujo;2918467382914;362
我可以很好地扫描页面,但是 "number" 有一些问题。
检查您是否以正确的顺序向 Book-constructor 提供了正确的参数。
构造函数:
public Book(String author, String title, String code, int pages, int number)
您的对象创建:
new Book(Integer.parseInt(data[0]), data[1], data[2], data[3], Integer.parseInt(data[4]))
你能找出错误吗?
您的输入是:1;J.K。罗琳;哈利波特与魔法石;1782637594826;342
你的构造函数是:
public Book(String author, String title, String code, int pages, int number){
/*.
.
.*/
}
输入的第一个字符是“1”,这是一个整数,但在您的构造函数中,第一个参数是 String
。这就是错误出现的原因。根据构造函数,“1”必须在输入的末尾。
为每本书设置一个编号,使用一个额外的static
变量,将其初始化为1,每次你想添加一本书,为书的编号设置静态变量的当前值,然后++
它。
public class Book {
private static int counter=1;
private int number;
private String author;
private String title;
private String code;
private int pages;
private static int multiplier = 15;
public Book(String author, String title, String code, int pages, int number) {
this.number = counter;
this.author = author;
this.title = title;
this.code = code;
this.pages = pages;
counter++;
}
祝你好运
我一直在做作业,但我 运行 遇到了一个问题。我的代码从 txt 文件中扫描有关书籍的一些信息(作者、标题等),然后将其打印出来。首先,我尝试打印作者、标题、ISBN 代码、页数,然后是 15*页数的书籍价格。它到目前为止有效,但是我想为每本书添加一个数字,所以它就像一个列表。但是当我修改代码以添加这些数字时,代码不想扫描它们。它说
int cannot be converted to String
出于好奇,我试图将数字扫描为字符串,但随后错误消息说
String cannot be converted to int
我的书class:
public class Book {
private int number;
private String author;
private String title;
private String code;
private int pages;
private static int multiplier = 15;
public Book(String author, String title, String code, int pages, int number) {
this.number = number;
this.author = author;
this.title = title;
this.code = code;
this.pages = pages;
}
@Override
public String toString() {
return author + " - " + title + ", ISBN:" + code + ", " + pages + " pages, Price: " + price() + "Ft";
}
public int price() {
return multiplier * pages;
}
public static int getMultiplier() {
return multiplier;
}
public static void setMultiplier(int multiplier) {
Book.multiplier = multiplier;
}
public int getNumber() {
return number;
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public String getCode() {
return code;
}
public int getPages() {
return pages;
}
}
还有我的"Controller"class:
public class Controller {
void start() {
scanning();
printing("Avilable books: ");
}
private List<Book> books = new ArrayList<>();
private void scanning() {
try {
Scanner fajlScanner = new Scanner(new File("books.txt"));
String row;
String data[];
while (fajlScanner.hasNextLine()) {
row = fajlScanner.nextLine();
data = row.split(";");
//1;J.K. Rowling;Harry Potter and the Philosopher's Stone;1782637594826;342
//this line below gives the error for the data[0]
books.add(new Book(Integer.parseInt(data[0]), data[1], data[2], data[3], Integer.parseInt(data[4])));
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void printing(String title) {
System.out.println(title);
for (Book book : books) {
System.out.println(book);
}
}
}
我的txt内容:
1;J.K. Rowling;Harry Potter and the Philosopher's Stone;1782637594826;342
2;J.R.R. Tolkien;The Fellowship of the Ring;1827493762573;431
3;Stephen King;Needful Things;8274653821647;411
4;Eric Knight;Lassie Come-Home;7263845618293;138
5;Molnár Ferenc;A pál utcai fiúk;9283746192846;194
6;Winston Groom;Forrest Gump;0385231342;228
7;Antoine de Saint-Exupéry;The Little Prince;8362748172649;69
8;Stephen King;Cujo;2918467382914;362
我可以很好地扫描页面,但是 "number" 有一些问题。
检查您是否以正确的顺序向 Book-constructor 提供了正确的参数。
构造函数:
public Book(String author, String title, String code, int pages, int number)
您的对象创建:
new Book(Integer.parseInt(data[0]), data[1], data[2], data[3], Integer.parseInt(data[4]))
你能找出错误吗?
您的输入是:1;J.K。罗琳;哈利波特与魔法石;1782637594826;342
你的构造函数是:
public Book(String author, String title, String code, int pages, int number){
/*.
.
.*/
}
输入的第一个字符是“1”,这是一个整数,但在您的构造函数中,第一个参数是 String
。这就是错误出现的原因。根据构造函数,“1”必须在输入的末尾。
为每本书设置一个编号,使用一个额外的static
变量,将其初始化为1,每次你想添加一本书,为书的编号设置静态变量的当前值,然后++
它。
public class Book {
private static int counter=1;
private int number;
private String author;
private String title;
private String code;
private int pages;
private static int multiplier = 15;
public Book(String author, String title, String code, int pages, int number) {
this.number = counter;
this.author = author;
this.title = title;
this.code = code;
this.pages = pages;
counter++;
}
祝你好运