• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is wrong with this code?  RSS feed

 
william keelman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get my java program to write to a file so...
I have this at the top:
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;

Then have this later:
System.out.println ("Please enter the name of the file that you want to create:");
BufferedWriter file = new BufferedWriter(new FileWriter(input.nextLine()));

Then I want to add aditional info to the file (it will be saved as an html file):
file.write ("<p> " + info + "</p>");


I believe it has something to do with file.write but idk please tell me

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"a ve",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additionally, this forum is for questions on Servlets. I will move your post to a more appropriate forum.
 
william keelman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay...still need help please...
 
william keelman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now I believe that there is also something wrong with
BufferedWriter file = new BufferedWriter(new FileWriter(input.nextLine()));

Thanks and Please respond as soon as possible,
Will
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend that you split the line up into smaller parts and examine what each part does, and what intermediate results are found on the way. For example:

becomes

Splitting up the two Writers will make it easier for you to debug if an error occurs.

If that doesn't help, then what is it that makes you think there's a problem? What are the symptoms?
 
william keelman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CMD SAID THIS:

D:\>javac myhtmler.java
myhtmler.java:21: cannot find symbol
symbol : class Writer
location: class myhtmler
Writer fw = new FileWriter(fileName);
^
myhtmler.java:22: cannot find symbol
symbol : class Writer
location: class myhtmler
Writer bw = new BufferedWriter(fw);
^
myhtmler.java:73: cannot find symbol
symbol : variable file
location: class myhtmler
file.write (" ");
^
myhtmler.java:74: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<html>");
^
myhtmler.java:75: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<head>");
^
myhtmler.java:76: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<title>" + name + "'s Web page </title>");
^
myhtmler.java:77: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("</head>");
^
myhtmler.java:78: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<body bgcolor=" + col + ">");
^
myhtmler.java:79: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<font color=" + fcol + ">");
^
myhtmler.java:80: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<font size=" + size + ">");
^
myhtmler.java:81: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<font face=\"" + face + "\">");
^
myhtmler.java:82: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<h1>" + name + "'s Page </h1>");
^
myhtmler.java:83: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<h2> Designed with MyHtmler </h2>");
^
myhtmler.java:84: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<p> " + info + "</p>");
^
myhtmler.java:85: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<p> " + infoa + "</p>");
^
myhtmler.java:86: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<p> " + infob + "</p>");
^
myhtmler.java:87: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<p> " + infoc + "</p>");
^
myhtmler.java:88: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<p> " + infod + "</p>");
^
myhtmler.java:89: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<img src=\"" + img + "\"width=\"" + w + "\"height=\
"" + h + "\">");
^
myhtmler.java:90: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<img src=\"" + imga + "\"width=\"" + wa + "\"height
=\"" + ha + "\">");
^
myhtmler.java:91: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<img src=\"" + imgb + "\"width=\"" + wb + "\"height
=\"" + hb + "\">");
^
myhtmler.java:92: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<img src=\"" + imgc + "\"width=\"" + wc + "\"height
=\"" + hc + "\">");
^
myhtmler.java:93: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("<img src=\"" + imgd + "\"width=\"" + wd + "\"height
=\"" + hd + "\">");
^
myhtmler.java:94: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("</font>");
^
myhtmler.java:95: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("</body>");
^
myhtmler.java:96: cannot find symbol
symbol : variable file
location: class myhtmler
file.write ("</html>");
^
26 errors

I do not thing that file.write is a valid command, what do I type instead? Also, there was something wrong with the thing you gave me, although I think it is closer that what I had had before.
[ May 02, 2006: Message edited by: william keelman ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looks like you forgot to import the java.io.Writer class.
[ May 02, 2006: Message edited by: Garrett Rowe ]
 
william keelman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now I only have 24 errors (all the same type)...here is what is at the top:
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
public class myhtmler
{
public static void main (String args [])
{

And here is a sample of one of the 24 lines with pretty much the same idea:

file.write("</html>");

like i mentioned 1238817489719872389127 times I do not think "file.write" is a valid command...if not what is?

Just for reference CMD said with the new changes:

D:\>javac myhtmler.java
myhtmler.java:74: cannot find symbol
symbol : variable file
location: class myhtmler
file.write(" ");
^
myhtmler.java:75: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<html>");
^
myhtmler.java:76: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<head>");
^
myhtmler.java:77: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<title>" + name + "'s Web page </title>");
^
myhtmler.java:78: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("</head>");
^
myhtmler.java:79: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<body bgcolor=" + col + ">");
^
myhtmler.java:80: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<font color=" + fcol + ">");
^
myhtmler.java:81: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<font size=" + size + ">");
^
myhtmler.java:82: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<font face=\"" + face + "\">");
^
myhtmler.java:83: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<h1>" + name + "'s Page </h1>");
^
myhtmler.java:84: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<h2> Designed with MyHtmler </h2>");
^
myhtmler.java:85: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<p> " + info + "</p>");
^
myhtmler.java:86: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<p> " + infoa + "</p>");
^
myhtmler.java:87: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<p> " + infob + "</p>");
^
myhtmler.java:88: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<p> " + infoc + "</p>");
^
myhtmler.java:89: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<p> " + infod + "</p>");
^
myhtmler.java:90: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<img src=\"" + img + "\"width=\"" + w + "\"height=\"
" + h + "\">");
^
myhtmler.java:91: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<img src=\"" + imga + "\"width=\"" + wa + "\"height=
\"" + ha + "\">");
^
myhtmler.java:92: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<img src=\"" + imgb + "\"width=\"" + wb + "\"height=
\"" + hb + "\">");
^
myhtmler.java:93: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<img src=\"" + imgc + "\"width=\"" + wc + "\"height=
\"" + hc + "\">");
^
myhtmler.java:94: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("<img src=\"" + imgd + "\"width=\"" + wd + "\"height=
\"" + hd + "\">");
^
myhtmler.java:95: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("</font>");
^
myhtmler.java:96: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("</body>");
^
myhtmler.java:97: cannot find symbol
symbol : variable file
location: class myhtmler
file.write("</html>");
^
24 errors

P.S. I still have my old version as comments...so feel free to try something as long as it won't mess up my computer...
 
Henry Wong
author
Sheriff
Posts: 22814
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It complaining that you don't have a variable called file. Now, either... You never declared the "file" variable. Or you did, but it is not in context when you actually used it.

It would help if you show the code around that.

Henry
 
william keelman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will nothing you said help, but someone who knew nothing of Java fixed it so I am closing this post.
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marty suggests you look at our FAQ, HowToAskQuestionsOnJavaRanch, because the better questions you ask, the more help we can be.
[ May 04, 2006: Message edited by: Joe Ess ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!