【问题标题】:How to search for a specific column via user input in Java?如何通过 Java 中的用户输入搜索特定列?
【发布时间】:2019-12-11 22:41:38
【问题描述】:

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

文本文件存储为:标题-作者-出版商-价格-页数-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 

【问题讨论】:

  • 请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
  • 您的代码正在读取单个文件。所有数据是否都存储在一个文件中?
  • 是的,某些元素可能丢失或不完整/不正确。 OOP 编程 - Graham Winter - O'Reilly - 32.50 - 120 - 0471974555 Windows XP Unwired - Wei Meng Lee - O'Reilly - 12.99 - 94 - 0596005369 CCDA 考试指南 - Anthony Bruno - Cisco Press - 49.95 - 232 - 0735700745 多媒体通讯 - Fred Halsall - Addison Wesley - 53.99 - 340 - 0201398184

标签: java arrays search arraylist println


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-02-24
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    相关资源
    最近更新 更多