我用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!");
}
}
}
答案 0 :(得分:4)
当您需要/需要阅读整行时,您正在阅读下一个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);
}
来自评论:
当我将in.next更改为in.nextLine时,同时运行程序Enter Title和Enter Author`;相反,首先输入标题应出现,并在给出标题输入作者后出现
请注意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);
}
更多信息: