[英]I'm trying to read this text file
我制作了兩個類,一個文本文件和一個應該打印所有內容的代碼,但無論我做什么,它仍然變成 null? 我正在嘗試使用 Book class 中的 toString 方法和 InventoryTester 中的 Inventory class 中的 printInventory 從文本文件中讀取代碼。
我的第一個代碼源自應該打印的內容。 我制作了一個構造函數,並使用了 setter 和 getter 來制作它。 然后用 toString() 方法結束它。 我制作了一本 class 書,它應該將所有內容打印到 InventoryTester 中的 printInventory。
預訂 class:
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.lang.*;
public class Book {
// Initiate variables
// Note: I used the DecimalFormat so every price is always going to have 2 decimal spaces
private static DecimalFormat df2 = new DecimalFormat("#.##");
private String ISBN;
private String author;
private String title;
private int edition;
private String pubCode;
private int quantity;
private double price;
//constructor
public Book(String isbn, String auth, String ti, int ed, String pc, int qty, double pr)
{
ISBN = isbn;
author = auth;
title = ti;
edition = ed;
pubCode = pc;
quantity = qty;
price = pr;
}
//getters
public String getTitle(){return title;}
public String getAuthor(){return author;}
public double getPrice(){return price;}
public int getEdition(){return edition;}
public String getISBN(){return ISBN;}
public String getpubCode(){return pubCode;}
public int getQuantity(){return quantity;}
//setters
public void changePrice(double newPrice){price = newPrice;}
public boolean changeQuantity(int changeAmt){
changeAmt = quantity + changeAmt;
if (changeAmt < 0) {
return false;
}
else {
return true;
}
}
//This prints out all of the information once the object 'Book' is used
public String toString()
{
String subAuthor = "";
String subTitle = "";
subAuthor = author.substring(0,16);
subTitle = title.substring(0,32);
return "ISBN: " +ISBN + "\nAuthor: " + subAuthor + "\nTitle: " + subTitle + "\nEdition: " + edition + "\nPublisher Code: " + pubCode +"\nQuantity: " +quantity+ "\nPrice: $ " +df2.format(price) + "\n\n";
}
}
然后我制作了一個名為 Inventory 的 class 來管理 Book class 的實例。 但是,我仍然不確定我的大部分代碼是否正確,因為我編譯它沒有任何錯誤。 我的意思是構造函數本身,addBook 方法和changeQuantity 方法。
構造函數接受一個參數,即書籍數組的大小。 它應該創建指定大小的空數組,並將 nextEntry 字段初始化為 0。我這里使用 printInventory 方法。 此方法應使用每本書的 toString() 方法打印圖書 class 中的整個庫存。
庫存 class:
import java.math.RoundingMode;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import java.util.Arrays;
public class Inventory {
private Book[] books;
private int nextEntry;
int questionAmt;
public Inventory(int size){
size = 7;
books = new Book[size];
nextEntry = 0;
}
public boolean addBook(Book theBook) {
if (nextEntry > 27)
{ return false;}
else{
nextEntry++;
books[nextEntry] = theBook;
return true;
}
}
public boolean changeQuantity(String isbn, int changeAmt) {
if (changeAmt < 0) {
return false;
}
else {
return true;
}
}
public void printInventory()
{
for (int i=0; i<books.length; i++) {
System.out.println(books[i].toString());
return;
}
}
然后我添加了名為inventory 的文本文件。 這是代碼中應該顯示的內容,我應該能夠在控制台內編輯它的數量。 我還添加了 28,因為它需要 28 本書。
inventory.txt:
28
013478796X_Tony Gaddis_Starting Out with Java: From Control Structures through Data Structures_4_PE_10_121.75
0321409493_John Lewis_Java Software Solutions: Foundations of Program Design_5_AW_12_94.05
0023606924_Richard Johnsonbaugh_Algorithms_1_PH_1_109.00
0134743350_Harvey Dietel_Java: How to Program, Early Objects_12_PE_12_134.84
0131474340_Ralph Morelli_Java, Java, Java, Object-Oriented Problem Solving_3_PH_4_95.25
0596100469_Alex Martelli_Python in a Nutshell_2_OR_6_39.99
0134802217_Tony Gaddis_Starting Out with Java: From Control Structures through Objects_7_PE_8_118.67
1403946876_Sally Fincher_Studying Programming_1_PM_3_26.59
0596510047_Andy Oram_Beautiful Code: Leading Programmers Explain How They Think_1_OR_5_44.99
0143037889_Ray Kurzweil_The Singularity is Near: When Humans Transcend Biology_1_PG_20_17.70
0135205972_John Lewis_Java Foundations: Introduction to Program Design and Data Structures_5_PE_5_129.99
0131872893_Wanda Dann_Learning to Program with Alice_1_PH_12_47.50
159413962X_Dave Eggers_The Circle_1_AW_4_3.99
1887902996_John Zelle_Python Programming: An Introduction to Computer Science_1_FB_2_26.40
0133356728_Rafael Gonzales_Digital Image Processing_4_PE_3_248.17
1592400876_Lynne Truss_Eats, Shoots & Leaves_1_PG_5_17.70
0072823798_William Collins_Data Structures and the Java Collections Framework_2_MH_6_105.57
0072866098_Allen Tucker_Programming Languages: Principles and Paradigms_2_MH_1_127.50
0534950973_Michael Sipser_Introduction to the Theory of Computation_2_CT_3_98.90
0131496710_Francis Hill_Computer Graphics Using OpenGL 3rd Edition_3_PH_4_112.00
0321173486_Dave Shreiner_OpenGL Programming Guide_5_AW_1_24.00
0072865512_Steven Schach_Object Oriented and Classical Software Engineering_6_MH_9_123.44
0321228383_Michael Kifer_Database Systems: An Application-Oriented Approach_2_AW_3_112.86
1416587787_Cliff Stoll_The Cuckoo's Egg_1_PG_3_13.32
1400032717_Mark Haddon_The Curious Incident of the Dog in the Night-Time_1_VI_10_13.95
006025492X_Maurice Sendak_Where the Wild Things Are_1_HC_6_17.95
0694003611_Margaret Brown_Goodnight Moon_1_HC_138_8.99
069401298X_Arnold Lobel_Frog and Toad Together_1_HC_27_11.55
最后但並非最不重要的一點是,我制作了一個 InventoryTester 文件來讀取文本文件並使用兩個類的代碼打印它。
庫存測試儀:
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import java.awt.Font;
import java.util.Scanner;
import java.io.*;
public class InventoryTester {
public static void main(String[] args) throws IOException {
Scanner inFile= new Scanner(new File("inventory.txt"));
Book books;
String inLine= inFile.nextLine();
int size= Integer.parseInt(inLine);
Inventory myInventory= new Inventory(size);
while (inFile.hasNext()) {
inLine= inFile.nextLine();
String ISBN = inFile.next();
String author = inFile.next();
String title = inFile.next();
int edition = inFile.nextInt();
String pubCode = inFile.next();
int quantity = inFile.nextInt();
double price = inFile.nextDouble();
myInventory.addBook(new Book(ISBN, author, title, edition, pubCode, quantity, price));
String[] tokens = inLine.split("_");
myInventory.printInventory();
}
inFile.close();
}
}
我試圖讓 Java 明白有字符串、整數和雙精度數。 我正在嘗試將使用 arrays 的文本文件轉換為字符串、整數和雙精度。 它應該打印,但代碼認為它是 null。 我錯過了什么嗎?
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Book.toString()" because "this.books[i]" is null
at Inventory.printInventory(Inventory.java:42)
at InventoryTester.main(InventoryTester.java:28)
----jGRASP wedge2: exit code for process is 1.
您可以使用包含書籍的 arraylist 制作 class 來管理您的庫存。 然后將 class 保存在二進制文件中。
class 可能如下所示:
public class BookList implements Serializable
{
private ArrayList<Book> books;
public BookList()
{
books = new ArrayList<>();
}
public void addBook(Book book)
{
this.books.add(book);
}
public void removeBook(Book book)
{
this.books.remove(book);
}
public ArrayList<Book> getAllBooks()
{
return books;
}
然后像這樣保存並加載文件。
File file = new File("inventory.bin")
public void save(BookList bookList){
FileOutputStream out = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(bookList);
}
public BookList load(){
BookList bookList = null;
FileInputStream in = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(in);
bookList = (BookList) ois.readObject();
in.close();
ois.close();
}
那么從文件中讀取應該不會有任何問題。
記住。 列表 class 和書 class 都需要實現 Serializable。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.