XMLEnocder mess.. (Java)

Soldato
OP
Joined
23 Dec 2010
Posts
3,483
Thanks for all of the help guys, I've just got one more issue now.

We have to save the information as 'human readable text' now.

I've done methods for the save which look like this.

Code:
/* -------------------- Saving and Loading Text -------------------- */

    public void saveTextBook(String fn) throws IOException {
        PrintWriter outfile = new PrintWriter(new OutputStreamWriter(
                new FileOutputStream(fn)));

        outfile.println(books.size());

        for (Book b : books) {
            outfile.println(b.getTitle());
            outfile.println(b.getPrice());
            outfile.println(b.getAuthor().getName());
        }

        outfile.close();
    }

    public void saveTextAuthor(String fn) throws IOException {
        PrintWriter outfile = new PrintWriter(new OutputStreamWriter(
                new FileOutputStream(fn)));

        outfile.println(authors.size());

        for (Author a : authors) {
            outfile.println(a.getName());
            outfile.println(a.getNationality());
        }
        outfile.close();
    }

    public void loadTextBook(String fn) throws IOException {
        Scanner infile = new Scanner(new InputStreamReader(new FileInputStream(
                fn)));

        int num = infile.nextInt();
        infile.nextLine();

        for (int i = 0; i < num; i++) {
            String t = infile.nextLine();
            double p = infile.nextDouble();
        }
        infile.close();

    }

    public void loadTextAuthor(String fn) throws IOException {
        Scanner infile = new Scanner(new InputStreamReader(new FileInputStream(
                fn)));

        int num = infile.nextInt();
        infile.nextLine();

        for (int i = 0; i < num; i++) {
            String n = infile.nextLine();
            String nat = infile.nextLine();            
            
        }
        infile.close();
    }

How would I go about loading this directly from the file?

As you can see, the load methods are there - but they don't work!

Thanks.
 
Soldato
Joined
30 Dec 2003
Posts
5,770
Location
London
Have you tried stepping through it with the debugger?

On first glance, assuming the file reading works, it's reading both into local string variables and then doing nothing with them before looping round again.

Presumable you need to instantiate / clear the lists and add new Author and Book objects to them which are created from these locals.
 
Soldato
OP
Joined
23 Dec 2010
Posts
3,483
Have you tried stepping through it with the debugger?

On first glance, assuming the file reading works, it's reading both into local string variables and then doing nothing with them before looping round again.

Presumable you need to instantiate / clear the lists and add new Author and Book objects to them which are created from these locals.

Pardon?
 
Soldato
Joined
30 Dec 2003
Posts
5,770
Location
London
Ok, I'll try and give an example. Take this method:

Code:
    public void loadTextAuthor(String fn) throws IOException {
        Scanner infile = new Scanner(new InputStreamReader(new FileInputStream(
                fn)));

        int num = infile.nextInt();
        infile.nextLine();

        for (int i = 0; i < num; i++) {
            String n = infile.nextLine();
            String nat = infile.nextLine();            
            
        }
        infile.close();
    }

I'm going to assume that the file reading parts work - I'm not familiar enough with Java to say otherwise. If it does, then the for loop will run as many times as there are authors. (If it doesn't, then putting a breakpoint and stepping through with a debugger will let you see what happens at each step).

On each run through, 'n' will be set to the author's name and 'nat' will be set to the author's nationality.

Where's the code that actually creates a new Author object from this? An Author needs to be created with the name and nationality, then needs to be added to the ArrayList 'this.authors' before it loops again.

It also may be an idea to clear this ArrayList before starting this read operation - though it depends what you actually want the result to be (i.e. whether you want to append more data, or reset).
 
Back
Top Bottom