java - 程序未从 URL 读取输入

标签 java

所以我的程序应该从 URL 读取名称列表,以便玩猜名字游戏;但是,该程序似乎无法从 URL 读取任何内容,更不用说将其添加到 ArrayList 中了。运行程序时,我得到的只是“此列表中有 0 个名称”,这意味着尚未从 URL 添加任何名称。

当我使用调试器并单步执行 URL 时,收到一条错误消息“无法单步执行,选定的线程未挂起”。

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class NameGuesser {
   private ArrayList<String> lastNames = new ArrayList<String>();
Scanner input = new Scanner(System.in);
   public void readNames() throws IOException, MalformedURLException {
       // Read the last names

       URL url = new URL(
               "http://www2.census.gov/topics/genealogy/1990surnames/dist.all.last");

       Scanner in = new Scanner(url.openStream());
       while (in.hasNext()) {
           // Read the last name
           String lastName = in.next();
           lastNames.add(lastName);

           // Ignore the statistical information
         //  in.nextDouble();
          // in.nextDouble();
         //  in.nextInt();
       }
       in.close();

       // Print out the number of names in the file and
       // Sort the names using Collections
       Collections.sort(lastNames);
       System.out.println("There are " + lastNames.size() + " names in this list");
   }

   public int guessName() {
       int counter = 0;
       int low = 0;
       int high = lastNames.size();
       int mid = high / 2;
       boolean notFound = true; //variables to hold game info
       String userInput = "";
       while (notFound) { //while the name is not found
           System.out.println("Does your name come before " + lastNames.get(mid) + " in the dictionary? (Y/N), or is " + lastNames.get(mid) + " your name? (S)");
           userInput = input.next(); //ask if it is in the middle
           if (userInput.equalsIgnoreCase("Y")) { //if before, set new upper bound
               high = mid;
               mid = ((high - low)/2) + low;
               counter++;
           } else if(userInput.equalsIgnoreCase("N")){ //if after, set new lower bound
               counter++;
               low = mid;
               mid = ((high - low)/2) + low;
           }
           else{ //if name is found, return counter
               System.out.println("Your name, " + lastNames.get(mid) + ", was found with " + counter + " guesses.");
               input.close();
               return counter;
           }
           if(high == low){ //if upper and lower bounds are equal
               System.out.println("Is your name: " + lastNames.get(mid) + " ? (Y/N)");
               userInput = input.next(); //ask if name is found
               if(userInput.equalsIgnoreCase("Y")){ //if yes, print success, counter, and return counter
                   System.out.println("Your name, " + lastNames.get(mid) + ", was found with " + counter + " guesses.");
                   input.close();
                   return counter;
               }
               else if(userInput.equalsIgnoreCase("N")){ //if no, inform user that guesser failed
                   System.out.println("Name not found. Attempted locating with " + counter + " guesses");
                   input.close();
                   return counter;
               }
           }
       }
       input.close();
       return counter;
   }
}

测试方法:

import java.io.IOException;
import java.net.MalformedURLException;


    public class NameGame {

       public static void main(String[] args) throws MalformedURLException, IOException {
           NameGuesser game = new NameGuesser();

             game.readNames(); 
       }
    }

最佳答案

您是否尝试过在浏览器中打开该 URL?它重定向到安全协议(protocol) (HTTPS)。 将您的 URL 更改为“https://www2.census.gov/topics/genealogy/1990surnames/dist.all.last ”,它应该可以正常工作。

关于java - 程序未从 URL 读取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43563945/

相关文章:

java - Memcache实现设计

java - 如何从字符串中提取变量

java.lang.OutOfMemoryError - new int[Integer.MAX_VALUE];

java - Spring MVC - 无法找到自定义标签的标签库

java - 如何在广播短信接收器中使用数据库方法

java - Univocity 主细节关系 Bean

java - Lombok @Builder 的必需参数

java - 当用户保持相机静止时Android相机自动对焦

java - 使用 retrofit2 时如何修复 "failed to connect"错误?

java - 错误的 ELF 类 : ELFCLASS32 (Possible cause: architecture word width mismatch)