因此,对于我的计算机科学课,我们必须创建一个程序,它将从一个名为"compact.txt“的文本文件中读取数据,并将其中的整数存储到一个int[]中。在此之后,我们必须将数字打印到终端,在文本文件中包含0,然后在文本文件中不包含0。代码会编译并运行,但是当它运行时,它不会向终端打印任何内容,并且它会完全冻结所有BlueJ。在冻结之后,我甚至不能从main内部复制代码,而必须从任务管理器强制关闭它。FileInput是我的类用来读入文件的。下面是我的代码:
import chn.util.*;
public class compact
{
public static void main(String [] args)
{
FileInput fI = new FileInput("compact.txt");
int[] ar = new int[100];
String line = fI.readLine();
fI.close();
System.out.println(line);
int count = 0;
int x = 0;
while(x < line.length())
{
if(!line.substring(x, x+1).equals(" "))
{
if(!line.substring(x + 1, x + 2).equals(" ") && !
(line.length() - 1 == x))
{
ar[count] = Integer.parseInt(line.substring(x + 1, x+2));
}
else
{
ar[count] = Integer.parseInt(line.substring(x, x+1));
}
count++;
}
x++;
}
System.out.print("Before: " + ar[0]);
for(int i = 1; i < count; i++)
{
System.out.print(", " + ar[i]);
}
System.out.print("\n");
System.out.println("After: ");
for(int i = 0; i < count; i++)
{
if(ar[i] == 0)
{
System.out.print("");
}
else
{
if(i == count - 1)
{
System.out.print(ar[i]);
}
System.out.print(ar[i] + ", ");
}
}
}
}这是包含在"compact.txt“文件中的内容:
0 6 13 0 0 75 33 0 0 0 4 29 21 0 86 0 32 66 0 0
https://stackoverflow.com/questions/47689032
复制相似问题