[英]java.util.NoSuchElementException error don't know how to fix
I am trying to read a file that contains a table of integers into a 13-by-17 2D array but I am getting a NoSuchElementException error that I do not know how to fix. 我试图将包含整数表的文件读入一个13×17的2D数组,但是我得到一个NoSuchElementException错误,我不知道如何修复。 For more context on the homework I am doing click the link: https://introcs.cs.princeton.edu/java/assignments/mozart.html 有关我正在做的家庭作业的更多背景,请单击链接: https : //introcs.cs.princeton.edu/java/assignments/mozart.html
96 22 141 41 105 122 11 30 70 121 26 9 112 49 109 14
32 6 128 63 146 46 134 81 117 39 126 56 174 18 116 83
69 95 158 13 153 55 110 24 66 139 15 132 73 58 145 79
40 17 113 85 161 2 159 100 90 176 7 34 67 160 52 170
148 74 163 45 80 97 36 107 25 143 64 125 76 136 1 93
104 157 27 167 154 68 118 91 138 71 150 29 101 162 23 151
152 60 171 53 99 133 21 127 16 155 57 175 43 168 89 172
119 84 114 50 140 86 169 94 120 88 48 166 51 115 72 111
98 142 42 156 75 129 62 123 65 77 19 82 137 38 149 8
3 87 165 61 135 47 147 33 102 4 31 164 144 59 173 78
54 130 10 103 28 37 106 5 35 20 108 92 12 124 44 131
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Waltz {
private static int minuetROWS = 13;
private static int trioROWS = 7;
private static int COLS = 17;
public static void main(String[] args) throws FileNotFoundException {
File file = new File("C:\\Users\\Brandizzy\\Downloads\\minuet.txt"); // Creates file object to read file.
//Scanner sc = new Scanner(file);
Waltz a = new Waltz(); // Creates an instance of the class.
String[][] minuet = a.getMinuet(file); // Calls the method getMinuet takes txt file in "file".
System.out.println(Arrays.toString(minuet));
}
/* This method takes the txt file "minuet.txt" as a parameter and stores the
file information into a 2D array called minuet.*/
public String[][] getMinuet(File file) throws FileNotFoundException{
Scanner sc = new Scanner(file);
String[][] minuet = new String[minuetROWS][COLS];
while(sc.hasNext()){
Scanner sc1 = new Scanner(file);
for(int i = 0; i< minuet.length; i++){
for(int j = 0; j < COLS;j ++){
String[] line = sc1.nextLine().trim().split(" ");
minuet[i][j] = line[j];
}
sc1.close();
}
sc.close();
}
return minuet;
}
}
Hello and good luck with your homework. 你好,祝你的功课好运。 Since it is an assignment, I'll try to give you a hint instead of a full answer. 由于这是一项任务,我会尝试给你一个提示,而不是一个完整的答案。
You'll note from the javadoc that Scanner#nextLine()
is the only thing in your example that can throw a NoSuchElementException
. 你会从javadoc中注意到, Scanner#nextLine()
是你的例子中唯一可以抛出NoSuchElementException
。 You might want to see if you can figure out why your nextLine()
invocation concludes that there is no next line to advance to. 您可能想知道是否可以找出为什么您的nextLine()
调用得出结论,没有下一行要前进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.