Results 1 to 16 of 16
  1. #1
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default Problem with datainput

    Fixed******
    Last edited by chrisLU5; 05-14-2011 at 08:02 PM. Reason: Copying****

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    when i restart the program it automatically clears my scores.txt file
    Sounds like a logic problem.
    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 03:56 PM.

  3. #3
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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?

  4. #4
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    as soon as i open the program above again the text file gets cleared.
    You need to find what part of your program does this. Then you need to figure out how to change the code so that it does NOT clear the file.
    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;

  6. #6
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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.

  8. #8
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    where abouts in the code does it destroy the file?
    Read the API doc for the FileOutputStream class.

  10. #10
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    Im so confused by all this. :(

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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.

  12. #12
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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.

  14. #14
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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.

  16. #16
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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.

Posting Permissions

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