private Scanner inputFile;
private String corpusFileString = "";
try
{
File file = new File(sourceFile.getText());
inputFile = new Scanner(file);
JOptionPane.showMessageDialog(null, "The file was found.");
if (text != null)
{
translate.setEnabled(true);
}
}
catch (FileNotFoundException ex)
{
JOptionPane.showMessageDialog(null, "The file was not found.");
}
try
{
numberWords = inputFile.nextInt();
}
catch (InputMismatchException ex)
{
JOptionPane.showMessageDialog(null, "The first line on the file must be an integer");
}
while (inputFile.hasNext())
{
corpusFileString = corpusFileString + inputFile.nextLine() + " ";
}
So when I read this file, the first line should be an integer(a different variable will hold that) or it will throw an exception.
The rest of the file should be data(another variable for all the data) but for some reason the String contains an empty space at the beginning and when I split it I have to use +1 in my array cause of that empty space.
The issue is that it’s reading the first int, then the rest of the first line.
Basically:
15\n a line here \n another line here
Where \n is a newline.
It reads 15, then it reads to \n, which is “” (omitting the newline character). The rest it reads as you expected.
Try using:
Instead of