user3703367
user3703367

Reputation:

Reading and writing to file not working correctly

I have been working on this code for the day and am almost at the finish line. What I want is that the code should work as a clip card, remembering the number of purchased coffees, and awarding the customer a free coffee every 10th purchase. I'm writing to a file and reading from it in order for a customer to be able to continue his clip card where he left of last time. So to my problem...I have properly been able to write my "count" variable to a file, and it is storing it correctly. However, every time I run the program again it starts off a 0 and I don't see why. I need it to save the current count, and read the count once run again. For example, if a customer has previously purchased 7 coffees and is returning to the store, his counter needs to start at 7. For some reason it is not doing that.

Here's what I have so far:

public class FelixNeww {

    public static void main(String [] args) {
        Scanner key;
        String entry;
        int count = 0;
        String password = "knusan01";

        FelixNeww  f = new FelixNeww();

       System.out.println(f.readFromFile());

        while(true) {
            System.out.println("Enter password: ");
            key = new Scanner(System.in);
            entry = key.nextLine();
            if(entry.compareTo(password) == 0){
                count++;
                System.out.println("You're one step closer to a free coffe! You have so far bought " 
                        + count + " coffe(s)");
                f.saveToFile(count);
            }
            if(count == 10  && count != 0){
                System.out.println("YOU'VE GOT A FREE COFFE!");
                count = 0;
            }
            if(entry.compareTo(password) != 0){
                System.out.println("Wrong password! Try again.\n");
            }
        }



    }

    public void saveToFile(int count)
    {
        BufferedWriter bw = null;
        try
        {
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("C:\\Temp\\countStorage.txt"))));
            bw.write(Integer.toString(count));
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(bw != null)
            {
                try
                {
                    bw.close();
                }
                catch(IOException e) {}
            }
        }
    }

    public int readFromFile()
    {
        BufferedReader br = null;
        try
        {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Temp\\countStorage.txt"))));
            String line = br.readLine();
            int count = Integer.parseInt(line);
            return count;
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(br != null)
            {
                try
                {
                    br.close();
                }
                catch(IOException e) {}
            }
        }
        return 0;
    }


}

Upvotes: 0

Views: 68

Answers (2)

lgraham076
lgraham076

Reputation: 81

The scope of count variable is local in both instances

public static void main(String [] args) {
    Scanner key;
    String entry;
    int count = 0;
    String password = "knusan01";


   System.out.println(f.readFromFile());

public int readFromFile()
{
    BufferedReader br = null;
    try
    {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Temp\\countStorage.txt"))));
        String line = br.readLine();
        int count = Integer.parseInt(line);
        return count;

In the readFromFile function, you read it from the file, return it, but don't keep track of it in a variable, why don't you replace the println with this inside your main:

count=f.readFromFile

Upvotes: 0

Edwin Torres
Edwin Torres

Reputation: 2864

You are currently setting your count variable to 0. You should set it to the value that's in the file. Do this just before the while loop:

count = f.readFromFile();
while(true) {

You should also implement a way to gracefully exit the while loop. For example, if the user enters "q", you can execute the break; statement to exit the while loop. And after your while loop, call key.close(); to close the Scanner object.

Upvotes: 2

Related Questions