Welcome to Dream.In.Code
Become a Java Expert!

Join 150,166 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,336 people online right now. Registration is fast and FREE... Join Now!




Wrighting to File

 
Reply to this topicStart new topic

Wrighting to File, Im having some problems with my Wright to file code

Eonian
26 Aug, 2008 - 04:53 AM
Post #1

New D.I.C Head
*

Joined: 26 Aug, 2008
Posts: 5


My Contributions
I am trying to wright a program for a Gallery this code is ment to add a painting to the catalogue file and every time i run it it displays an error "Error: ';' expected"


CODE

public class Catalogue
{
    // CONSTANTS ***************************************************************
    
    final String CAT_FILE = "piclist.txt";
    final String TEMP_FILE = "temp.txt";
    
    final int[] PREMIUMS = {0, 3, 6, 10, 15,
                            25, 40, 60, 85, 120, 160};
                            
    final int SALE_MULTIPLE = 150;
    
    // INSTANCE VARIABLES ******************************************************

    // (none)
    
    // CONSTRUCTORS ************************************************************
    
    /**
     * Constructor
     */
    Catalogue ()
    {
        // no actions
    }

    // METHODS    
    
    void addPicture(int pCatNum, String pTitle, int pCategory)
    {
        try
        {
            FileWriter fw = new FileWriter(CAT_FILE, true);
            PrintWriter pw = new PrintWriter(fw);
        
            pw.println(pCatNum + "\t" + pTitle + "\t" + pCategory);
            fw.close();
        }
        
        catch (java.io.IOException ioe)
        {
            User.message("ERROR: unable to write to catalogue file");
            User.message("EXCEPTION: " + ioe);
        }
        
    }


can anyone help me please

User is offlineProfile CardPM
+Quote Post

akozlik
RE: Wrighting To File
26 Aug, 2008 - 07:57 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 714



Thanked: 31 times
Dream Kudos: 800
My Contributions
What does the entire error say? It should tell you what line the error is on.
User is offlineProfile CardPM
+Quote Post

Eonian
RE: Wrighting To File
26 Aug, 2008 - 08:01 AM
Post #3

New D.I.C Head
*

Joined: 26 Aug, 2008
Posts: 5


My Contributions
QUOTE(Eonian @ 26 Aug, 2008 - 05:53 AM) *

"Error: ';' expected"


i have done in the wrighting at the top of my post
User is offlineProfile CardPM
+Quote Post

nick2price
RE: Wrighting To File
26 Aug, 2008 - 08:05 AM
Post #4

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
Hardly an intermediate question, must be the most basic of basic questions. The code you have provided looks ok as far as i can see, but i presume there is more, like a user class etc. You need to also import the correct libraries. Your error is self explanatory, somewhere you are missing a semi column ; Your ide or whatever you are using should point you to the line that is missing this ; find the line and just add it.
User is offlineProfile CardPM
+Quote Post

Eonian
RE: Wrighting To File
26 Aug, 2008 - 08:28 AM
Post #5

New D.I.C Head
*

Joined: 26 Aug, 2008
Posts: 5


My Contributions
Very Sorry im new to this site and wasn't sure on the Ranking system for the questions

I am still learning Java and have been refered on an assignment from uni there is more code the the bit in the post is the only bit i am having a problem with i can remove infomation from the file but i can not add it

i am using BlueJ and it dosent seem to point me to the line though i am new to this program too sorry for the confusin and thanks for your help ill look through my code and see if i can spot it.
User is offlineProfile CardPM
+Quote Post

nick2price
RE: Wrighting To File
26 Aug, 2008 - 09:02 AM
Post #6

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
Post all the code and i will tell you where you are missing the ; As i say, i presume there is more like a User class and a main method to execute the code.
User is offlineProfile CardPM
+Quote Post

Eonian
RE: Wrighting To File
26 Aug, 2008 - 09:16 AM
Post #7

New D.I.C Head
*

Joined: 26 Aug, 2008
Posts: 5


My Contributions
this is the code it should all work on its own

CODE
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;

/*******************************************************************************
* Textfile-based gallery catalogue
*
*******************************************************************************/
public class Catalogue
{
    // CONSTANTS ***************************************************************
    
    final String CAT_FILE = "piclist.txt";
    final String TEMP_FILE = "temp.txt";
    
    final int[] PREMIUMS = {0, 3, 6, 10, 15,
                            25, 40, 60, 85, 120, 160};
                            
    final int SALE_MULTIPLE = 150;
    
    // INSTANCE VARIABLES ******************************************************

    // (none)
    
    // CONSTRUCTORS ************************************************************
    
    /**
     * Constructor
     */
    Catalogue ()
    {
        // no actions
    }

    // METHODS    
    
    void addPicture(int pCatNum, String pTitle, int pCategory)
    {
        try
        {
            FileWriter fw = new FileWriter(CAT_FILE, true);
            PrintWriter pw = new PrintWriter(fw);
        
            pw.println(pCatNum + "\t" + pTitle + "\t" + pCategory);
            fw.close();
        }
        
        catch (java.io.IOException ioe)
        {
            User.message("ERROR: unable to write to catalogue file");
            User.message("EXCEPTION: " + ioe);
        }
        
    }
    // *************************************************************************
    //
    // TO DO: write calcPremiums() method which prints a header saying
    //        "XXX Insurance Premiums" where XXX is the month, then reads each  
    //        record in the catalogue file and for each prints an advice of the
    //        form:
    //
    //        Insurance for Freda Bloggs
    //        Catalogue number: 42
    //        Insurance category: 6
    //        Monthly premium: £1400
    //
    //        and finally returns the total cost of the month's insurance
    // *************************************************************************

    
    /**
     * Remove picture from catalogue and calculate payment
     *
     * @param pCatNum catalogue number
     * @return payment due (or -1 if pNum not found in catalogue file)
     */
    public int sellPicture(int pCatNum)
    {
        String inputLine;
        String[] inputFields;
        int catNum;
        int category = -1;
        int payment;
        
        try
        {
            FileReader fr = new FileReader(CAT_FILE);
            BufferedReader br = new BufferedReader (fr);
            
            FileWriter fw = new FileWriter(TEMP_FILE, false);
            PrintWriter pw = new PrintWriter(fw);
            
            inputLine = br.readLine();
            while (inputLine != null)
            {
                inputFields = inputLine.split("\t");
                catNum = Integer.parseInt(inputFields[0]);

                if (catNum == pCatNum)
                {
                    category = Integer.parseInt(inputFields[2]);
                }
                else
                {
                    pw.println(inputLine);
                }
                inputLine = br.readLine();
            }
            
            fr.close();
            fw.close();
            
            if (category != -1)
            {
                File catFile = new File(CAT_FILE);
                File tempFile = new File(TEMP_FILE);
                catFile.delete();
                tempFile.renameTo(catFile);
                
                payment = PREMIUMS[category] * SALE_MULTIPLE;
            }
            else
            {
                payment = -1;
            }
            return payment;
            
        }
        catch (IOException ioe)
        {
            User.message("Unable to read/write from catalogue file");
            User.message("EXCEPTION: " + ioe);
            return -1;
        }
    }
    


}

User is offlineProfile CardPM
+Quote Post

nick2price
RE: Wrighting To File
26 Aug, 2008 - 09:28 AM
Post #8

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
That isnt all the code, as i say, ur missing a user class where you output your exceptions. So i changed them too
CODE
System.err.println("Unable to read/write from catalogue file");
            System.err.println("EXCEPTION: " + ioe);


Everything compiles fine now, but as i say, nothing is going to run unless you add a main method.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:53AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month