Question: Can you please have this work in eclipse I've asked about 5 times through chegg and keep getting errors. please help Create a JavaFX GUI
Can you please have this work in eclipse I've asked about 5 times through chegg and keep getting errors. please help
Create a JavaFX GUI program that allows a user to input the name of a state and display the state capital and population for that state.
When the program starts, it should first
READ and load the data Open the data file (download attached file) and read the data. Create a StateCapital object for each capital and add it to a Map. Close the data file.
You will need to create a class called StateCapital consisting of the following data - city - state - population
Include a constructor, accessor and mutator methods for each data members
Override the toString method
StateCapitals.txt
I keep getting these errors with this code please help
or provide new code
java.io.FileNotFoundException: StateCapitals.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.
at java.util.Scanner.
at application.Main.loadStateCapitals(Main.java:60)
at application.Main.start(Main.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:185)
at java.lang.Thread.run(Unknown Source)
package application; import java.io.File; import java.io.FileNotFoundException; import java.util.HashMap; import java.util.Map; import java.util.Scanner;
import javafx.application.Application; 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.stage.Stage;
public class Main extends Application { private Map
@Override public void start(Stage primaryStage) { loadStateCapitals();
Label nameLabel = new Label("Enter state name:"); TextField nameField = new TextField(); Button submitButton = new Button("Submit"); Label resultLabel = new Label();
GridPane gridPane = new GridPane(); gridPane.setPadding(new Insets(10, 10, 10, 10)); gridPane.setVgap(5); gridPane.setHgap(5); gridPane.setAlignment(Pos.CENTER); gridPane.add(nameLabel, 0, 0); gridPane.add(nameField, 1, 0); gridPane.add(submitButton, 1, 1); gridPane.add(resultLabel, 0, 2, 2, 1);
submitButton.setOnAction(e -> { String name = nameField.getText().trim().toLowerCase();
if (stateCapitals.containsKey(name)) { StateCapital stateCapital = stateCapitals.get(name); resultLabel.setText(stateCapital.toString()); } else { resultLabel.setText("State not found."); } });
Scene scene = new Scene(gridPane, 400, 200); primaryStage.setTitle("State Capitals"); primaryStage.setScene(scene); primaryStage.show(); }
private void loadStateCapitals() { try { File file = new File("StateCapitals.txt"); Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) { String line = scanner.nextLine(); String[] tokens = line.split(",");
if (tokens.length == 3) { String city = tokens[0].trim(); String state = tokens[1].trim().toLowerCase(); int population = Integer.parseInt(tokens[2].trim());
StateCapital stateCapital = new StateCapital(city, state, population); stateCapitals.put(state, stateCapital); } }
scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
public static void main(String[] args) { launch(args); } }
package application;
public class StateCapital {
private String city;
private String state;
private int population;
public StateCapital(String city, String state, int population) {
this.city = city;
this.state = state;
this.population = population;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
@Override
public String toString() {
return city + ", " + state + " - Population: " + population;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
