473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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.Associations;

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(String 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(String subject) {
this.subject = subject;
}


public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(" { Book Details --");
sb.append("Title:" + getTitle());
sb.append(", ");
sb.append("Author:" + getAuthor());
sb.append(", ");
sb.append("Subject:" + getSubject());
sb.append(". } \n");

return sb.toString();
}
}

2>

package java.Associations;
import java.awt.print.Book;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

//For jdk1.5 with built in xerces parser
//import com.sun.org.apache.xml.internal.serialize.OutputFo rmat;
//import com.sun.org.apache.xml.internal.serialize.XMLSeria lizer;

//For JDK 1.3 or JDK 1.4 with xerces 2.7.1

import com.sun.org.apache.xml.internal.serialize.OutputFo rmat;
import com.sun.org.apache.xml.internal.serialize.XMLSeria lizer;


public class XMLCreatorExample {

//No generics
List myData;
Document dom;

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

//initialize the list
loadData();

//Get a DOM object
createDocument();
}


public void runExample(){
System.out.println("Started .. ");
createDOMTree();
printToFile();
System.out.println("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 InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);

myData.add(new Book(strLine));
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("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
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
//get an instance of builder
DocumentBuilder db = dbf.newDocumentBuilder();

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

}catch(ParserConfigurationException pce) {
//dump it
System.out.println("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.createElement("Books");
dom.appendChild(rootEle);

//No enhanced for
Iterator it = myData.iterator();
while(it.hasNext()) {
Book b = (Book)it.next();
//For each Book object create <Book> element and attach it to root
Element bookEle = createBookElement(b);
rootEle.appendChild(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 createBookElement(Book b){

Element bookEle = dom.createElement("Book");
bookEle.setAttribute("Subject", b.getSubject());

//create author element and author text node and attach it to bookElement
Element authEle = dom.createElement("Author");
Text authText = dom.createTextNode(b.getAuthor());
authEle.appendChild(authText);
bookEle.appendChild(authEle);

//create title element and title text node and attach it to bookElement
Element titleEle = dom.createElement("Title");
Text titleText = dom.createTextNode(b.getTitle());
titleEle.appendChild(titleText);
bookEle.appendChild(titleEle);

return bookEle;

}

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

try
{
//print
OutputFormat format = new OutputFormat(dom);
format.setIndenting(true);

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


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

serializer.serialize(dom);

} catch(IOException ie) {
ie.printStackTrace();
}
}


public static void main(String args[]) {

//create an instance
XMLCreatorExample xce = new XMLCreatorExample();

//run the example
xce.runExample();
}
}
Apr 6 '09 #1
7 2575
r035198x
13,262 8TB
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.
Apr 6 '09 #2
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.
Apr 6 '09 #3
r035198x
13,262 8TB
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.
Apr 6 '09 #4
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
Apr 6 '09 #5
r035198x
13,262 8TB
Like I said above, you should read the text file using FileReader. See this article for further details.
Apr 6 '09 #6
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.
Apr 6 '09 #7
r035198x
13,262 8TB
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.
Apr 6 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: acemann7 | last post by:
Windows 2000 udb v 7.2 Got the latest fixpak 13 Applied it. Still can't get a single java sp to run. I used to get reason "2"... now reason "0" - reason 0 is not even documented. I point...
1
by: Praveen | last post by:
Hi, I have installed WebSphere Portal on AIX and connected to DB2 on a remote machine, Getting the followin errors when trying to get the values from database thru applications installed on...
2
by: Aleksandar | last post by:
Hi, I need to convert set of Java classes exported from IBM Modeling environment to C# for implementation. When I invoke JCLA conversion from File->Open->Convert in Visual Studio 2005 it starts...
1
by: Giovanni Tirloni | last post by:
Hello, I'm trying to connect to DB2 Express-C for Linux using the JDBC Universal Driver and I get the error below. I thought it was my code then I installed Websphere Community Edition 1.0.0.1...
2
by: Richard | last post by:
Our web programmer was looking in his application log an found the following error: 2006-08-31 16:33:35,129 ERROR org.hibernate.util.JDBCExceptionReporter - < SQL0723N An error occurred in a...
4
by: cnixuser | last post by:
Hello, I am posting reguarding some modifications I made to a text editor program that I wrote in my java class at my school today. The file I was modifying is a very simple java text editor with...
5
by: ajos | last post by:
hi frnds, this is the way i ve written--> <html:text name="bdgtmastForm" property="publicity_code" size="5" maxlength="5"> but its giving me an error which seems irrelevent.. type Exception...
1
by: ajos | last post by:
hi evrybdy, the problem is:- i had this running before in jsp but when i changed the jsp page using struts tags there occoured a problem... when i enter values in the 2 text boxes and click enter...
3
by: nnaveenraju | last post by:
Hi Gurus, I am new to Ajax and JAVA. I am able to call the servlet class from the JSP page using AJAX. The servlet class is called successfully. I am processing some data and the data has...
0
by: louis08 | last post by:
When i connect to the server i get a BufferOverflowException, HELP!!!! This is where the error is thrown. final Transport transport = new TCPTransport(client);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.