我正在读取文件并将其复制到数组中。我的文件有五行文字,每句都有一个句子。我得到我的输出“数组大小是5”,但之后没有。如果我添加了阵列的打印线,它会给我5个空值...从文本文件读入数组 - 获取“空值”
有人可以帮助解释我做错了什么吗?谢谢!
public static int buildArray() throws Exception
{
System.out.println("BuildArray is starting ");
java.io.File textFile; // declares a variable of type File
textFile = new java.io.File ("textFile.txt"); //reserves the memory
Scanner input = null;
try
{
input = new Scanner(textFile);
}
catch (Exception ex)
{
System.out.println("Exception in method");
System.exit(0);
}
int arraySize = 0;
while(input.hasNextLine())
{
arraySize = arraySize + 1;
if (input.nextLine() == null)
break;
}
System.out.println("Array size is " + arraySize);
// Move the lines into the array
String[] linesInRAM = new String[arraySize];// reserve the memory
int count = 0;
if (input.hasNextLine())
{
while(count < arraySize)
{
System.out.println("test");
linesInRAM[count] = input.nextLine();
System.out.println(linesInRAM[count]);
count = count + 1;
}
}
你永远不会重置扫描仪,所以它直到在文件的结尾... – MadProgrammer 2014-10-22 02:50:46