[英]Adding array values from a text file and printing them
我的C驱动器中有一个名为new.txt
的文本文件,我正尝试使用以下程序将Java程序将此文件的内容放入多维数组。
package square;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.nio.Buffer;
import java.util.Arrays;
import java.util.Scanner;
public class Square {
public static void main(String[] args) {
String a[][] = new String[4][4];
try {
FileReader fr = new FileReader("C://new.txt");
Scanner scanner=new Scanner(fr);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
a[i][j] = scanner.next();
}
}
System.out.print(Arrays.deepToString(a));
scanner.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在这里,当我尝试运行该程序时,出现以下异常。
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at square.Square.main(Square.java:20)
当我更改a[i][j] = scanner.next();
到a[i][j] = scanner.nextInt();
并将数组类型更改为int
m会引发以下异常。
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at square.Square.main(Square.java:20)
请让我知道我要去哪里哪里以及如何解决。
我的文本文件的内容是。
#####
#####
#####
#####
…..
默认的分隔符是空格。由于您没有任何空格,因此第一个读取的字符串本身是##### \\ n ##### \\ n ##### \\ n #####。还有很多其他问题,例如此问题中的循环错误。您可以尝试以下更改并查看是否适合您,否则请提供更多说明
String a[][] = new String[5][5];
Scanner scanner=new Scanner(fr).useDelimiter("");
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; i++) {
if(scanner.hasNext("\n"))
scanner.next();
a[i][j] = scanner.next();
}
System.out.print(Arrays.deepToString(a));
scanner.close();
这里的问题很简单。 首先查看您的文本文件:
#####
#####
#####
#####
…..
在这里,我们有5个标记(4个'#####'和1个'.....')。 看一下:next()函数上的http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html 。
NoSuchElementException - if no more tokens are available
IllegalStateException - if this scanner is closed
因此,在扫描程序读取了最后一个令牌(即“ .....”)之后,就不再有可用的令牌了。
这是我建议的解决方案:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class Square {
public static void main(String[] args) {
String a[][] = new String[5][5];
try {
FileReader fr = new FileReader("new.txt");
Scanner scanner=new Scanner(fr);
ArrayList<String> l = new ArrayList<String>();
while(scanner.hasNext())
{
l.add(scanner.next());
}
for(int i = 0; i < l.size(); ++i)
{
String toSplit = l.get(i);
String[] splitted = toSplit.split("");
for(int j = 0; j < splitted.length; ++j)
{
a[i][j] = splitted[j];
}
}
scanner.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// test the result
for(int i = 0; i < a.length; ++i)
{
for(int j = 0; j < a[i].length; ++j)
{
System.out.println(a[i][j]);
}
}
}
}
请注意:最后两行为空,因为最后一个令牌具有3个字符。 因此a [4] [3]和a [4] [4]将为空。
前面的注解:
请检查您的String
数组的二维。 目前是
new String[4][4];
并允许存储16个值(4x4)。 循环中使用的代码看起来像您期望更多的值。 因此我将数组增加到
new String[5][5];
现在来回答您的问题:
在外部规避5的固定值for
-loop我以一取代它while
-loop:
int i=0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] separatedChars = line.split("(?!^)");
for (int j = 0; j < 5; j++) {
a[i][j] = separatedChars[j];
}
i++;
}
System.out.print(Arrays.deepToString(a));
scanner.close();
相反,仅使用内下一for
-loop我使用scanner.nextLine()
读取的完整产品线,然后分裂它。 然后将结果用于填充给定的数组。 四行5x#输出
[[#, #, #, #, #], [#, #, #, #, #], [#, #, #, #, #], [#, #, #, #, #], [null, null, null, null, null]]s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.