Jessy
Jessy

Reputation: 15661

Java array problems

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

}

Upvotes: 0

Views: 1833

Answers (5)

Richard Walton
Richard Walton

Reputation: 4785

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 }
     }
}

Upvotes: 1

Marty Lamb
Marty Lamb

Reputation: 1988

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

Upvotes: 0

zpesk
zpesk

Reputation: 4353

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.

Upvotes: 0

Pete
Pete

Reputation: 18075

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.

Upvotes: 0

David Z
David Z

Reputation: 131550

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.

Upvotes: 0

Related Questions