This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes a beginner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a beginner" Watch "a beginner" New topic
Author

a beginner

J Fox
Greenhorn

Joined: Feb 01, 2005
Posts: 10
ok, I am a new Java user ( just started college) and i know how to use the

public static void main(String[]args){

but what does it mean exactly?....my teacher isn't all that great and she's teaching incredibly slow too!...so if anyone can explain in "easy to understand" terms....plz do

thanks
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
First, the special part about:
public static void main(String[] args)
is that this is the method the jvm looks for to run a class from the command line. Other than that it is a method just like any other.

public: It has public access, meaning any user of the class has access to it.

static: The method is a member of the Class rather than a member of instances of the class. Static methods only have access to static variables within the class.

void: The return type is void (has no return).

main: The name of the method. As said before the jvm looks for the main method

String[]: This method takes an array of String objects. If you run this class from the command line using: java MyClass one two three : the String array will contain the Strings "one" "two" and "three".

args: The name of the String array. Can be anything, you could call it bob and it wouldn't matter. 'args' is just common practice.

The other thing to understand is this method is enclosed in a Class. The method is meant as an entry point and should not be used to place all your application logic in. Generally I feel really bad if I have much more than:
MyClass class = new MyClass();
class.someMethod();

unless I'm parsing command line arguments. Even then I try and push the parsing into another method.

Any more questions feel free to post.
J Fox
Greenhorn

Joined: Feb 01, 2005
Posts: 10
Thanks alot for the Info. I actually understand what it means now, thanks.
But I do hava one more question....im trying to do a little program and its giving me errors but Im Doing it just like the book.



But it says this at the C:\> prompt...

Echo.java:12: cannot find symbol
symbol : method create(java.io.inputstream)
Location : java.util.Scanner
Scanner scan = scan.nextline();
<--- (it has an upright arrow at that period.)
Echo.java:16: cannot find symbol
symbol : method nextline()
Location : java.util.Scanner
message = scan.nextline();
2 Errors <----(it has the upright arrow by that period too.)

i dont know if it doesnt have that certain tool or (class) but i have no
idea what to do...so if you could help me out...plz

[ EJFH: Fixed tags. ]
[ February 02, 2005: Message edited by: Ernest Friedman-Hill ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Your book may be a bit outdated; perhaps it covers a pre-release version of JDK 1.5. In any case, the Scanner class doesn't have a "create" method. It does have a constructor that takes an InputStream, though, so you could replace

Scanner scan = Scanner.create(System.in);

with

Scanner scan = new Scanner(System.in);

Similarly, there's no "nextline" method. There is, however, a "nextLine" method, with a capital "L". Making this small change should get your program to compile.


[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
To get up-to-date information about the methods available in the Java 1.5 API, you should visit the API documentation. In fact, you should bookmark this page. In my opinion, learning how to navigate the API docs is a crucial skill when programming in Java.

Layne


Java API Documentation
The Java Tutorial
J Fox
Greenhorn

Joined: Feb 01, 2005
Posts: 10
thank ya'll so much....ive been trying to get this kind of help
for so long...tryin to find chats...but that sucks...i really appreciate
all ya'lls help.
J Fox
Greenhorn

Joined: Feb 01, 2005
Posts: 10
ok im using the Scanner scan = scan.nextLine();.....it works and i thank you
but how make it to where when they type in somethin.....i can make a response to that certain work or phrase?... like for instance..

J Fox
Greenhorn

Joined: Feb 01, 2005
Posts: 10
Accidently pressed the tab and space bar....but as i was saying..
how do i make it to where if they put "yes" I could make the program
say "that's good". And when they put "no" I could make it say "that sucks"
thanks...(remeber this is for instance.)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
For future reference, you can easily go back and edit your messages by clicking on the paper-and-pencil icon to the right of your user name. It's handy for situations like the one you encountered here.

As for the answer to your question, it sounds like you need an if statment of some kind. If you need the syntax, I'll let you look it up in your textbook or google for it. With the if statement, you typically need some kind of comparison. In this case, you should probably compare if two Strings are equal using the String.equal() method. Again, you may need to google for the exact syntax.

I've debated whether or not to put an example here, but I think I'll refrain this time. You can pick these things up from any decent Java text or tutorial. If you don't have a text to use as reference, I would suggest that you get one. There are several online texts, such as Bruce Eckel's Thinking in Java. Personally, I like having hard-copy instead of electronic books so I can write notes in the margin and highlite them. If you have the funds, I strongly suggest you look into it. If you need recommendations, use the Saloon's Search tool.

There is a lot of material available to help with Java syntax. From there you can go just about anywhere.

HTH

Layne
J Fox
Greenhorn

Joined: Feb 01, 2005
Posts: 10
Thanks Lane..I'll do that
But is the book you listed the one you have?....
Anyways I'll go to barns and nobles and try to find it
Thanks again!!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
No, I've only read the first chapter or so in Bruce Eckel's book. My primary Java text is Core Java by Cay Horstmann and Gary Cornell. I found this to be a great book to start learning Java. However, I should point out that I already had a firm grasp on C++ at the time, so my background might be different than yours is.

Layne
J Fox
Greenhorn

Joined: Feb 01, 2005
Posts: 10
yeah, but i have another question.....i can get my applet to compile...but how do i execute it
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
put it in an html page and load that page in an html enabled browser.
As you're using the 1.5 compiler that needs to be a browser that has a 1.5 plugin enabled.


42
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
As mentioned above, you need a web page to hold the applet. I don't have an example handy, but you should be able to google for one. During development, you may find it easiest to load the web page with the appletviewer program that comes with the JDK. You can run this from the command line with

appletviewer applet.html

Of course, you can replace "applet.html" with any file name as long as it is an HTML file.

Layne
J Fox
Greenhorn

Joined: Feb 01, 2005
Posts: 10
Where do I get the 1.5 Plugin at??
and I dont really know html that well...not at all really
umm..I saw the example in my book


and if that is the correct "code" how do I upload it to a browser
sorry i ask so many questions....just trying to stay one step ahead
Thanks..
J Fox
Greenhorn

Joined: Feb 01, 2005
Posts: 10
I still need some help with the applet thing...but I have a better question now.
Take this following code for example:

ok, when the user inputs "Good" or "Bad" , how do i get the program to return a reply for either the response of "good" or "bad"...

just a simple question...but i cant figure it out...if you can help that
would be great...thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a beginner