Results 1 to 16 of 16
Thread: Problem with datainput
- 05-12-2011, 04:17 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
- 05-12-2011, 04:53 PM #2when i restart the program it automatically clears my scores.txt file
What clears the scores? Can your program detect that the file exists and that it has data and NOT clear it?
Also it would help if you posted code for all the classes used in your program.Last edited by Norm; 05-12-2011 at 04:56 PM.
- 05-12-2011, 04:55 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
Well i ran another short program to read the text file and it reads the 4 values, but as soon as i open the program above again the text file gets cleared.
This is part of the specification that is for the part of the program im working on:
Check that the program works correctly and correct any errors found. Before you can test the program you will need to write and test a short program to initialise the values on the file for each of the four choices to zero.
How do i do this?
- 05-12-2011, 04:58 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
This is the class:
Java Code:import Barnfield.Dialogs; class Vote { private String question; private String answer; private int score = 0; public void setQuestion (String question){ this.question = question; } String getQuestion(){ return question; } public void setAnswer (String answer){ this.answer = answer; } String getAnswer(){ return answer; } void sum(int total){ score += 1; } int getScore(){ return score; } }
- 05-12-2011, 05:56 PM #5as soon as i open the program above again the text file gets cleared.
What do you want the program to do if the file exists?
How do you detect if the file exists?
If it does exist, then what should your program do?
BTW What package and where is: Barnfield.Dialogs;
- 05-12-2011, 06:10 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
Ok ill have a go.
Read the number of votes already in the text file, default is 0, for example score 1 = 0, score 2 = 0......
I havent added that part yet, but i know it does.
Right if user selects option 1 then it will display the number of votes contained in the text file. If the user selects option 2 and chooses an answer from 1 - 4, then it should add +1 to the vote in the text file for the selection chosen.
Its just a package we use at our university to use the JOptionPane.
- 05-12-2011, 06:34 PM #7
Most of my previous post was about your program logic. You need to think this thru and decide what to do if the file exists. Currently your code opens an output file to write over the file and destroys it before you read from it. So you need to consider what to do if the file exists BEFORE opening it for output and destroying it.
Without the I/O package you use, no one can compile your code.
- 05-12-2011, 06:40 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
Ok thank you, i really need to read what its actually doing properly. Sorry where abouts in the code does it destroy the file?
Oh i see, do you want me to post it?
- 05-12-2011, 06:46 PM #9where abouts in the code does it destroy the file?
- 05-12-2011, 06:53 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
Im so confused by all this. :(
- 05-12-2011, 07:01 PM #11
Ok, forget about the program code for a sec.
In English, What do you want the program to do if the Scores.txt file exists?
There needs to be some decisions made and maybe some actions taken before going on.
- 05-12-2011, 10:45 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
Right i need it to be read by the program to show the users scores and also record the users scores by adding to the scores that are already contained in the text file, if that makes any sense.
- 05-12-2011, 10:48 PM #13
So how are you going to change your code to do that?
You program currently only reads the file if .....
Perhaps it should always read it and save its data somewhere.
- 05-13-2011, 03:00 PM #14
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
Honestly i have no idea im so new to all this.
Sorry to be a pain, i need a push i the right direction.
When the user votes i need it to add +1 to the total in the text file, the when the program ends i need it to write all of the current votes that have been added and the ones inside the file already, so that when the program is started again it shows all of the votes that have been counted over a number of votes done.
- 05-13-2011, 03:43 PM #15
If you need to save the data in the text file, you need to read it into your program BEFORE you set up to write to the file. Creating/opening the output file deletes the data in the text file.
The File class has a method you can use to detect if a file exists. If the file exists when you start the program, what do you want the program to do before it destroys the data in the file? This needs a bit of logic.
- 05-13-2011, 05:16 PM #16
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
Thank you for all of your help, it was really simple in the end, i was just trying to make a simple equation into a hard one. Basically i changed:
From:
dataOut.writeInt(answer1.getScore());
To:
dataOut.writeInt(a + answer1.getScore());
And it does exactly what i wanted it to.
Youv'e been great thank you.
Bookmarks