Results 1 to 5 of 5
Thread: Java array problem
- 03-23-2009, 02:35 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 38
- Rep Power
- 0
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++; } ....... ......
- 03-23-2009, 03:11 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
if (fileNumber == a.length){ ....}
fileNumber = 4,
System.exit(0);
method modifies image4.txt wont perform
-
On first glance your problem isn't in incrementing in the actionPerformed but the increment in the constructor. Why are you doing that?
- 03-23-2009, 03:18 AM #4
Member
- Join Date
- Mar 2009
- Posts
- 38
- Rep Power
- 0
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?
- 03-23-2009, 03:21 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 38
- Rep Power
- 0
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
-
Array List problem
By kurenai in forum New To JavaReplies: 2Last Post: 06-25-2008, 07:30 AM -
array problem
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-05-2008, 02:25 AM -
array problem
By wats in forum New To JavaReplies: 1Last Post: 12-12-2007, 07:08 AM -
Problem with array Copy
By coco in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:46 AM -
array problem
By Albert in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 01:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks