Java beginner with no previous programming experience seeking advice

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2874 Views - Last Post: 19 February 2011 - 07:45 PM Rate Topic: -----

#1 Noah89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-February 11

Java beginner with no previous programming experience seeking advice

Posted 18 February 2011 - 12:09 AM

Like my topic title says: I have no experience at all, so I've been trying to do the "hello world" tutorial but for some reason it just doesn't work. I can't remember the messages that the IDE would give me but no matter how hard I tried I couldn't figure out what I was doing wrong. I would type the code as the tutorial showed but I'm not sure if fit as in the right place (I'm sure I typed it correctly so the mistake was something else or maybe I just skipped something important)Please, If anyone could tell where to find a good step-by-step tutorial, book, or anything that will help me understand all the basic of programming in java. I've been using NetBeans as my IDE.

I will appreciate any help/advise! Thanks...

Is This A Good Question/Topic? 0
  • +

Replies To: Java beginner with no previous programming experience seeking advice

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Java beginner with no previous programming experience seeking advice

Posted 18 February 2011 - 01:06 AM

Well the first thing you should do is post your exact code as seen in your IDE.

I know when you use Netbeans to create a "Java Application" it does automatically created a file/class called package.Main

If you put your HelloWorld class within this file it will generate an error. You have two options, remove the Main class from the project and add a new class called HelloWorld then build, or post the code within the Main however only use the main method, the code would be as follows

class Main {
  public static void main(String[] args) {
    System.out.println("HelloWorld!");
  }
}

Was This Post Helpful? 0
  • +
  • -

#3 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Java beginner with no previous programming experience seeking advice

Posted 18 February 2011 - 06:31 AM

When you create a class, it must be in a file of the same name which is the first thing. If your class is called HelloWorld then it will be in a file called HelloWorld.java or else it won't work. That's probably the problem, but it's hard to help with little information.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Java beginner with no previous programming experience seeking advice

Posted 18 February 2011 - 08:01 AM

That only applies to public classes. Multiple non-public classes can exist in the same file.
Was This Post Helpful? 0
  • +
  • -

#5 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Java beginner with no previous programming experience seeking advice

Posted 18 February 2011 - 08:07 AM

View Postmacosxnerd101, on 18 February 2011 - 08:01 AM, said:

That only applies to public classes. Multiple non-public classes can exist in the same file.

Yes, but I'm assuming he's just working with public classes as he just started.
Was This Post Helpful? 0
  • +
  • -

#6 Noah89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-February 11

Re: Java beginner with no previous programming experience seeking advice

Posted 18 February 2011 - 10:47 AM

Hi guys thanks for the feedback! Im posting one pic with my code and the second one is the error that I get when I try to run the project!

FOr what I understood from what DaneAU said, you need to have one file with the name of the class you want, and also, if there would be more than one class in your code you have to create a new class file for that new class of the same name. Am I right?

I gonna try now to fix it! :D
In he meanwhile you guys can laugh at my code! LOL...

Attached image(s)

  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Java beginner with no previous programming experience seeking advice

Posted 18 February 2011 - 07:54 PM

Yea you might notice your class is named helloWorld yet the actual file name is Main.java these two must be the same and your class should be HelloWorld as a capital letter is used for the first letter of a class name.

You could make a simple fix by changing the class name to class Main in the code you posted and it would compile should there be no errors.


Now within your code you have an error
Public static void main(String[] args)


Should be
public static void main(String[] args)


notice that the public is spelled without a capital letter. This should resolve those errors.

Remove all your code and paste the following in to your file.
package helloWorld;

class Main 
{
  public static void main(String[] args) {
      System.out.println("HelloWorld!");
  }
}


As per your comment

Quote

FOr what I understood from what DaneAU said, you need to have one file with the name of the class you want, and also, if there would be more than one class in your code you have to create a new class file for that new class of the same name. Am I right?


Yes you are right. You can have more than one class in the same file. However for now i suggest creating a new file for each class.

Now just as a suggestion, seem as though you are just starting out it is often a lot easier to use a simple text editor and compile via cmd or terminal using javac to get started. Alternatively use a very simple and friendly IDE such as BlueJ - http://www.bluej.org/

For a beginner Netbeans can be overwhelming and it often leads to confusion with the very things you are discussing in this thread.

Anyway i have attached a project file for BlueJ should you download it and use it.

You can open this project up or create your own. It is very helpful for beginners and i recommend using BlueJ

Attached File(s)


This post has been edited by DaneAU: 18 February 2011 - 08:10 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java beginner with no previous programming experience seeking advice

Posted 18 February 2011 - 08:26 PM

and the package statement must be the first line of your file (after comments sure) but actual first line of code
Was This Post Helpful? 1
  • +
  • -

#9 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Java beginner with no previous programming experience seeking advice

Posted 18 February 2011 - 09:20 PM

Yep thanks pbl forgot that part :P
Was This Post Helpful? 0
  • +
  • -

#10 Noah89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-February 11

Re: Java beginner with no previous programming experience seeking advice

Posted 19 February 2011 - 06:22 PM

Hey guys, so I decided to star all over again (Not that It was such a big project!) and it WORKED!!! :D FInally! LOL... and I did it in like 30sec... -.- I kinda feel embarrassed it took so long. However, you guys mentioned that my first line of code is always the package statement. As you might notice there's no package within my code. Could any of you explain that to me?

DaneAU, I downloaded the file. Do I use the Command Prompt to run it??? How can I see the Output of the code in BlueJ???

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java beginner with no previous programming experience seeking advice

Posted 19 February 2011 - 06:27 PM

View PostNoah89, on 19 February 2011 - 08:22 PM, said:

However, you guys mentioned that my first line of code is always the package statement. As you might notice there's no package within my code. Could any of you explain that to me?

If you are a beginner in Java stay away from package right now. Just do not put a package statement in your code.
You'll learn the concept later on when you will need it. (And that may take a while :))
Was This Post Helpful? 0
  • +
  • -

#12 Noah89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-February 11

Re: Java beginner with no previous programming experience seeking advice

Posted 19 February 2011 - 06:35 PM

But what did I do different from my previous attempts? Cuz there was a Package statement and now there's none... I'm just wondering why is that!

and again thanks for all the help and patience! :) Really appreciate it!

This post has been edited by Noah89: 19 February 2011 - 06:42 PM

Was This Post Helpful? 0
  • +
  • -

#13 Noah89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-February 11

Re: Java beginner with no previous programming experience seeking advice

Posted 19 February 2011 - 06:51 PM

Btw, is there any way of knowing which tutorial in the "Java Tutorial Section" is more appropriate for a total beginner like me?
Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Java beginner with no previous programming experience seeking advice

Posted 19 February 2011 - 07:02 PM

All you had to do was ask. :)


Here's a good beginner tutorial:
http://www.dreaminco...a-for-n00blets/
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java beginner with no previous programming experience seeking advice

Posted 19 February 2011 - 07:09 PM

View PostNoah89, on 19 February 2011 - 08:35 PM, said:

But what did I do different from my previous attempts? Cuz there was a Package statement and now there's none... I'm just wondering why is that!

You had an error because the package statement was not the first statement in your code not because it was erronous.

Package garanty uniqueless of names in all the Java world.
Usually you reverse the URL of your site which is unique.
So www.pblinc.ca so all my Java package start by

package ca.pblinc

now your package is also a directory structure so if I develop a SecretCode application I will need the folowing directory structure on my disk:

\ca\pblinc\secretcode

all the .java files in that directory will have

package ca.pblinc.secretcode

if I want to split my application and have

\ca\pblinc\secretcode\decode
\ca\pblinc\secretcode\encode
\ca\pblinc\secretcode\gui


all the .java files in \ca\pblinc\secretcode\gui will start by
package ca.pblinc.secretcode.gui

So if in the \ca\pblinc\secretcode\gui directory I have MyPanel.java
it will start by
package ca.pblinc.secretcode.gui

and it will allow the compiler to differentiate it from jour own MyPanel.java which wouldn't have the same package
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2