tags:

views:

361

answers:

5

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++;      
       }
       ...
   }

}

A: 

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.

David Zaslavsky
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...
Jessy
Cont..If I didnt put.. fileNumber++; after the 1st display, when the button clicked, it will pointing to the same e.g. index[0].
Jessy
If I understand you correctly (which is kind of hard, to be honest), that's exactly what I meant...
David Zaslavsky
Im in a dead road..:-(
Jessy
A: 

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.

Pete
I have to make the increament so that when the user click the button it will automatically display the next elements.
Jessy
Yes, but when you display the first element you're not writing it to file.
Marty Lamb
+1  A: 

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 }
     }
}
Richie_W
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.
Jessy
A: 

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.

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

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++;      
    }
    ...
}
Marty Lamb
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.
Jessy
e.g. the first display will not have answer because no button clicked.
Jessy
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?
Marty Lamb
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.
Jessy
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");
Jessy
OK, it might be helpful if you edit your original question and list out exactly what you want to happen step by step. E.g., "launch program, display "a", user presses button, "a" gets written to file, button text changes to... and so on.
Marty Lamb
1) Create array (four elements..{"a.txt","b.txt","c.txt","d.txt")2) Display first elements (index[0])3) Increament 1 for array index4) User presses button (e.g. button name = 5 boxes)5) File written on text file (i.filename in the array, ii. button value=5 boxes)
Jessy
6) index increasement 7) button presses 8)file written 9) display image...process continue until the end of array
Jessy
at the end...if four elements on array...will have four display and four files written on txt file
Jessy