Error in the java Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • svpriyan
    New Member
    • Apr 2009
    • 18

    #1

    Error in the java Code

    Dear Buddies,
    I have two Java Prog:

    My task is to read the Content of the file and write to an XML file.
    so i started doing it perfectly...
    What i have problem is when i tried to execute the main class, i am not able to reach the book class yet. it says like this

    My error comes under loadData() method.

    cannot find symbol
    symbol: Constructor book(java,lang, String)
    location: class.java.awt. print.Book


    because i used line by line command to read from file and there is no way to read that way in Book class???( parameter is not passed int hat way)
    can you help me to solve this problem.

    I realy got stuck with this

    Thanks


    My progs are
    1>

    package java.Associatio ns;

    public class Book {

    private String title;

    private String author;

    private String subject;

    public Book(String subject) {
    this.subject = subject;
    }
    public Book() {

    }

    public Book(String title, String author, String subject) {
    this.title = title;
    this.author = author;
    this.subject = subject;
    }

    public String getAuthor() {
    return author;
    }

    public void setAuthor(Strin g author) {
    this.author = author;
    }

    public String getTitle() {
    return title;
    }

    public void setTitle(String title) {
    this.title = title;
    }

    public String getSubject() {
    return subject;
    }

    public void setSubject(Stri ng subject) {
    this.subject = subject;
    }


    public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append(" { Book Details --");
    sb.append("Titl e:" + getTitle());
    sb.append(", ");
    sb.append("Auth or:" + getAuthor());
    sb.append(", ");
    sb.append("Subj ect:" + getSubject());
    sb.append(". } \n");

    return sb.toString();
    }
    }

    2>

    package java.Associatio ns;
    import java.awt.print. Book;
    import java.io.*;
    import java.util.Array List;
    import java.util.Itera tor;
    import java.util.List;

    import javax.xml.parse rs.DocumentBuil der;
    import javax.xml.parse rs.DocumentBuil derFactory;
    import javax.xml.parse rs.ParserConfig urationExceptio n;

    import org.w3c.dom.Doc ument;
    import org.w3c.dom.Ele ment;
    import org.w3c.dom.Tex t;

    //For jdk1.5 with built in xerces parser
    //import com.sun.org.apa che.xml.interna l.serialize.Out putFormat;
    //import com.sun.org.apa che.xml.interna l.serialize.XML Serializer;

    //For JDK 1.3 or JDK 1.4 with xerces 2.7.1

    import com.sun.org.apa che.xml.interna l.serialize.Out putFormat;
    import com.sun.org.apa che.xml.interna l.serialize.XML Serializer;


    public class XMLCreatorExamp le {

    //No generics
    List myData;
    Document dom;

    public XMLCreatorExamp le() {
    //create a list to hold the data
    myData = new ArrayList();

    //initialize the list
    loadData();

    //Get a DOM object
    createDocument( );
    }


    public void runExample(){
    System.out.prin tln("Started .. ");
    createDOMTree() ;
    printToFile();
    System.out.prin tln("Generated file successfully.") ;
    }

    /**
    * Add a list of books to the list
    * In a production system you might populate the list from a DB
    */
    private void loadData(){

    try{
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream ("C:\\coding\\1 .txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream (fstream);
    BufferedReader br = new BufferedReader( new InputStreamRead er(in));
    String strLine;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null) {
    // Print the content on the console
    System.out.prin tln (strLine);

    myData.add(new Book(strLine));
    }
    //Close the input stream
    in.close();
    }catch (Exception e){//Catch exception if any
    System.err.prin tln("Error: " + e.getMessage()) ;
    }


    }

    /**
    * Using JAXP in implementation independent manner create a document object
    * using which we create a xml tree in memory
    */
    private void createDocument( ) {

    //get an instance of factory
    DocumentBuilder Factory dbf = DocumentBuilder Factory.newInst ance();
    try {
    //get an instance of builder
    DocumentBuilder db = dbf.newDocument Builder();

    //create an instance of DOM
    dom = db.newDocument( );

    }catch(ParserCo nfigurationExce ption pce) {
    //dump it
    System.out.prin tln("Error while trying to instantiate DocumentBuilder " + pce);
    System.exit(1);
    }

    }

    /**
    * The real workhorse which creates the XML structure
    */
    private void createDOMTree() {

    //create the root element <Books>
    Element rootEle = dom.createEleme nt("Books");
    dom.appendChild (rootEle);

    //No enhanced for
    Iterator it = myData.iterator ();
    while(it.hasNex t()) {
    Book b = (Book)it.next() ;
    //For each Book object create <Book> element and attach it to root
    Element bookEle = createBookEleme nt(b);
    rootEle.appendC hild(bookEle);
    }

    }

    /**
    * Helper method which creates a XML element <Book>
    * @param b The book for which we need to create an xml representation
    * @return XML element snippet representing a book
    */
    private Element createBookEleme nt(Book b){

    Element bookEle = dom.createEleme nt("Book");
    bookEle.setAttr ibute("Subject" , b.getSubject()) ;

    //create author element and author text node and attach it to bookElement
    Element authEle = dom.createEleme nt("Author");
    Text authText = dom.createTextN ode(b.getAuthor ());
    authEle.appendC hild(authText);
    bookEle.appendC hild(authEle);

    //create title element and title text node and attach it to bookElement
    Element titleEle = dom.createEleme nt("Title");
    Text titleText = dom.createTextN ode(b.getTitle( ));
    titleEle.append Child(titleText );
    bookEle.appendC hild(titleEle);

    return bookEle;

    }

    /**
    * This method uses Xerces specific classes
    * prints the XML document to file.
    */
    private void printToFile(){

    try
    {
    //print
    OutputFormat format = new OutputFormat(do m);
    format.setInden ting(true);

    //to generate output to console use this serializer
    //XMLSerializer serializer = new XMLSerializer(S ystem.out, format);


    //to generate a file output use fileoutputstrea m instead of system.out
    XMLSerializer serializer = new XMLSerializer(
    new FileOutputStrea m(new File("book.xml" )), format);

    serializer.seri alize(dom);

    } catch(IOExcepti on ie) {
    ie.printStackTr ace();
    }
    }


    public static void main(String args[]) {

    //create an instance
    XMLCreatorExamp le xce = new XMLCreatorExamp le();

    //run the example
    xce.runExample( );
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Please use code tags when posting code.
    2.) Your book class is clashing with the import java.awt.print. Book class. Why do you need both Book classes? Fully qualify their names in the code if you need both.

    Comment

    • svpriyan
      New Member
      • Apr 2009
      • 18

      #3
      Thanks for the Information, sure, next time i will do it in that way.
      Yea i able to get the code work,
      though still my problem exists with the Reading file in the loadData() Method. because Book.java does not have any method to satisfy the call from load data.

      How can i solve that, if u can .. please kind enough to give some idea.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        1.) Do not use those classes for reading character files. Use FileReader wrapped with BufferedReader or the Scanner(1.5 +) class.
        2.) Well to create a book you need author, title e.t.c. You therefore need to read the values of those attributes from the xml file before calling the Book class' constructor.

        Comment

        • svpriyan
          New Member
          • Apr 2009
          • 18

          #5
          Hai
          thanks for your reply
          My main idea is :
          I have a txt file which already has data. and i willing to read that and generate an XML file

          Now i am in problem with reading the TXT file only.// loadData() Method.

          I need some suggestions for that to solve

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Like I said above, you should read the text file using FileReader. See this article for further details.

            Comment

            • svpriyan
              New Member
              • Apr 2009
              • 18

              #7
              Ok, Thanks
              I will look on it.
              But when i replace this code to the LoadData()
              ==
              private void loadData(){
              myData.add(new Book("Head First Java", "Kathy Sierra .. etc","Java 1.5"));
              myData.add(new Book("Head First Design Patterns", "Kathy Sierra .. etc","Java Architect"));
              }

              ==
              I did not get generated output. do u have any idea.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Put some println statements in your methods to find out how far your code reaches before stopping. You can pin point the problem line that way.

                Comment

                Working...