reading and writing file in javafx program










0















I'm writing a program writes and read the file. it has 50 numbers and if users enter number more than 50 it should ask the users to enter it again. my question is for the error it should show the message box but again return to the text field for input. Also one more question, for example, user1 is entering 1 and user2 is also entering 1, it should tell the user2 "please enter another number it's already entered." how can I do it in writing and reading files.
this is my code:



public class JavaFXApplication24 extends Application 
private int seatInput;
@Override
public void start(Stage primaryStage)
StackPane pane = new StackPane();

HBox hbox = new HBox();
Label num = new Label("Please enter the number from 1 to 50: ");

TextField numInput = new TextField();

File file = new File("NumberBook.txt");
Button ok = new Button("ok");
ok.setOnAction((new EventHandler<ActionEvent>()
public void handle(final ActionEvent event ) NumberFormatException e)


try
PrintWriter output = new PrintWriter(file);

catch (IOException ex)
System.out.printf("error", ex);

try
Scanner input1 = new Scanner(file);

System.out.printf("Entered: %s", seatInput);

catch (FileNotFoundException ex)
System.out.printf("error", ex);




));
hbox.getChildren().addAll(num,numInput,ok);



pane.getChildren().addAll(hbox);

Scene secondScene = new Scene(pane, 600, 400);

// New window (Stage)
Stage newWindow = new Stage();
newWindow.setTitle("Booking and Timing");
newWindow.setScene(secondScene);
newWindow.show();













share|improve this question






















  • This is probably not the main issue, but: Neither numInput.getText().equals(1) nor numInput.getText().equals(50) yield true regardless of the input in the TextField. Comparing String to Integer using equals always yields false.

    – fabian
    Nov 12 '18 at 19:21















0















I'm writing a program writes and read the file. it has 50 numbers and if users enter number more than 50 it should ask the users to enter it again. my question is for the error it should show the message box but again return to the text field for input. Also one more question, for example, user1 is entering 1 and user2 is also entering 1, it should tell the user2 "please enter another number it's already entered." how can I do it in writing and reading files.
this is my code:



public class JavaFXApplication24 extends Application 
private int seatInput;
@Override
public void start(Stage primaryStage)
StackPane pane = new StackPane();

HBox hbox = new HBox();
Label num = new Label("Please enter the number from 1 to 50: ");

TextField numInput = new TextField();

File file = new File("NumberBook.txt");
Button ok = new Button("ok");
ok.setOnAction((new EventHandler<ActionEvent>()
public void handle(final ActionEvent event ) NumberFormatException e)


try
PrintWriter output = new PrintWriter(file);

catch (IOException ex)
System.out.printf("error", ex);

try
Scanner input1 = new Scanner(file);

System.out.printf("Entered: %s", seatInput);

catch (FileNotFoundException ex)
System.out.printf("error", ex);




));
hbox.getChildren().addAll(num,numInput,ok);



pane.getChildren().addAll(hbox);

Scene secondScene = new Scene(pane, 600, 400);

// New window (Stage)
Stage newWindow = new Stage();
newWindow.setTitle("Booking and Timing");
newWindow.setScene(secondScene);
newWindow.show();













share|improve this question






















  • This is probably not the main issue, but: Neither numInput.getText().equals(1) nor numInput.getText().equals(50) yield true regardless of the input in the TextField. Comparing String to Integer using equals always yields false.

    – fabian
    Nov 12 '18 at 19:21













0












0








0








I'm writing a program writes and read the file. it has 50 numbers and if users enter number more than 50 it should ask the users to enter it again. my question is for the error it should show the message box but again return to the text field for input. Also one more question, for example, user1 is entering 1 and user2 is also entering 1, it should tell the user2 "please enter another number it's already entered." how can I do it in writing and reading files.
this is my code:



public class JavaFXApplication24 extends Application 
private int seatInput;
@Override
public void start(Stage primaryStage)
StackPane pane = new StackPane();

HBox hbox = new HBox();
Label num = new Label("Please enter the number from 1 to 50: ");

TextField numInput = new TextField();

File file = new File("NumberBook.txt");
Button ok = new Button("ok");
ok.setOnAction((new EventHandler<ActionEvent>()
public void handle(final ActionEvent event ) NumberFormatException e)


try
PrintWriter output = new PrintWriter(file);

catch (IOException ex)
System.out.printf("error", ex);

try
Scanner input1 = new Scanner(file);

System.out.printf("Entered: %s", seatInput);

catch (FileNotFoundException ex)
System.out.printf("error", ex);




));
hbox.getChildren().addAll(num,numInput,ok);



pane.getChildren().addAll(hbox);

Scene secondScene = new Scene(pane, 600, 400);

// New window (Stage)
Stage newWindow = new Stage();
newWindow.setTitle("Booking and Timing");
newWindow.setScene(secondScene);
newWindow.show();













share|improve this question














I'm writing a program writes and read the file. it has 50 numbers and if users enter number more than 50 it should ask the users to enter it again. my question is for the error it should show the message box but again return to the text field for input. Also one more question, for example, user1 is entering 1 and user2 is also entering 1, it should tell the user2 "please enter another number it's already entered." how can I do it in writing and reading files.
this is my code:



public class JavaFXApplication24 extends Application 
private int seatInput;
@Override
public void start(Stage primaryStage)
StackPane pane = new StackPane();

HBox hbox = new HBox();
Label num = new Label("Please enter the number from 1 to 50: ");

TextField numInput = new TextField();

File file = new File("NumberBook.txt");
Button ok = new Button("ok");
ok.setOnAction((new EventHandler<ActionEvent>()
public void handle(final ActionEvent event ) NumberFormatException e)


try
PrintWriter output = new PrintWriter(file);

catch (IOException ex)
System.out.printf("error", ex);

try
Scanner input1 = new Scanner(file);

System.out.printf("Entered: %s", seatInput);

catch (FileNotFoundException ex)
System.out.printf("error", ex);




));
hbox.getChildren().addAll(num,numInput,ok);



pane.getChildren().addAll(hbox);

Scene secondScene = new Scene(pane, 600, 400);

// New window (Stage)
Stage newWindow = new Stage();
newWindow.setTitle("Booking and Timing");
newWindow.setScene(secondScene);
newWindow.show();










javafx






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 19:02









MjahMjah

65




65












  • This is probably not the main issue, but: Neither numInput.getText().equals(1) nor numInput.getText().equals(50) yield true regardless of the input in the TextField. Comparing String to Integer using equals always yields false.

    – fabian
    Nov 12 '18 at 19:21

















  • This is probably not the main issue, but: Neither numInput.getText().equals(1) nor numInput.getText().equals(50) yield true regardless of the input in the TextField. Comparing String to Integer using equals always yields false.

    – fabian
    Nov 12 '18 at 19:21
















This is probably not the main issue, but: Neither numInput.getText().equals(1) nor numInput.getText().equals(50) yield true regardless of the input in the TextField. Comparing String to Integer using equals always yields false.

– fabian
Nov 12 '18 at 19:21





This is probably not the main issue, but: Neither numInput.getText().equals(1) nor numInput.getText().equals(50) yield true regardless of the input in the TextField. Comparing String to Integer using equals always yields false.

– fabian
Nov 12 '18 at 19:21












1 Answer
1






active

oldest

votes


















0














From a quick glance here are some problems with your code:



1) if ((numInput.getText().equals(1)&& numInput.getText().equals(50))). There are 2 problems here. Firstly, as fabian pointed out, this statement will always evaluate to false. Secondly, from your question I think you want the user to enter a number from 1 to 50, but here you're checking if they enter 1 and 50 (which also means it's always false).



2) JOptionPane.showInputDialog("Please enter again") Because your application is JavaFX, you should use the built in Dialog instead.



Now to answer your questions, you can call requestFocus on the text field right after the error message is displayed. Your second question seems like you want some networking functionality in the application, which adds a whole lot of complexity to it. You might need to do some research on how to do it in Java.






share|improve this answer

























  • figured out the dialog but still not able if statement

    – Mjah
    Nov 12 '18 at 22:55












  • if ( (numInput.getText().length() > 50) && (numInput.getText().length() < 1)) {

    – Mjah
    Nov 12 '18 at 22:55











  • please help with the if statement

    – Mjah
    Nov 12 '18 at 22:56











  • Convert the text input to int with something like Integer.parseInt then compare to 1 and 50.

    – Gnas
    Nov 13 '18 at 9:08












  • if ((Integer.parseInt(numInput.getText()) > 50) || ((Integer.parseInt(numInput.getText()) < 1)))

    – Mjah
    Nov 14 '18 at 0:15










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268501%2freading-and-writing-file-in-javafx-program%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














From a quick glance here are some problems with your code:



1) if ((numInput.getText().equals(1)&& numInput.getText().equals(50))). There are 2 problems here. Firstly, as fabian pointed out, this statement will always evaluate to false. Secondly, from your question I think you want the user to enter a number from 1 to 50, but here you're checking if they enter 1 and 50 (which also means it's always false).



2) JOptionPane.showInputDialog("Please enter again") Because your application is JavaFX, you should use the built in Dialog instead.



Now to answer your questions, you can call requestFocus on the text field right after the error message is displayed. Your second question seems like you want some networking functionality in the application, which adds a whole lot of complexity to it. You might need to do some research on how to do it in Java.






share|improve this answer

























  • figured out the dialog but still not able if statement

    – Mjah
    Nov 12 '18 at 22:55












  • if ( (numInput.getText().length() > 50) && (numInput.getText().length() < 1)) {

    – Mjah
    Nov 12 '18 at 22:55











  • please help with the if statement

    – Mjah
    Nov 12 '18 at 22:56











  • Convert the text input to int with something like Integer.parseInt then compare to 1 and 50.

    – Gnas
    Nov 13 '18 at 9:08












  • if ((Integer.parseInt(numInput.getText()) > 50) || ((Integer.parseInt(numInput.getText()) < 1)))

    – Mjah
    Nov 14 '18 at 0:15















0














From a quick glance here are some problems with your code:



1) if ((numInput.getText().equals(1)&& numInput.getText().equals(50))). There are 2 problems here. Firstly, as fabian pointed out, this statement will always evaluate to false. Secondly, from your question I think you want the user to enter a number from 1 to 50, but here you're checking if they enter 1 and 50 (which also means it's always false).



2) JOptionPane.showInputDialog("Please enter again") Because your application is JavaFX, you should use the built in Dialog instead.



Now to answer your questions, you can call requestFocus on the text field right after the error message is displayed. Your second question seems like you want some networking functionality in the application, which adds a whole lot of complexity to it. You might need to do some research on how to do it in Java.






share|improve this answer

























  • figured out the dialog but still not able if statement

    – Mjah
    Nov 12 '18 at 22:55












  • if ( (numInput.getText().length() > 50) && (numInput.getText().length() < 1)) {

    – Mjah
    Nov 12 '18 at 22:55











  • please help with the if statement

    – Mjah
    Nov 12 '18 at 22:56











  • Convert the text input to int with something like Integer.parseInt then compare to 1 and 50.

    – Gnas
    Nov 13 '18 at 9:08












  • if ((Integer.parseInt(numInput.getText()) > 50) || ((Integer.parseInt(numInput.getText()) < 1)))

    – Mjah
    Nov 14 '18 at 0:15













0












0








0







From a quick glance here are some problems with your code:



1) if ((numInput.getText().equals(1)&& numInput.getText().equals(50))). There are 2 problems here. Firstly, as fabian pointed out, this statement will always evaluate to false. Secondly, from your question I think you want the user to enter a number from 1 to 50, but here you're checking if they enter 1 and 50 (which also means it's always false).



2) JOptionPane.showInputDialog("Please enter again") Because your application is JavaFX, you should use the built in Dialog instead.



Now to answer your questions, you can call requestFocus on the text field right after the error message is displayed. Your second question seems like you want some networking functionality in the application, which adds a whole lot of complexity to it. You might need to do some research on how to do it in Java.






share|improve this answer















From a quick glance here are some problems with your code:



1) if ((numInput.getText().equals(1)&& numInput.getText().equals(50))). There are 2 problems here. Firstly, as fabian pointed out, this statement will always evaluate to false. Secondly, from your question I think you want the user to enter a number from 1 to 50, but here you're checking if they enter 1 and 50 (which also means it's always false).



2) JOptionPane.showInputDialog("Please enter again") Because your application is JavaFX, you should use the built in Dialog instead.



Now to answer your questions, you can call requestFocus on the text field right after the error message is displayed. Your second question seems like you want some networking functionality in the application, which adds a whole lot of complexity to it. You might need to do some research on how to do it in Java.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 9:01

























answered Nov 12 '18 at 21:12









GnasGnas

50329




50329












  • figured out the dialog but still not able if statement

    – Mjah
    Nov 12 '18 at 22:55












  • if ( (numInput.getText().length() > 50) && (numInput.getText().length() < 1)) {

    – Mjah
    Nov 12 '18 at 22:55











  • please help with the if statement

    – Mjah
    Nov 12 '18 at 22:56











  • Convert the text input to int with something like Integer.parseInt then compare to 1 and 50.

    – Gnas
    Nov 13 '18 at 9:08












  • if ((Integer.parseInt(numInput.getText()) > 50) || ((Integer.parseInt(numInput.getText()) < 1)))

    – Mjah
    Nov 14 '18 at 0:15

















  • figured out the dialog but still not able if statement

    – Mjah
    Nov 12 '18 at 22:55












  • if ( (numInput.getText().length() > 50) && (numInput.getText().length() < 1)) {

    – Mjah
    Nov 12 '18 at 22:55











  • please help with the if statement

    – Mjah
    Nov 12 '18 at 22:56











  • Convert the text input to int with something like Integer.parseInt then compare to 1 and 50.

    – Gnas
    Nov 13 '18 at 9:08












  • if ((Integer.parseInt(numInput.getText()) > 50) || ((Integer.parseInt(numInput.getText()) < 1)))

    – Mjah
    Nov 14 '18 at 0:15
















figured out the dialog but still not able if statement

– Mjah
Nov 12 '18 at 22:55






figured out the dialog but still not able if statement

– Mjah
Nov 12 '18 at 22:55














if ( (numInput.getText().length() > 50) && (numInput.getText().length() < 1)) {

– Mjah
Nov 12 '18 at 22:55





if ( (numInput.getText().length() > 50) && (numInput.getText().length() < 1)) {

– Mjah
Nov 12 '18 at 22:55













please help with the if statement

– Mjah
Nov 12 '18 at 22:56





please help with the if statement

– Mjah
Nov 12 '18 at 22:56













Convert the text input to int with something like Integer.parseInt then compare to 1 and 50.

– Gnas
Nov 13 '18 at 9:08






Convert the text input to int with something like Integer.parseInt then compare to 1 and 50.

– Gnas
Nov 13 '18 at 9:08














if ((Integer.parseInt(numInput.getText()) > 50) || ((Integer.parseInt(numInput.getText()) < 1)))

– Mjah
Nov 14 '18 at 0:15





if ((Integer.parseInt(numInput.getText()) > 50) || ((Integer.parseInt(numInput.getText()) < 1)))

– Mjah
Nov 14 '18 at 0:15

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268501%2freading-and-writing-file-in-javafx-program%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo