The code I'm attempting to write has a jTextField for a user to input a string. The user will then click the jButton to create the file. For the creation of the file, I had in mind: Taking the string input from the user, and appending it to .txt within a file creation method. The code below shows this unsuccessful attempt:

String input;
Scanner scan = new Scanner(System.in);
input = scan; //This is totally wrong, but I'm not sure how to convert the scanner

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    final Formatter x;
    try{

      x = new Formatter(input + ".txt");

    } catch(Exception e) {
        System.out.println("Your code is bad. You should feel bad.");
    }
}           

 private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
     input = jTextField1.getText();         
}        
有帮助吗?

解决方案

Nevermind, I found the best solution for my case. Thanks, though!

  final Formatter x;
    try{

        x = new Formatter(jTextField1.getText() + ".txt");

    } catch(Exception e) {
        System.out.println("Something went horribly wrong..");
    }
}                           
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top