English 中文(简体)
瓦莱改为按行各行各行各案的档案。
原标题:Vala read file line by line
int n = sc.nextInt();
char[][] original = new char[n][n];
char [][] result = new char[n][n];
String line;

for (int i = 0; i < n; i++) {
  line = sc.nextLine();
  System.out.println(line);
  for (int u = 0; u < n; u++) {
    original[i][u] = line.charAt(u);
  }
}

for (int i = 0; i < n; i++) {
  line = sc.nextLine();
  for (int u = 0; u < n; u++) {
    result[i][u] = line.charAt(u);
  }
}

我有这样的档案:

2
ha
ah
lo
ol

我对背信弃义的N型号,然后是两个N*N型号。

我正试图把它们读到两个阵列中,但我发现这一错误:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:695) at transform.main(transform.java:25)

第25条是:

original[i][u] = line.charAt(u);

I just don t get it, I think I m doing everything just fine. Any ideas? Thank you!

最佳回答

我假定您的<代码>sc是一个扫描器? <代码>nextInt()在您的档案中读到下一个编号,但not<>> /em>处理以下新线。 因此,你第一次打电话到nextLine()将只剩下空洞(即所有特性,直到尚未处理的新线为止)。 您将看到,打电话确实是空线。 可在你准备确定这种行为之前插入newLine()。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...