Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 尝试将文件写入数组-获取NoTouchElementException_Java_Arrays_Nosuchelementexception - Fatal编程技术网

Java 尝试将文件写入数组-获取NoTouchElementException

Java 尝试将文件写入数组-获取NoTouchElementException,java,arrays,nosuchelementexception,Java,Arrays,Nosuchelementexception,(为此,我需要使用数组,而不是数组列表。剪切代码以使其更易于理解。重命名变量和常量以添加标签,以便稍后阅读本文的其他人更好地理解我的做法。) 在类级别,我的数组定义如下: private static final int CONSTANT = 20; public static String[] arrayName = new String[CONSTANT - 1]; readArrays("filenName.txt", arrayName); 在主要方法中,我指的是这样的方法: pri

(为此,我需要使用数组,而不是数组列表。剪切代码以使其更易于理解。重命名变量和常量以添加标签,以便稍后阅读本文的其他人更好地理解我的做法。)

在类级别,我的数组定义如下:

private static final int CONSTANT = 20;
public static String[] arrayName = new String[CONSTANT - 1];
readArrays("filenName.txt", arrayName);
在主要方法中,我指的是这样的方法:

private static final int CONSTANT = 20;
public static String[] arrayName = new String[CONSTANT - 1];
readArrays("filenName.txt", arrayName);
这样的一条线指向以下完整的方法,即错误发生的地方:

    public static void readArrays(String codeFile, 
                                 String[] Arrays)
 {        

   try ( Scanner fin = new Scanner ( new File(codeFile) ); ) 
    {
     for (int i = 0; i < CONSTANT - 1; i++) 
          {
          Arrays[i] = fin.next();
          }
    }
    catch (FileNotFoundException e) 
    {
        System.err.println( "Read Error: " + e.getMessage() );
    }
是什么导致了这个错误?最终,我的目标是使用相同的方法将多个文本文件的全部内容写入多个数组,而不使用任何其他方法

try(Scanner-fin=新扫描仪(新文件(代码文件));)
    try ( Scanner fin = new Scanner ( new File(codeFile) ); ) 
    {
     for (int i = 0; i < CONSTANT - 1 && fin.hasNext(); i++) 
          {
          Arrays[i] = fin.next();
          }
    }
    catch (FileNotFoundException e) 
    {
        System.err.println( "Read Error: " + e.getMessage() );
    }
{ 对于(int i=0;i
您还需要检查扫描仪是否有下一步

Java8中,您可以使用流来读取文件:

List<String> contents = Files.lines(Paths.get("filenName.txt")).collect(Collectors.toList());
List contents=Files.line(path.get(“filenName.txt”)).collect(Collectors.toList());
try(Scanner-fin=新扫描仪(新文件(代码文件));)
{
对于(int i=0;i
您还需要检查扫描仪是否有下一步

Java8中,您可以使用流来读取文件:

List<String> contents = Files.lines(Paths.get("filenName.txt")).collect(Collectors.toList());
List contents=Files.line(path.get(“filenName.txt”)).collect(Collectors.toList());

下面的代码可能会对您有所帮助。 有两件事,在调用next()之前,您必须检查scanner中是否存在下一个元素,另一个元素是常量“20”,如果大小超过20,它将导致数组索引边界异常

将它们添加到列表中并将其转换为数组将解决此问题。请尝试下面的代码

    List<String> list = new ArrayList<String>(); 
    while (fin.hasNext()) {
        list.add(fin.next());
    }

    String[] array = new String[list.size()];
    list.toArray(array);

    for (String str: array) {
        System.out.println(str);
    }
List List=new ArrayList();
while(fin.hasNext()){
list.add(fin.next());
}
String[]数组=新字符串[list.size()];
列表。toArray(数组);
for(字符串str:array){
系统输出打印项次(str);
}

下面的代码可能会对您有所帮助。 有两件事,在调用next()之前,您必须检查scanner中是否存在下一个元素,另一个元素是常量“20”,如果大小超过20,它将导致数组索引边界异常

将它们添加到列表中并将其转换为数组将解决此问题。请尝试下面的代码

    List<String> list = new ArrayList<String>(); 
    while (fin.hasNext()) {
        list.add(fin.next());
    }

    String[] array = new String[list.size()];
    list.toArray(array);

    for (String str: array) {
        System.out.println(str);
    }
List List=new ArrayList();
while(fin.hasNext()){
list.add(fin.next());
}
String[]数组=新字符串[list.size()];
列表。toArray(数组);
for(字符串str:array){
系统输出打印项次(str);
}

谢谢!这解决了错误,但现在我遇到了一个新问题:使用:System.out.println(java.util.Arrays.toString(Arrays));结果为20个空。是否检查文件路径是否正确?可能找不到该文件。您使用的是相对路径还是绝对路径?我将文本文件复制到项目目录中(我完全在单独的项目中创建了该目录)。我确信这些名字拼写正确。它们是否需要在Netbeans内的项目列表中可见?编辑:从头开始,原来我以前用这个新程序做了一些事情,用空格重写了文件。把它们复制回来之后,一切都正常了。谢谢!这解决了错误,但现在我遇到了一个新问题:使用:System.out.println(java.util.Arrays.toString(Arrays));结果为20个空。是否检查文件路径是否正确?可能找不到该文件。您使用的是相对路径还是绝对路径?我将文本文件复制到项目目录中(我完全在单独的项目中创建了该目录)。我确信这些名字拼写正确。它们是否需要在Netbeans内的项目列表中可见?编辑:从头开始,原来我以前用这个新程序做了一些事情,用空格重写了文件。把它们复制回来之后,一切都恢复正常了。