Я продолжаю получать эту ошибку для цикла while: java.lang.ArrayIndexOutOfBoundsException; Это значение: прочитайте новые данные в массиве имен из «Input.txt» по одной строке в строке и поместите их в массив. Количество строк в файле должно быть таким же, как количество ячеек в массиве.читать новые данные в массив имен из input.txt
// Open an input textfile named "Input.txt".
File f = new File("Input.txt");
Scanner inputFile = new Scanner(f);
System.out.println("\nThe array contents:");
// Read new data into the array of names from "Input.txt" one String per line, and places them in an array.
//The number of Strings in the file must be the same as the number of cells in the array.
String[] input = new String [5];
int k=0;
while (inputFile.hasNext())
{
String str = inputFile.nextLine();
input[k]=str;
k++;
}
inputFile.close();
//Print the new array contents on the screen
PrintWriter pw = new PrintWriter("Input.txt");
for(String str : array)
pw.println(str);
pw.close();
}
/**
* Method printOnScreen sends the entire contents of an
* array to the Screen using an "enhanced for" loop.
*
* @param array the array of Strings be printed on Screen
*
*/
public static void printOnScreen(String[] args)
{
for(String val : args)
System.out.println(val);
}
Попробуйте поместить строки в «ArrayList», а затем использовать метод «toArray()» в «ArrayList». – LearningDeveloper