aspose file tools*
The moose likes Beginning Java and the fly likes Creating my First Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating my First Object" Watch "Creating my First Object" New topic
Author

Creating my First Object

Sean Magee
Ranch Hand

Joined: Aug 23, 2004
Posts: 69
Hello there! Since being on these forums, I have learned a lot more about Java programming than I have in class. I am only in an intro class, but still, thank you guys! Just wanted to let ya know.

Now for mt problem. We just had a class on creating objects, but i have been having a lot of problems. The following code was similar to what the professor gave us in class, which was used as an example to learn from, not code that was supposed to be compiled directly by copying and pasting.
There are a lot of things that i am not getting right. Could you please look at my code and give me some pointers? Or maybe point me into the right direction. Thanks!





Code won't compile of course, and I know these are bad variable names, but what variable names would be good for this? Thanks again in advance!
[ October 29, 2004: Message edited by: Sean Magee ]
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
I think you need a space between the new keyword and the File or FileReader class names. You probably also need to import java.io.* to use the File class and the various reader classes.
Apart from that, Java variable names are usually in lowercase and something descriptive that tells the programmer something about the object or primitive being referred to.

Good luck with your course!

DD


SCJP 1.4<br />SCWCD (in progress)
Alain Boucher
Ranch Hand

Joined: Feb 25, 2003
Posts: 51
Try This:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import javax.swing.*;

public class Read extends JFrame{

public static void main (String [] args) {
try{
System.out.println("Begin");
File myFile = new File ("c:\\doc2.txt");
FileReader fr = new FileReader (myFile);
BufferedReader r = new BufferedReader(fr);

String line = r.readLine();

JOptionPane op = new JOptionPane(line);

Read inst = new Read();

inst.setSize(400,400);
inst.getContentPane().add(op);
inst.pack();
inst.setVisible(true);
}catch (Throwable e){
System.out.println("ERROR: " + e.getMessage());
}
}
}


Alain Boucher<br />Spare-Brain Consultants Inc.<br />SCJ2P,SCWCD,
Sean Magee
Ranch Hand

Joined: Aug 23, 2004
Posts: 69
oops, they orignally did have spaced in between, tried that out after first code compiled. Ok, didnt know i had to import anything, will try that out.
Sean Magee
Ranch Hand

Joined: Aug 23, 2004
Posts: 69
Thank you Alain adn Dun! However, Alain, this



What exactly are you doing there? Could you explain?

and my teacher also want us to implement a code like this



however, previously in my code i used the JOption Pane and not the System.out, and compiler expects a ")" for some reason.

I understand that this code makes it so it sops reading the txt when no data is recieved. But, can I use JOption here>?
[ October 29, 2004: Message edited by: Sean Magee ]
Sean Magee
Ranch Hand

Joined: Aug 23, 2004
Posts: 69
here is my updated code.




ANy pointers? Getting weird errors.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Sean -

You're missing a comparison operator. Your code is:


You need to add a comparison operator:



See the comparison now? The while is reading a line into the line variable and, if it's not equal to null, show the message. Typographical errors are the most common mistakes that cause bugs in programs.

Good luck in your class!


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Sean Magee
Ranch Hand

Joined: Aug 23, 2004
Posts: 69
Don't know if anyone cares, but here is my code completed by me, from the help of you fello Ranchers. Thanks!



I do have one question though, if someone can explain this. (sorry i ask too many questions)

At first, i had"
import java.io.*;

and it wouldnt work until i added:
import java.io.File;

How come? Shouldn't: import java.io.*; include import java.io.file?

[ October 29, 2004: Message edited by: Sean Magee ]
[ October 29, 2004: Message edited by: Sean Magee ]
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Sean,

Well done!


At first, i had"
import java.io.*;

and it wouldnt work until i added:
import java.io.File;

I compiled your code without the java.io.File successfully. However, for readability, it is preferable to use "import java.io.File" rather than "import java.io.*". Here is a recent thread that talks about this topic.

Use capital letter for class name, e.g. "ReadFile". You might want take a look at Sun Coding Convention.

No need to use "System.exit(0)" in this case. Let it exit naturally.

Joyce
[ October 29, 2004: Message edited by: Joyce Lee ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating my First Object
 
Similar Threads
comparing two files
JAVA I/O
check values in a csv file in all files in a directory
Flagging duplicate lines when comparing the input from two seperate text files
Write File to Harddrive