Why is the \"studentdata.txt\" file not being found and allowing me to type with
ID: 3720174 • Letter: W
Question
Why is the "studentdata.txt" file not being found and allowing me to type within the displayed stage?
Below is the code and images of "studentdata.txt" file within and Output error info.
Thank you for your help.
package project03;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.io.File;
import java.util.*;
import java.io.FileReader;
import java.io.IOException;
public class Project03 extends Application
{
private TextField tfCourse;
private TextField tfLastName;
private TextField tfFirstName;
private Label lblRecordNumber;
private ArrayList fileRecords;
private int currentRecordIndex;
//not sure to keep this?
private static final String filename = "studentdata.txt";
@Override
public void start(Stage primaryStage)
{
int top, right, bottom, left;
String[] fields;
try
{
fileRecords = getRecords();
setFields(0);
}
catch (Exception e)
{
System.out.println("Error! Problem opening file. Error was: " + e);
}
GridPane centerPane = new GridPane();
centerPane.setHgap(5);
centerPane.setVgap(5);
centerPane.setPadding(new Insets(top = 25, right = 10, bottom = 10,
left = 10));
centerPane.add(new Label("Course:"),0,0);
centerPane.add(tfCourse,1,0);
centerPane.add(new Label("Last Name:"),0,1);
centerPane.add(tfLastName,1,1);
centerPane.add(new Label("First Name:"),0,2);
centerPane.add(tfFirstName,1,2);
Button butPrevious = new Button("<-Previous");
butPrevious.setId("PreviousButton");
butPrevious.setOnAction(defineActionEvent("PreviousButton"));
Button butNext = new Button("Next->");
butNext.setId("NextButton");
butNext.setOnAction(defineActionEvent("NextButton"));
centerPane.add(butPrevious,0,4);
centerPane.add(butNext,1,4);
centerPane.add(lblRecordNumber,1,6);
//Set properties of UI Nodes
tfCourse.setEditable(false);
tfLastName.setEditable(false);
tfFirstName.setEditable(false);
GridPane.setHalignment(butPrevious, HPos.LEFT);
GridPane.setHalignment(butNext, HPos.RIGHT);
GridPane.setHalignment(lblRecordNumber, HPos.RIGHT);
Scene mainScene = new Scene(centerPane, 400, 250);
primaryStage.setTitle("Student Data Record");
primaryStage.setScene(mainScene);
primaryStage.show();
}
public Project03()
{
tfCourse = new TextField();
tfLastName = new TextField();
tfFirstName = new TextField();
lblRecordNumber = new Label();
currentRecordIndex = 0;
} //Project03 Constructor
private EventHandler defineActionEvent(final String forNode)
{
EventHandler theHandlerObject = null;
theHandlerObject = new EventHandler() {
@Override
public void handle(ActionEvent event)
{
switch (forNode)
{
case "PreviousButton":
if (currentRecordIndex > 0)
{
currentRecordIndex--;
setFields(currentRecordIndex);
}
else
{
System.out.println("Reached BEGINNING of Records.");
}
break;
case "NextButton":
if (currentRecordIndex < (fileRecords.size() -1))
{
currentRecordIndex++;
setFields(currentRecordIndex);
}
else
{
System.out.println("Reached END of Records.");
}
break;
default:
System.out.println("Error. Undefined object.");
break;
}
}
};
return theHandlerObject;
} //defineActionEventdefineActionEvent() method.
private static ArrayList getRecords() throws Exception
{
ArrayList theRecords = new ArrayList<>();
Scanner inputFile;
inputFile = new Scanner(new File("studentdata.txt"));
while(inputFile.hasNextLine())
{
theRecords.add(inputFile.nextLine());
}
return theRecords;
}
private void setFields(int theIndex)
{
String[] fields = fileRecords.get(theIndex).split(",");
tfCourse.setText(fields[0]);
tfLastName.setText(fields[1]);
tfFirstName.setText(fields[2]);
lblRecordNumber.setText(String.format("Record #%3d",
theIndex));
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Explanation / Answer
Hi,
In function getRecords() where you are reading the file try following code -
Scanner inputFile ? = new Scanner(new File("studentdata.txt"));
And remove the line -
//not sure to keep this?
private static final String filename = "studentdata.txt";
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.