如何通过Java中的用户输入来搜索特定列?

问题描述 投票:0回答:1

我想提示用户搜索“作者”,如果是,则显示该特定作者撰写的所有书籍。到目前为止,这是我的代码,请参见代码底部。

文本文件存储为:标题-作者-发布者-价格-页面-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 
java arrays search arraylist println
1个回答
0
投票

将文件内容存储在数据结构中。可以根据您的需要以几种方式完成此操作。例如,您可以创建一个Book对象,其中包含标题,作者,发布者,价格,页面和isbn,并将它们存储在数组中。然后,您可以在该数组中进行线性搜索。

© www.soinside.com 2019 - 2024. All rights reserved.