0

I have put few elements in array (e.g. 5 elements) The first element, index[0] of the array will automatically displayed without the user need to click the button.

After the button clicked, it will print the next elements of array and the processes continue until the last element of array. Everytimes the button clicked, a file will be written on txt file.

My problem here was, there are e.g. 5 elements of array (successfully displayed when button clicked), however only four files written on txt file. How to make it five...Helppp... Im in a dead road :-(

public class mainFrame extends JFrame implements ActionListener {
   ........
   private JButton answer1 = new JButton();
   String [] a = {"a","b","c","d","e"}
   in fileNumber = 0; 
   }

public mainFramme (){
  System.out.println(a.get(fileNumber))
  fileNumber++;
  answer1.addActionListener(this); 
  }

 public void actionPerformed(ActionEvent e) {     
    if (e.getSource==answer1) {
       System.out.println(a.get(fileNumber))
       try {
       .....
       fout = new FileOutputStream ("myfile.txt",true);
       Filename = new File(files.get(fileNumber));      
       new PrintStream(fout).println (Filename);
       new PrintStream(fout).println ("Answer 1");
       fileNumber++;      
       }
       ...
   }

}

2
  • When you ask a question about Java, please use a "java" tag to reflect this. Commented Mar 22, 2009 at 23:54
  • Im so sorry, I just dont realised it. Will definitly do it next time. Thanks. Commented Mar 23, 2009 at 0:05

5 Answers 5

1

Your problem lies here:

public mainFramme (){
  System.out.println(a.get(fileNumber))
  fileNumber++
  answer1.addActionListener(this); 
  }

You're incrementing fileNumber before the button is pressed so it equals 1. Arrays are indexed from 0 in Java, meaning to get the first element in the array you use array[0] - Seeing as fileNumber will equal 1, you'll be getting the second element of the array - Thus missing the first.

EDIT DUE TO COMMENT:

Ok, then are you calling the flush() and close() methods on the file output stream? 'flush' ensures that any data in the stream is written out before it is closed. It may help if you post your entire actionPerformed method.

Some of the code you posted in no ideal either (i.e the new PrintStream stuff)

Perhaps this might help:

public void actionPerformed(ActionEvent e) {
   if(e.getSource() == answer1) {
       try {
           PrintWriter output = new PrintWriter(new FileOutputStream(fileName.txt));

           // Write stuff to file using output.printLn();
           output.flush();
           output.close();
        }catch (IOException e) { // exception handling }
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I realised that but, my intention was actually to write a file on txt file whenever a button clicked. No file will be written without button clicked. It was like, the first elements indext[0] displayed so the user will know which button to click.
0

Others have basically answered already, but from your comments I don't think it's clear. You are automatically displaying the first String to the screen, but not to a file. It would be better to move the file operations out of the actionPerformed() method. The way you have it written, the file operations are only called when the button is pressed, which is never the case for the first String.

This might be clearer:

public class mainFrame extends JFrame implements ActionListener {
    ........
    private JButton answer1 = new JButton();
    String [] a = {"a","b","c","d","e"}
    in fileNumber = 0; 
}

public mainFrame (){
    nextString();
    answer1.addActionListener(this); 
}

public void actionPerformed(ActionEvent e) {     
    if (e.getSource==answer1) nextString();
}

private void nextString() {
    try {
        .....
        System.out.println(a.get(fileNumber))
        fout = new FileOutputStream ("myfile.txt",true);
        Filename = new File(files.get(fileNumber));      
        new PrintStream(fout).println (Filename);
        new PrintStream(fout).println ("Answer 1");
        fileNumber++;      
    }
    ...
}

9 Comments

Dear Marty Lamb, thanks for the idea..but I still cant solve my problem. When I clicked the button, file that written on the file actually based on the value of the button (Answer1). If I write the file on the first display without clicking the button, it will not valid for my program.
e.g. the first display will not have answer because no button clicked.
Then I'm confused. If it's not valid to write the file when you display the first string (without pressing the button), then why do you expect five files?
Im sorry to make you confused. Actually, I wanted to write both file and the value of the button (e.g new PrintStream(fout).println ("Answer 1");) in the text file.
and actually I have not only 1 button but 5 five button. The new method (private void nextString() fixed the value of the button to ("Answer 1");
|
0

In the constructor, where you have

System.out.println(a.get(fileNumber))
fileNumber++;

It looks to me like you're printing one string to stdout (i.e. the screen) without writing it to the file. I bet that's why you're missing one of the array elements in the file.

3 Comments

Dear David, It was because, file only written on txt file when the button clicked. mmm..it was like..the first question displayed, and when the user answer it(button being clicked-answer recorded on txt file), the next question will be displayed...
Cont..If I didnt put.. fileNumber++; after the 1st display, when the button clicked, it will pointing to the same e.g. index[0].
If I understand you correctly (which is kind of hard, to be honest), that's exactly what I meant...
0

Your incrementing the filenumber value before you've created your first file (for the 0th element). This is leading to the 4 files, namely for elements in indices 1-4. The 0th file is not created.

2 Comments

I have to make the increament so that when the user click the button it will automatically display the next elements.
Yes, but when you display the first element you're not writing it to file.
0

You are missing a bunch of semicolons - i am not sure if this is causing the problem (shouldnt even run)

Make sure each line that needs semicolons has it.

1 Comment

Dear zPesk, thanks to point that.. but actually my real program works, just the array problem...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.