Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Help

Options
  • 08-03-2010 8:50pm
    #1
    Registered Users Posts: 10


    Hello,

    I would like to make a Java program where the user types a name into a text box and creates a .txt file with that name, now I have the GUI designed and I have asked some people at work (who tbh arent very experienced in Java) and they tell me that this is impossible, Im just here looking for a second opinion...


Comments

  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Did you ask the cleaners at work?

    How would creating a file in a popular programming language be "impossible"?

    Is this a joke?


  • Registered Users Posts: 10 Goldeye


    I work in an accountancy office, the guy I asked doesnt use Java and hasnt since college, I havent used Java either, but Im just trying it out for a project Im working on for a friend...


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    use netbeans, research "StreamWriter", it will take about 5mins after that,

    Good Luck!


  • Registered Users Posts: 10 Goldeye


    Do you know any good sites to search? When I look on Google, I cant find anything helpful.

    Sorry about this, when it comes to Java, Im way out of practice


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Try the Sun Java Tutorial.

    I'm glad you didn't say you work in a software company or with developers.


  • Advertisement
  • Registered Users Posts: 10 Goldeye


    It would be a bad advertisement for that software company if I was!

    I dont have an issue with creating the files, but I do have an issue with naming it. You see, I want the user to name it themselves. I have tried allowing it equal to (txtName.getText() + ".txt") but this didnt work, anyone have any ideas on how you would do this?


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    Hi,

    Do you have a text field that the user can enter the file name into? If so, you should use the contents of the text field to concatenate a file name for yourself.

    example:

    fileName = jTextField1.toString() + ".txt";


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    As in you can't assemble the String or you can't rename/create the file once you've done so? Or is it that you can't get the input from the user?

    If you post a code snippet and confirm the type of the txtName variable then someone might be able to offer more concrete help.


  • Registered Users Posts: 10 Goldeye


    Okay so, I have the GUI set up (at the moment) with a text box and a button.
    The user must enter a name into a text box, press the button and will produce a text file with the name they entered, for this I am using FileOutpuStream, as follows
    outputFile = new FileOutputStream(txtFileName.toString() + ".txt");

    which creates a file with this name

    java.awt.TextField[textfield0,0,0,0x0,invalid,text=,editable,selection=0-0]

    I think this might be too hard, Ill probably try something else.


  • Closed Accounts Posts: 2,720 ✭✭✭Sid_Justice


    what are you trying to do at the highest level ? Like write a program for storing what kind of information about what?

    Best practice on programming questions is to supply the code you've written using the code tags.
    [PHP]public class Test {

    public static void main(String[] args) {
    System.out.println("Hello World");


    }

    }
    [/PHP]


  • Advertisement
  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Goldeye wrote: »
    ...
    java.awt.TextField[textfield0,0,0,0x0,invalid,text=,editable,selection=0-0]
    ...

    In a nutshell what you're doing is calling toString() on an instance of the TextField class; and all this is doing is returning a description of the instance as opposed to anything useful.

    The first issue is that you should really be using the newer Swing classes; and the second issue is that your conceptual view of how a text field works is slightly skewed.

    What must happen is that an event has to be fired which you actively listen for; and then you do whatever you want with the information the user has entered.

    I suggest having a look through the Swing tutorial.


  • Registered Users Posts: 428 ✭✭Joneser


    I think the best way to do this would probably to declare your file first :
    File myFile = new File("file path");

    And then you can write to it using a fileWriter, the true in the method means that the file will be appended to, otherwise the data in the file will just be replaced with what you are writing to it.

    FileWriter myWriter = new FileWriter(myFile, true);
    myWriter.write(myString);


  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    What the above poster said.

    The logical steps are
    • Create your File using java.io.File
    • Interrogate your user using a jTextField
    • Store the inputted value
    • Use java.io.FileWriter to name and write the file

    This should give you a step in the right direction?


  • Registered Users Posts: 2,781 ✭✭✭amen


    all the above is good but the since the user is struggling once the user has entered the name of the file you need a button maybe Create File and in the click event of the button your add the code to name/create the file


  • Registered Users Posts: 10 Goldeye


    Got this sorted out, thanks for all the help.


Advertisement