gpt4 book ai didi

Java 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 18:32:19 27 4
gpt4 key购买 nike

我用 Java 制作了一个简单的图书馆管理应用程序。当我运行它并输入一本书的标题时,例如“Java:如何编程”它崩溃了。但如果我只是输入“Java”而不是“Java:如何编程”,一切都会顺利进行。

出了什么问题?请帮忙。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;

public class MainSystem {

static String fileName = null;
static Library lib = new Library();
static Scanner in = new Scanner(System.in);
static Boolean running = true;

public static void main(String[] args) {
while (running) {
System.out.println("\nEnter 0 for load a library."
+ "\nEnter 1 for save and quit"
+ "\nEnter 2 for list all books in library"
+ "\nEnter 3 for add book to library");

int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the file name to load");
loadScript(in.next());
break;

case 1:
saveAndQuit();
break;
case 2:
System.out.println(lib.toString());
break;
case 3:
addBook();
break;
}
}
System.exit(0);
}

private static void addBook() {
// TODO Auto-generated method stub
int isbn;
String title, author;
double price;

System.out.println("\nEnter Title: ");
title = in.next();

System.out.println("\nEnter Author: ");
author = in.next();

System.out.println("\nEnter ISBN: ");
isbn = in.nextInt();

System.out.println("\nEnter Price: ");
price = in.nextDouble();

Book b = new Book(isbn, title, author, price);
lib.addBook(b);
}

private static void saveAndQuit() {
// TODO Auto-generated method stub
System.out.println("Enter file name: ");
fileName = in.next() + ".ser";
running = false;
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(fileName);
out = new ObjectOutputStream(fos);
out.writeObject(lib);
fos.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private static void loadScript(String name) {
// TODO Auto-generated method stub
FileInputStream fis = null;
ObjectInputStream in = null;
File file = new File(name + ".ser");
if (file.exists()) {
try {
fis = new FileInputStream(file);
in = new ObjectInputStream(fis);
lib = (Library) in.readObject();
fis.close();
in.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} else {
System.out.println("\nThe file does not exist!");
}
}

}

最佳答案

当您想要/需要读取整行时,您正在读取下一个String。更改此:

in.next();

in.nextLine();

当询问您的图书数据时:

private static void addBook() {
// TODO Auto-generated method stub
int isbn;
String title, author;
double price;

System.out.println("\nEnter Title: ");
title = in.nextLine();

System.out.println("\nEnter Author: ");
author = in.nextLine();

System.out.println("\nEnter ISBN: ");
isbn = in.nextInt();

System.out.println("\nEnter Price: ");
price = in.nextDouble();

Book b = new Book(isbn, title, author, price);
lib.addBook(b);
}
<小时/>

来自评论:

When I change in.next to in.nextLine, while running the program Enter Title and Enter Author` appear simultaneously; instead first Enter Title should appear and after giving a Title Enter Author should appear

请注意,nextLine 将消耗所有输入,直到 Scanner 找到新的断线输入。这意味着您必须在读取书籍数据之前添加额外的 nextLine():

private static void addBook() {
// TODO Auto-generated method stub
int isbn;
String title, author;
double price;
in.nextLine(); //consume the data...

System.out.println("\nEnter Title: ");
title = in.nextLine();

System.out.println("\nEnter Author: ");
author = in.nextLine();

System.out.println("\nEnter ISBN: ");
isbn = in.nextInt();

System.out.println("\nEnter Price: ");
price = in.nextDouble();

Book b = new Book(isbn, title, author, price);
lib.addBook(b);
}

更多信息:

Scanner.nextLine documentation

关于Java 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23618883/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com