如何检索集合中单个字符串的长度?

时间:2014-05-11 09:30:05

标签: java list set

在我目前的作业中,我有一个文本文件,其中包含存储在List中的大量单词。我的一个方法必须将此List中某个(用户指定的)长度的所有单词存储到一个Set中。此外,不同的列表分为两个不同的类。我的问题是:如何检索该列表中单个元素的长度?编辑:这是完整的课程。

文件和列表在客户端类HangmanMain:

中设置如下

//类HangmanMain是Hangman程序的驱动程序。它读到了一个    //在游戏过程中使用的单词词典,然后玩游戏    // 用户。这是一个作弊的刽子手版本,它推迟了一个字    //保持其选项开放您可以更改SHOW_COUNT的设置以查看     //每回合还剩下多少选项。

import java.util.*;  
import java.io.*;

public class HangmanMain {
public static final String DICTIONARY_FILE = "E:/CSC143/Workspace/Assignment2/src/dictionary.txt";
public static final boolean DEBUG = false; // show words left

public static void main(String[] args) throws FileNotFoundException {
    System.out.println("Welcome to the cse143 hangman game.");
    System.out.println();

    // open the dictionary file and read dictionary into an ArrayList
    Scanner input = new Scanner(new File(DICTIONARY_FILE));
    List<String> dictionary = new ArrayList<String>();
    while (input.hasNext()) {
        dictionary.add(input.next().toLowerCase());
    }

    // set basic parameters
    Scanner console = new Scanner(System.in);
    System.out.print("What length word do you want to use? ");
    int length = console.nextInt();
    System.out.print("How many wrong answers allowed? ");
    int max = console.nextInt();
    System.out.println();

    // set up the HangmanManager and start the game
    List<String> dictionary2 = Collections.unmodifiableList(dictionary);
    HangmanManager hangman = new HangmanManager(dictionary2, length, max);
    if (hangman.words().isEmpty()) {
        System.out.println("No words of that length in the dictionary.");
    } else {
        playGame(console, hangman);
        showResults(hangman);
    }
}

// Plays one game with the user
public static void playGame(Scanner console, HangmanManager hangman) {
    while (hangman.guessesLeft() > 0 && hangman.pattern().contains("-")) {
        System.out.println("guesses : " + hangman.guessesLeft());
        if (DEBUG) {
            System.out.println(hangman.words().size() + " words left: "+ hangman.words());
        }
        System.out.println("guessed : " + hangman.guesses());
        System.out.println("current : " + hangman.pattern());
        System.out.print("Your guess? ");
        char ch = console.next().toLowerCase().charAt(0);
        if (hangman.guesses().contains(ch)) {
            System.out.println("You already guessed that");
        } else {
            int count = hangman.record(ch);
            if (count == 0) {
                System.out.println("Sorry, there are no " + ch + "'s");
            } else if (count == 1) {
                System.out.println("Yes, there is one " + ch);
            } else {
                System.out.println("Yes, there are " + count + " " + ch+ "'s");
            }
        }
        System.out.println();
    }
}

// reports the results of the game, including showing the answer
public static void showResults(HangmanManager hangman) {
    // if the game is over, the answer is the first word in the list
    // of words, so we use an iterator to get it
    String answer = hangman.words().iterator().next();
    System.out.println("answer = " + answer);
    if (hangman.guessesLeft() > 0) {
        System.out.println("You beat me");
    } else {
        System.out.println("Sorry, you lose");
    }
}
}

我的方法在HangmanManager类中设置。

import java.util.*; 
import java.io.*;

public class HangmanManager {
private List<String> dictionary;
private int length;
private int max;    
private Set<String> w = new HashSet<String>();
private SortedSet<Character> guess;

Integer L1 = new Integer(length);
public HangmanManager (List<String> dictionary, int length, int max){
    this.dictionary = dictionary;
    this.length = length;
    this.max = max;
}
public Set<String> words (){

    while (scan.hasNext()){
        if (scan.next().equals(L1)){
            w.add(scan.next());
        }
    }
    return w;

}

public int guessesLeft(){

    return max;

}

public SortedSet <Character> guesses(){
    Scanner input = new Scanner (System.in);

    return guess;

}

public String pattern(){
    return null;

}

public int record (char guess){
    return guess;

}



}

Scan只是一个占位符名称。正如您在第一个块中看到的那样,while循环将DICTIONARY_FILE中的所有字符串添加到&#34;字典&#34;名单。我想在第二个块中做的是将一定长度的所有单词添加到w列表中,但我不知道如何从完全不同的类中读取文件(如果这甚至可能)而且我也不知道如何获取所述文件中每个元素的长度。你们有什么想法吗?或者你需要我上传更多信息。

PS:我的任务的完整标题是&#34;邪恶的刽子手。&#34;从我所查看的内容来看,这是一个相当常见的编程任务,所以你应该能够谷歌并获得更多关于我的意思的信息。

PSS:不要介意HangmanManager中的其他方法。我主要专注于&#34; Words&#34;方法

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

好的,假设您拥有设置了所有单词的列表,这里有一些伪代码可以指导您前进。

for every string in set
   check if word is of specified length
      if true:
        add word to set
      if false:
        do nothing

String api可能有一些有用的方法,尤其是确定字符串的长度.. :)

关于问题的后半部分,如果没有更多代码来查看它是如何设置的,如何访问另一个类中的内容有点困难。通常,您使用getter方法来检索字段。