有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中链接文件列表并检索输入

我目前正在使用Wolfam应用程序用Java编程一个搜索引擎,我将该应用程序作为另一个类中的对象。我创建了两个文件列表,一个用于用户已经键入的问题,另一个用于应用程序生成的答案。当我创建了一个try循环,在控制台上显示问题是否被提出时。 我的问题是,如果问题已经被问到,那么如何使用答案列表文件检索该问题的答案,而不是使用应用程序下载答案? 我尝试了所有我认为多列表数组可以做到的事情,但我对这个主题的记忆有点模糊,我尝试创建了一个方法,可以在get(I)的try循环中使用;但我不知道如何将正确答案与问题联系起来。 我的代码是:

public class Assignment  {
    public static void main(String[] args)throws  IOException, FileNotFoundException {

        String s1 = JOptionPane.showInputDialog ("please type in your question");

        File q = new File("Questions.List");
        File a = new File("Answers.List");
        FileWriter w = new FileWriter(q, true);
        PrintWriter  p = new PrintWriter(w);
        p.println(s1);
        p.close();

        // Create an instance of the Wolfram Alpha API - including your AppID key       
        WolframAlpha WOLFRAM_ALPHA = new WolframAlpha("apple_id");

        // Sample question - capturing the response from Wolfram Alpha in a String called response, and printing to console
        String response = WOLFRAM_ALPHA.query(s1);
        Scanner Ans = new Scanner (a);

        try {
            Scanner scanner = new Scanner(q);

            int lineNum = 0;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                lineNum++;
                if(line.contains(s1))  
                    System.out.println( "This is question number" + lineNum + " " + "And has been asked previously ");
                if (!q.equals(s1))                  
                    break;          
            }
            String [] v =response.split("<section><title>");

            for(int i = 0; i<v.length; i++) {
                if(v[i].contains("Result"))
                    System.out.println("Response: " + i + v[i]);
                else if (v[i].contains("formula"))
                    System.out.println("Response: " + i + v[i]);
                else if (v[i].contains("Response"))
                    System.out.println("Response: " + i + v[i]);
                else if (v[i].contains("Definition"))
                    System.out.println("Response: " + i + v[i]);

                FileWriter wr = new FileWriter(a, true);
                PrintWriter  pr = new PrintWriter(wr);
                pr.println(i + v[i]);
                pr.close();
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

共 (0) 个答案