0

So my program is supposed to read a list of names from a URL in order to play guess the name game; however, the program does not seem to be able to read anything from the URL, let alone add it onto an ArrayList. When running the program, all I get is "There are 0 names in this list" meaning that no names had been added from the URL.

When I use the debugger and step into the URL, I get an error saying "can't step, selected thread is not suspended."

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;
   }
}

Tester Method:

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 Answer 1

1

Have you tried opening the URL in a browser? It redirects to a secure protocol (HTTPS). Change your URL to "https://www2.census.gov/topics/genealogy/1990surnames/dist.all.last" and it should work fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, I can't believe I overlooked that simple detail. My program works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.