简体   繁体   English

如何正确打开新的扫描仪并进行输入

[英]How to properly open a new Scanner and take input

I recently stumbled upon the JSoup library, so I decided to experiment with hit by creating a google query program. 我最近偶然发现了JSoup库,因此我决定通过创建一个google查询程序来尝试hit。

The idea is to type in a Google search, take in the number of queries you want to display, display them, then ask the user for one more integer for input, which is the index that's displayed next to the link. 想法是输入Google搜索,输入要显示的查询数量,显示查询, 然后要求用户输入一个整数,该整数是链接旁边显示的索引。

The problem is that the new Scanner is never called. 问题在于从未调用过新的扫描仪。 It prints the prompt and closes. 它打印提示并关闭。

NOTE : I know I can just go to google myself and search. 注意 :我知道我可以自己去Google搜索。 I'm just experimenting with this new library that scratched that part of my brain that makes me want to look further into something. 我只是在试验这个新的库,该库划伤了我的大脑,使我想进一步研究某些东西。

Here is the code, and the output -- Sorry if it's sloppy. 这是代码,以及输出-对不起,很抱歉。 Still learning... : 还在学习...

import java.io.IOException;
import java.util.Scanner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class GoogleSearchJava {

    static int index;
    static String linkHref;

    public static final String GOOGLE_SEARCH_URL = "https://www.google.com/search";

    public static void main(String[] args) throws IOException {

        //GET INPUT FOR SEARCH TERM

        Scanner input = new Scanner(System.in);
        System.out.print("Search: ");
        String searchTerm = input.nextLine();
        System.out.print("Enter number of query results: ");
        int num = input.nextInt();

        String searchURL = GOOGLE_SEARCH_URL + "?q=" + searchTerm + "&num=" + num;

        //NEED TO DEFINE USER AGENT TO PREVENT 403 ERROR.
        Document document = Jsoup.connect(searchURL).userAgent("Mozilla/5.0").get();

        //OPTION TO DISPLAY HTML FILE IN BROWSWER. DON'T KNOW YET.
        //System.out.println(doc.html());

        //If google search results HTML change the <h3 class="r" to <h3 class ="r1"
        //need to change below stuff accordingly
        Elements results = document.select("h3.r > a");

        index = 0;
        String news = "News";
        for (Element result : results) {

            index++;
            linkHref = result.attr("href");
            String linkText = result.text();
            String pingResult = index + ": " + linkText + ", URL:: " + linkHref.substring(6, linkHref.indexOf("&"));
            if (pingResult.contains(news)) {
                System.out.println("FOUND " + "\"" + linkText + "\"" + "NO HYPERTEXT FOR NEWS QUERY RESULTS AT THIS TIME. SKIPPED INDEX.");
                System.out.println();
            } else {
                System.out.println(pingResult);
            }
        }
        System.out.println();
        System.out.println();


        goToURL(linkHref, input);
    }

    public static int goToURL(String hRef, Scanner input) {

        try {

            System.out.print("Enter Index (i.e. 1, 2, etc) you wish to visit, 0 to exit: ");

            int newIndex = input.nextInt();

            for (int i = 0; i < index; i++) {

                if (newIndex == index) {
/*
RUNNING LINUX COMMAND WITH RUNTIME CLASS TO COCANTENATE THE HYPERLINK SUBSTRING
*/
                    Process process = Runtime.getRuntime().exec("xdg-open " + hRef.substring(6, hRef.indexOf("&"))); 
                    process.waitFor();
                    break;
                } else if (newIndex == 0) {
                    System.out.println("Shutting program down.");
                    System.exit(0);
                }
            }
        } catch (Exception e) {
            System.out.println("ERROR while parsing URL");
        }
        return index;
    }
}

HERE IS THE OUTPUT It stops before the new Scanner can take input 输出这里在新的扫描仪可以输入之前停止

Search: Oracle
Enter number of query results: 3
1: Oracle | Integrated Cloud Applications and Platform Services, URL:: =http://www.oracle.com/
2: Oracle Corporation - Wikipedia, the free encyclopedia, URL:: =https://en.wikipedia.org/wiki/Oracle_Corporation
3: Oracle (@Oracle) | Twitter, URL:: =https://twitter.com/oracle%3Flang%3Den


Enter Index (i.e. 1, 2, etc) you wish to visit, 0 to exit: Shutting program down.

Process finished with exit code 0

As you can see, it goes straight to the else statment to shut the program down. 如您所见,它直接进入else语句以关闭程序。 Any help would be greatly appreciated. 任何帮助将不胜感激。 This is a fun project, and I look forward to completing it. 这是一个有趣的项目,我期待完成它。

Per the suggestion of an SO team member, I asked why Scanner was not asking for input. 根据SO团队成员的建议,我问为什么Scanner不要求输入。 Technically speaking, I fixed the problem with the program stopping BEFORE getting input. 从技术上讲,我解决了程序获取输入之前停止的问题。 Though a problem still exists where it is not actually processing the input, the previous problem was fixed and here is my solution. 尽管仍然没有实际处理输入的问题仍然存在,但先前的问题已解决,这是我的解决方案。

I did not close the original Scanner, and added the Scanner as a parameter to my "goToURL" method. 我没有关闭原始扫描仪,而是将扫描仪作为参数添加到“ goToURL”方法中。 I also removed an else statement that was closing the program, as the input to allow the program to keep running is still buggy. 我还删除了正在关闭程序的else语句,因为允许程序继续运行的输入仍然有问题。 Nonetheless, here is the "working" code that at least solves the original problem. 尽管如此,这里的“工作”代码至少可以解决原始问题。

Additionally, I placed the String elements (pingResult) into an ArrayList to improve the loop structure in the goToURL method. 另外,我将String元素(pingResult)放入ArrayList中,以改善goToURL方法中的循环结构。 I felt this was a decent way to go about using a simple data structure for accessing elements: 我觉得这是使用简单的数据结构访问元素的一种不错的方式:

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class GoogleSearchJava {

    static int index;
    static String linkHref;

    public static final String GOOGLE_SEARCH_URL = "https://www.google.com/search";

    public static void main(String[] args) throws IOException {

        //GET INPUT FOR SEARCH TERM

        Scanner input = new Scanner(System.in);
        System.out.print("Search: ");
        String searchTerm = input.nextLine();
        System.out.print("Enter number of query results: ");
        int num = input.nextInt();

        String searchURL = GOOGLE_SEARCH_URL + "?q=" + searchTerm + "&num=" + num;

        //NEED TO DEFINE USER AGENT TO PREVENT 403 ERROR.
        Document document = Jsoup.connect(searchURL).userAgent("Mozilla/5.0").get();

        //OPTION TO DISPLAY HTML FILE IN BROWSWER. DON'T KNOW YET.
        //System.out.println(doc.html());

        //If google search results HTML change the <h3 class="r" to <h3 class ="r1"
        //need to change below stuff accordingly
        Elements results = document.select("h3.r > a");

        index = 0;
        String news = "News";
        /*
        THIS WILL ADD THE pingResult STRINGS TO AN ARRAYLIST
        */
        ArrayList<String> displayResults = new ArrayList<>();
        for (Element result : results) {
            index++;
            linkHref = result.attr("href");
            String linkText = result.text();
            String pingResult = index + ": " + linkText + ", URL:: " + linkHref.substring(6, linkHref.indexOf("&")) + "\n";

            if (pingResult.contains(news)) {
                System.out.println("FOUND " + "\"" + linkText + "\"" + "NO HYPERTEXT FOR NEWS QUERY RESULTS AT THIS TIME. SKIPPED INDEX.");
                System.out.println();
            } else {
                displayResults.add(pingResult);
            }
        }
        for(String urlString : displayResults) {
            System.out.println(urlString);
        }
        System.out.println();
        System.out.println();


        goToURL(linkHref, input, displayResults);
    }

    public static int goToURL(String hRef, Scanner input, ArrayList<String> resultList) {

        try {

            System.out.print("Enter Index (i.e. 1, 2, etc) you wish to visit, 0 to exit: ");

            index = input.nextInt();

            for (String string : resultList) {

                if (string.startsWith(Integer.toString(index))) {

                    Process process = Runtime.getRuntime().exec("xdg-open " + hRef.substring(6, hRef.indexOf("&")));
                    process.waitFor();
                }
            }
        } catch (Exception e) {
            System.out.println("ERROR while parsing URL");
        }
        return index;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM