Results 1 to 5 of 5
  1. #1
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default Java array problem

    Hi everyone,
    I make a program which will write a record in text file everytimes the button clicked. e.g. I have four files which I put in array. The first array [0] will display automatically without the user need to click the button. When the user click button, the next drawing will displayed, and the process continue until the last array.

    My problem here is, the number of drawings displayed are based on the number of array elements e.g. 4. But I can only able to write 3 record on the text files and not 4.

    I know the problem was because I do the increament of the index after the first button click. However if I didnt, when the button clicked at the first time, the drawing will still not change because it pointing to the same array index [0].

    How can I write on files based on the length of the array?


    Java Code:
    public class mainClass extend JFrame implements ActionListener {
         ....
         JButton next1 = new JButton ("Answer1");
         JButton next2 = new JButton ("Answer2");
         private drawingPanel draw;
         String[] a = {"image1.txt","image2.txt","image3.txt", "image4.txt"};
         fileNumber = 0;
    
    public mainClass { 
        ......
        draw = new drawingPanel(files,fileNumber);
        panel.add (draw);
        [COLOR="Blue"][B]fileNumber++; [/B][/COLOR]
        next1.actionListener(this);
        next2.actionListener(this);
        }
    
    public void actionPerformed () {
        if (fileNumber == a.length){
           dispose();
           JOptionPane.showMessageDialog(null, "The end");
           System.exit(0); 
           }
        else {
           try {
             fout = new FileOutputStream ("fileRecord.txt",true);
    	  Filename = new File(files.get(fileNumber));		
    	  new PrintStream(fout).println (Filename);			
              ....
              if (e.getSource()==next1) {
                 	new PrintStream(fout).println ("answer1");	
    		draw = new drawingPanel(files,fileNumber); 	
    		panel.removeAll();
    		panel.add(draw);
    		panel.revalidate();
    		[COLOR="Blue"][B]fileNumber++;[/B][/COLOR]	
                    }
              else if (e.getSource()==next2) {
                 	new PrintStream(fout).println ("answer2");	
    		draw = new drawingPanel(files,fileNumber);	
    		panel.removeAll();
    		panel.add(draw);
    		panel.revalidate();
    		fileNumber++;	
                    }
    
               .......
               ......

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if (fileNumber == a.length){ ....}

    fileNumber = 4,
    System.exit(0);

    method modifies image4.txt wont perform

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    On first glance your problem isn't in incrementing in the actionPerformed but the increment in the constructor. Why are you doing that?

  4. #4
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    I know...I really in a dead road now. Really need help ..

    What should I do, so that I can write on text file when it comes to the last record of the array?

  5. #5
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    The first drawing index [0] will automatically display on the JPanel. And the user will click button (answers) based on what they see on the drawing. After clicking the button, the next drawing will display...if I didnt do the increament in the constructor , when the button clicked it wont change the display..still pointing to index[0]..until the next click.

Similar Threads

  1. Array List problem
    By kurenai in forum New To Java
    Replies: 2
    Last Post: 06-25-2008, 07:30 AM
  2. array problem
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-05-2008, 02:25 AM
  3. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 07:08 AM
  4. Problem with array Copy
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:46 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 01:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •