Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “从文本文件读取”;“dictionary.txt”;_Java - Fatal编程技术网

Java “从文本文件读取”;“dictionary.txt”;

Java “从文本文件读取”;“dictionary.txt”;,java,Java,下面是我的代码示例。我有一个名为dictionary.txt的文本文件,我正试图从中读取,但在构造函数行中不断出现错误。我不确定如何构建构造函数来读取dictionary.txt文件,以及它如何与name=new文件(“dictionary.txt”)交互 import java.util.Scanner; 导入java.io.*; 导入java.util.*; 公共类词表{ //实例变量 私有字符串[]单词;//接收的单词数组 私有int字数; 私有文件名; 私人信件; //建造师 公共单词列

下面是我的代码示例。我有一个名为dictionary.txt的文本文件,我正试图从中读取,但在构造函数行中不断出现错误。我不确定如何构建构造函数来读取dictionary.txt文件,以及它如何与
name=new文件(“dictionary.txt”)交互

import java.util.Scanner;
导入java.io.*;
导入java.util.*;
公共类词表{
//实例变量
私有字符串[]单词;//接收的单词数组
私有int字数;
私有文件名;
私人信件;
//建造师
公共单词列表(字符串“WHAT GOES HERE?”)引发FileNotFoundException{
//引发异常,因为它获取并扫描文件
字数=0;
名称=新文件(“dictionary.txt”);
hasLetter=null;
扫描仪列表扫描仪=新扫描仪(名称);
while(listScanner.hasNextLine()){
listScanner.nextLine();
字数++;
}
close();
words=新字符串[wordCount];
Scanner secondScanner=新扫描仪(名称);

对于(int i=0;i而不是说它在构造函数中抛出一个file not found异常,而是在类名处说它。如果这不起作用,请尝试使用try-catch语法而不是在类中抛出一个异常

构造函数行中的字符串应该是一个与您试图读取的.txt文件的文件路径相对应的变量

此外,还要对代码进行格式化,以使其更易于阅读。通过格式化,将字符串变量添加到构造函数中,然后添加一个主方法来运行整个过程,完成的类应该如下所示:

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


public class WordLists {

    //instance variables
    private String[] words; //array of words taken in 
    private int wordCount;
    private File name;
    private Boolean hasLetter;
    Scanner listScanner, secondScanner;

    //constructor    
    public WordLists(String path) throws FileNotFoundException{
        //throws exception because it takes and scans a file
        wordCount = 0;
        name = new File(path);
        hasLetter = false;

        listScanner = new Scanner(name);
        secondScanner = new Scanner(name);

        while(listScanner.hasNextLine()){
            listScanner.nextLine();
            wordCount++;
        }

        words = new String [wordCount];

        for(int i = 0; i < wordCount; i++){
            words[i] = secondScanner.nextLine();
        }

        listScanner.close();
        secondScanner.close(); 
    }

    public static void main(String[] args){
        try { 
            WordLists w = new WordLists("dictionary.txt");
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }

        System.out.println("Done");
    }
}
import java.util.Scanner;
导入java.io.*;
导入java.util.*;
公共类词表{
//实例变量
私有字符串[]单词;//接收的单词数组
私有int字数;
私有文件名;
私人信件;
扫描器列表扫描器,二次扫描器;
//建造师
公共单词列表(字符串路径)引发FileNotFoundException{
//引发异常,因为它获取并扫描文件
字数=0;
名称=新文件(路径);
hasLetter=假;
listScanner=新扫描仪(名称);
secondScanner=新扫描仪(名称);
while(listScanner.hasNextLine()){
listScanner.nextLine();
字数++;
}
words=新字符串[wordCount];
for(int i=0;i
您得到的错误消息是什么,它指向的是哪一行。它指向//构造函数注释下的那一行,我得到的是抛出的FileNotFoundException。有一个dictionary.txt文件可读取,但由于某些原因,它不接受数组参数。按照您当前的设置方式,没有任何内容。您可以在那里执行
String filePath
然后执行
name=new File(filePath);
那么它将从哪里读取dictionary.txt文件?如果这听起来很愚蠢,我很抱歉,我是java新手,这是我第一次尝试从txt文件读取。dictionary.txt存储在哪里?
import java.util.Scanner;
import java.io.*;
import java.util.*;


public class WordLists {

    //instance variables
    private String[] words; //array of words taken in 
    private int wordCount;
    private File name;
    private Boolean hasLetter;
    Scanner listScanner, secondScanner;

    //constructor    
    public WordLists(String path) throws FileNotFoundException{
        //throws exception because it takes and scans a file
        wordCount = 0;
        name = new File(path);
        hasLetter = false;

        listScanner = new Scanner(name);
        secondScanner = new Scanner(name);

        while(listScanner.hasNextLine()){
            listScanner.nextLine();
            wordCount++;
        }

        words = new String [wordCount];

        for(int i = 0; i < wordCount; i++){
            words[i] = secondScanner.nextLine();
        }

        listScanner.close();
        secondScanner.close(); 
    }

    public static void main(String[] args){
        try { 
            WordLists w = new WordLists("dictionary.txt");
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }

        System.out.println("Done");
    }
}