java - 如何通过 Java 中的用户输入搜索特定列?
问题描述
我想提示用户搜索“作者”,如果是,则显示该特定作者撰写的所有书籍。到目前为止,这是我的代码,请参阅我的代码底部。
文本文件存储为:标题 - 作者 - 出版商 - 价格 - 页面 - isbn
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
public class AS2 {
public static void main(String[] args)
{
String text;
String filename;
String[] splitText = new String[6]; //use this array to store data when we split a line of text
// input the file name first
Scanner keyboard = new Scanner (System.in);
System.out.println("please enter the file name you want to read data from:");
filename = keyboard.nextLine();
try {
File Fileobject = new File (filename);
Scanner fileReader = new Scanner (Fileobject);
//System.out.println("Title \t Author \t Publisher \t Price \t Pages \t ISBN ");
while(fileReader.hasNext())
{
text = fileReader.nextLine(); //Read a line of data from text file
// first name and last name are separated by a semicolon
// Line below separates the content of line of data read by semicolon & store them in the array
splitText = text.split("-");
//Check that splitText has 6 elements, if it has fewer then continue with the *next* iteration
if (splitText.length<6) {
System.out.println("Data is missing from this book");
//Keep searching and repeat the while loop from the top
continue;
}
if (splitText[0].trim().length() == 0) {
System.out.println("Title does not exist");
}
if (splitText[1].trim().length() == 0) {
System.out.println("author does not exist");
}
if (splitText[2].trim().length() == 0) {
System.out.println("publisher does not exist");
}
boolean isValidPrice = true;
try {
Double.parseDouble(splitText[3]);
}
catch (Exception e) {
isValidPrice = false;
}
if (!isValidPrice) {
System.out.println("Book price may not be a numeric value" + splitText[3]);
}
if (splitText[4].trim().length() == 0) {
System.out.println("Book pages not numerical");
}
if (splitText[5].trim().length() < 10) {
System.out.println("Invalid ISBN");
}
// Assuming the line of data read from file has first name & last name
// Once the data is split, there should be two items in the array
// Output the contents of array elements
System.out.println(String.format("%-12s %s", "Title:", splitText[0]) );
System.out.println(String.format("%-12s %s", "Author:", splitText[1]) );
System.out.println(String.format("%-12s %s", "Publisher:", splitText[2]) );
System.out.println(String.format("%-12s %s", "Price:", splitText[3]) );
System.out.println(String.format("%-12s %s", "Pages:", splitText[4]) );
System.out.println(String.format("%-12s %s", "ISBN:", splitText[5]) );
System.out.println("------------------------------------" );
}//End of while loop
fileReader.close();
}//End of try
catch(FileNotFoundException e)
{
//System.out.println("Invalid file name, file does not exist");
}
//System.out.println("Do you want to search for an Author?");
//String answer = input.nextLine();
// (answer.equals("yes") answer.equals("y")); {
// System.out.println("Enter author name");
//print out all books with that author name (splitText1)
}
}
输出:
:Prompt user to search for Author
Yes or No
Enter Authors name
Print out all books with that authors name
解决方案
将文件的内容存储在数据结构中。这可以根据您的需要以多种方式完成。例如,您可以创建一个Book
包含标题、作者、出版商、价格、页面和 isbn 的对象,并将它们存储在一个数组中。然后,您可以在该数组中进行线性搜索。
推荐阅读
- ios - 使用外部试飞用户
- python - 无法同时使用 cv2 和 numpy 向图像添加蒙版
- github - 有没有办法自定义 GitHub 比较视图使用的差异算法?
- java - 当我重新运行程序时,为什么我的客户端不通过套接字向服务器发送数据?
- c++ - c ++将管道转换为逗号分隔
- ruby-on-rails - 如何在模块中的 Controller 中添加回调“after_create”?
- sql - 当免费配额不允许时,有什么方法可以在 bigquery 上运行大型查询的一部分?
- flutter - SliverOverlapAbsorber 能够接受多个条子
- python - 使用 Tkinter() 的按钮大小问题:python
- python-3.x - 异步循环函数停止