Nazdar! Vedel by mi niekto pomôcť? Potrebujem, aby mi z txt súboru načítalo do poľa hodnoty. Vypisuje mi iba samé nuly.
Tu je zdrojový kód:
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ReadArrays
{
private Scanner input;
/**Enable user to open file*/
public void openFile()
{
try
{
input = new Scanner(new File("D:/target2.txt"));
} /*END TRY*/
catch (FileNotFoundException fileNotFoundException)
{
System.err.println("Error opening file!");
System.exit(1);
} /*END CATCH*/
} /*END METHOD OPENFILE*/
/**read record from file*/
public void readRecords()
{
/**object to be written to screen*/
System.out.printf("%-12s\n", "Target");
try //read records from file using Scanner object
{
int[] pole = new int[100];
int i = 0;
while(input.hasNext() && i<100)
{
/**display record contents*/
pole[i++] = input.nextInt();
System.out.printf("%d\n", pole[i]);
} /*END WHILE*/
} /*END TRY*/
catch (NoSuchElementException elementException)
{
System.err.println("Error reading from file");
System.exit(1);
} /*END CATCH*/
} /*END METHOD readRecords*/
/**close file and terminate application*/
public void closeFile()
{
if (input != null)
input.close(); //close file
} /*END METHOD closeFile*/
}