Java 如何将数据从文件中获取到数组中?

Java 如何将数据从文件中获取到数组中?,java,Java,在过去的两个小时里,我一直在做一个任务,但没能找到答案。作为参考,我将发布下面的说明,然后解释我所做的工作 编写一个实现StringSet接口的Java类(请参见 随附文本文件)。实例变量之一必须是 保存数据的字符串数组;你可以决定什么,如果有的话 您需要的其他实例变量。您还需要实现 所需的方法、一个或多个构造函数以及您需要的任何其他方法 认为有必要。我还提供了一个测试人员类,您应该 能够使用运行您的代码 到目前为止,我已经创建了一个名为MyStringSet的接口实现。我已经将接口中的所有方法

在过去的两个小时里,我一直在做一个任务,但没能找到答案。作为参考,我将发布下面的说明,然后解释我所做的工作

编写一个实现StringSet接口的Java类(请参见 随附文本文件)。实例变量之一必须是 保存数据的字符串数组;你可以决定什么,如果有的话 您需要的其他实例变量。您还需要实现 所需的方法、一个或多个构造函数以及您需要的任何其他方法 认为有必要。我还提供了一个测试人员类,您应该 能够使用运行您的代码

到目前为止,我已经创建了一个名为MyStringSet的接口实现。我已经将接口中的所有方法都放到了我的实现中,并且已经编写了我认为可以工作的代码。我的主要问题是,我不知道如何将调用的main方法中的数据放入数组中。用户输入一个文件,然后它应该返回单词计数和其他方法。由于该文件是从tester类调用的,因此我需要将该数据存储到我已经创建的数组或数组列表中。下面我列出了我当前的实现和我使用的tester类。非常感谢您的帮助

我的实施:

public class MyStringSet implements StringSet {
  String[] myArray = new String [] {};
  List<String> myList = Arrays.asList(myArray);
  
  //default constructor
  public MyStringSet(){
   resize(5);
    }

  // precondition: larger is larger than current Set size
  // postcondition: enlarges Set
  public void resize(int larger) {
      myArray = Arrays.copyOf(myArray, myArray.length + larger);
    }
  
  // postcondition: entry is inserted in Set if identical String
  // not already present; if identical entry exists, takes no
  // action.  Calls resize if necessary
  public void insert(String entry) {
    
    Set<String> myArray = new HashSet<String>();
    Collections.addAll(myArray, entry);
    
    
    }

  // postcondition:  removes target value from Set if target is
  // present; takes no action otherwise
  public void remove(String target) {
    if(target != null){
        int n = 0;
        int index = n;
        for(int i = index; i < myArray.length - 1; i++) {
        myArray[i] = myArray[i+1];
    }
}
}
    
  // precondition: Set is not empty
  // postcondition: A random String is retrieved and removed from
  // the Set
  public String getRandomItem () {
      String s = "String is Empty";
    if (myArray != null) {
        int rnd = new Random().nextInt(myArray.length);
        return myArray[rnd];
    }
    
    else {
        return s ;
    }
    }
  
  // precondition: Set is not empty
  // postcondition: the first item in the Set is retrieved and
  // removed from the Set
  public String getFirstItem () { 
    String firstItem = myList.get(0);
    return firstItem;
    }
  
  // postcondition: returns true if target is present, false
  // if not
  public boolean contains(String target) {
      if (target == null) {
        return false;
        
        }
      else {
        return true;
        }
    }
  
  // postcondition: returns true if Set is empty, false if not
  public boolean is_empty( ) {
    if(myArray == null){
        return true;
    
    }
    else {
        return false;
    }
    }
  
   // postcondition: returns total number of Strings currently in set
  public int inventory() {
      int total = myList.size();
      return total;
    }
  
  // postcondition: returns total size of Set (used & unused portions)
  public int getCapacity( ) {
    int capacity = myArray.length;
    return capacity;
    }
}
公共类MyStringSet实现StringSet{
字符串[]myArray=新字符串[]{};
List myList=Arrays.asList(myArray);
//默认构造函数
公共MyStringSet(){
调整大小(5);
}
//前提条件:大于大于当前设置大小
//后置条件:放大设置
公共空间大小调整(整数较大){
myArray=Arrays.copyOf(myArray,myArray.length+更大);
}
//后置条件:如果字符串相同,则在集合中插入条目
//尚未存在;如果存在相同的条目,则不接受
//如有必要,调用调整大小
公共作废插入(字符串条目){
Set myArray=newhashset();
Collections.addAll(myArray,entry);
}
//后置条件:如果目标为,则从集合中删除目标值
//出席;否则不采取行动
公共无效删除(字符串目标){
如果(目标!=null){
int n=0;
int指数=n;
for(int i=索引;i
测试仪等级:

public class SetTester
{
    public static void main(String [] args) {
        StringSet words = new MyStringSet();
        Scanner file = null;
        FileInputStream fs = null;
        String input;
        Scanner kb = new Scanner(System.in);
        int wordCt = 0;
        
        boolean ok = false;
        
        while (!ok)
        {
            System.out.print("Enter name of input file: ");
            input = kb.nextLine();
            try
            {
                fs = new FileInputStream(input);
                ok = true;
            }
            catch (FileNotFoundException e)
            {
                System.out.println(input + " is not a valid file.  Try again.");
            }
        }
        
        file = new Scanner(fs);
        while (file.hasNext())
        {
            input = file.next();
            words.insert(input);
            System.out.println("Current capacity: " + words.getCapacity());  
            wordCt++;
        }
        
        System.out.println("There were " + wordCt + " words in the file");
        System.out.println("There are " + words.inventory() + " elements in the set");
        System.out.println("Enter a value to remove from the set: ");
        input = kb.nextLine();
        while (!words.contains(input))
        {
            System.out.println(input + " is not in the set");
            System.out.println("Enter a value to remove from the set: ");
            input = kb.nextLine();
        }
        
        words.remove(input);
        System.out.println("There are now " + words.inventory() + " elements in the set");
        System.out.println("The first 10 words in the set are: ");
        for (int x=0; x<10; x++)
            System.out.println(words.getFirstItem());
        System.out.println("There are now " + words.inventory() + " elements in the set");
        System.out.println("5 random words from the set are: ");
        for (int x=0; x<5; x++)
            System.out.println(words.getRandomItem());
        System.out.println("There are now " + words.inventory() + " elements in the set");
    }
}
公共类setter
{
公共静态void main(字符串[]args){
StringSet words=new MyStringSet();
扫描文件=空;
FileInputStream fs=null;
字符串输入;
扫描仪kb=新扫描仪(System.in);
int-wordCt=0;
布尔ok=false;
而(!ok)
{
System.out.print(“输入输入文件名:”);
input=kb.nextLine();
尝试
{
fs=新文件输入流(输入);
ok=正确;
}
catch(filenotfounde异常)
{
System.out.println(输入+“不是有效文件。请重试。”);
}
}
文件=新扫描仪(fs);
while(file.hasNext())
{
input=file.next();
插入(输入);
System.out.println(“当前容量:+words.getCapacity());
wordCt++;
}
System.out.println(“文件中有“+wordCt+”个单词”);
System.out.println(“集合中有“+words.inventory()+”元素”);
System.out.println(“输入要从集合中删除的值:”);
input=kb.nextLine();
而(!words.contains(输入))
{
System.out.println(输入+“不在集合中”);
System.out.println(“输入要从集合中删除的值:”);
input=kb.nextLine();
}
删除(输入);
System.out.println(“集合中现在有“+words.inventory()+”个元素”);
System.out.println(“集合中的前10个单词是:”);
对于(int x=0;x
主要问题是我不知道如何将调用的main方法中的数据放入数组中

按照这个简单的例子,您已经正确地做到了这一点

StringSet words = new MyStringSet();
words.insert("something");
但是,insert方法的内容似乎不正确1)您从不检查相同的值2)从不调整集合的大小3)
Collections.addAll
很可能不符合您的要求

因此,考虑到这些因素,并保留一个数组,您应该在其上循环,并检查下一个非空的、不相同的值将出现在哪里
// postcondition: entry is inserted in Set if identical String
// not already present; if identical entry exists, takes no
// action.  Calls resize if necessary
public void insert(String entry) {
    int i = 0;
    while (i < myArray.length && myArray[i] != null) {
       if (myArray[i].equals(entry)) return; // end function because found matching entry
       i++;
    }
    if (i >= myArray.length) {
        // TODO: resize()

        insert(entry); // retry inserting same entry into larger array
    }
    // updates the next non-null array position
    myArray[i] = entry;
}