Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

please help with a newbie to programming

Status
Not open for further replies.

viingx

Programmer
Oct 17, 2002
5
US
hi all

I'm very very new to java programming. As the matter of fact, this is the first programming class I've ever taken. I'm not asking for an assignment help for I am trying to read ahead and practice as i go along. This is the test program that I'm writing but for some reason it always get the

"cannot resolve symbol error at File f = new File("Try.txt");"

all i'm trying to do is just to output the message into the file Try.txt

can someone tell me what i'm doing wrong? thank you. once again, i'm really really new, so please don't flame me.



/**/

class Test
{
public static void main(String[] args)
{
File f = new File("Try.txt");
FileOutputStream ff = new OutputStream(f);
PrintStream p = new PrintStream(ff);

p.println("bla");
p.println("blabla");
p.println("blabla");
}
}
 
It is important to import the packages that you intend to use at the top of your code. The package you need to import is java.io. Add this line to the top of your code:

import java.io.*;

-gc "I don't look busy because I did it right the first time."
 
godcomplex...thank you very much for replying

I took your suggestion but there is still an error

right now my code looks like this


/**/
import java.io.*;
class Test
{
public static void main(String[] args)
{
File f = new File("Try.txt");
FileOutputStream ff = new OutputStream(f);
PrintStream p = new PrintStream(ff);

p.println("bla");
p.println("blabla");
p.println("blabla");
}
}


this is the error that i got.


C:\cs140\bla.java:10: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
FileOutputStream ff = new FileOutputStream(f);
 
you must 'catch' IO exceptions ...

try :
Code:
import java.io.*;
class Test {
   public static void main(String[] args) {
     try {
         File f = new File("Try.txt");
         FileOutputStream ff =  new  OutputStream(f);
         PrintStream p = new PrintStream(ff);

         p.println("bla");
         p.println("blabla");
         p.println("blabla");
     }
     catch (FileNotFoundException fnfe) {
         System.out.println("I can't find the file ! : " +fnfe);
     }
     catch (IOException ioe) {
       System.out.println("Something bad happened : " +ioe);
     }
    }
}

Ben
 
viingx,

The information that you have received so far from the forums looks most informative, of particular importance is the post regarding try / catch blocks which are used for dealing with exceptions. If you are not familiar with exceptions yet then you will be doing yourself a favour by visiting:


I noticed a small inconsistency in your code, in your post you used the following code line:
Code:
FileOutputStream ff =  new  OutputStream(f);
It is my belief that the above line should actually be ammended to:
Code:
FileOutputStream ff = new FileOutputStream(f);
This slight alteration in code along with the use of try / catch blocks in your code should clear up any problems that you are having.

Regards

Davo
 
thanks again for all of the suggestions, it has been really informative and i've finally gotten it...with throw exceptions.

/**/
import java.io.*;
class Test
{
public static void main(String[] args) throw Exception
{
File f = new File("Try.txt");
FileOutputStream ff = new FileOutputStream(f);
PrintStream p = new PrintStream(ff);

p.println("bla");
p.println("blabla");
p.println("blabla");
}
}

I still don't know how to "catch" an error yet, but thank you JProg for the site. I will be using the most of it.
 
viingx,

I showed you how to 'catch' errors in the above post !!
 
sedj

i know, and i really appreciate it. what i meant exactly was that i still didn't know as in "where to start" i'm not sure if that makes any sense to you but, hmm...i just need to learn a bit more that's all. :)

thank you
 
viingx,

I have some code I can send you which you can study. It includes File I/O, Exceptions, and some other cool stuff, but it's a bit big to post here. If you have an e-mail address you don't mind posting here, I'll send it to you. Or if you prefer I'll just post it here.

It's up to you,

Greg _______________________________________
Constructed from 100% recycled electrons
 
MissouriTiger

Thank you for offering the code for me to study :). Please send it to neayjai@yahoo.com.

Thank you.
 
Okay, I sent you some code. Also, here's a method which demonstrates a popular usage of exception handling.

It tests a String to nake sure all characters are numeric. Just pass a String to it & if any characters are non-numeric, it returns FALSE. Otherwise it returns true.

public static boolean isNumeric(String s)
{
String strIn = s;
try
{
Double.parseDouble(strIn);
return true;
}
catch(Exception e)
{
return false;
}
}


Hope this is useful,

Gregamundo
_______________________________________
Constructed from 100% recycled electrons
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top