2017-04-22 1 views
0

내 프로그램은 이름 게임을 추측하기 위해 URL에서 이름 목록을 읽어야합니다. 그러나이 프로그램은 ArrayList에 URL을 추가하는 대신 URL에서 아무 것도 읽을 수없는 것 같습니다. 프로그램을 실행할 때 얻을 수있는 것은 "이 목록에 0 개의 이름이 있습니다"라는 것입니다. URL에서 이름이 추가되지 않았 음을 의미합니다.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(); 
     } 
    } 

답변

1

당신은 브라우저에서 URL을 여는 시도 해 봤나? 보안 프로토콜 (HTTPS)로 리디렉션됩니다. URL을 "https://www2.census.gov/topics/genealogy/1990surnames/dist.all.last"으로 변경하면 정상적으로 작동합니다.

+0

고마워, 나는 그 간단한 세부 사항을 간과했다고 생각할 수 없다. 내 프로그램이 지금 작동합니다. – Abe

관련 문제