File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes First program by myself. Need help? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "First program by myself. Need help?" Watch "First program by myself. Need help?" New topic

First program by myself. Need help?

Avery Jerauld

Joined: Aug 15, 2011
Posts: 28

Hello everyone. I'm new here. I'm taking an online course about coding but the teacher is really bad and isn't doing a good job in teaching the class. I'm trying to read up on beginner's guides everywhere but I still need some help.

I need to write a program to compute the ideal weight for both males and females, where the ideal weight for a female who is 5'3" would be 100 + 15 = 115 pounds and for a male the ideal weight is 106 pounds plus 6 pounds for each inch in height over 5 feet.

I've tried making my own code but I've gotten 6 identifier errors. The teacher told me to use previous codes as an example to what I should do so I used my Circles program (which found the area of a circle with any given radius).

Here's what I came up with for the Ideal Weight program.

Again, I have no idea what anything really means (only some very basic stuff. I have yet to learn how to put it all together), so I tried replacing things that seemed fit.

Don't make fun of me too bad :P
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Executable code is allowed only inside methods and constructors, not in the body of the class.

And welcome to the Ranch!

luck, db
There are no new questions, but there may be new answers.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

In Java, all code must be inside a method (OK, there are a few exceptions, but since you are a beginner, we'll ignore those), and all methods must be inside a class.

You have the class defined, but no methods, and a bunch of executable statements. That won't work. The first thing you should do is create a main method.

But let's back up even further. You should never have gotten this far. I've been writing java code for about 10 years, and I never NEVER write more than 2-3 lines before I recompile, debug, and test.


That way, you know exactly where the problem is going to be, and you don't get 100+ errors and get discouraged.

I took your code, wrapped it in a main method, and re-compiled.

You're missing a semi-colon on one line, so i fixed that...and got 11 errors, because you are missing some import statements.

My point is, you would probably do better to almost start over. EVERY SINGLE TIME I start a new project, my code looks something like this the first time I compile:

Even just now, when I attempted to compile your code, I did this first, and discovered I had typed "statis" instead of "static" on my main method, giving me an error. But since I had only a few lines of code, it was easy to find and correct. Then I pasted your code inside my main method (taking out my println), and recompiled.

If I really wanted to do it, right, I would only and 1-2 lines of your code at a time and recompile after each, fixing errors as I go.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joydeep Ghatak
Ranch Hand

Joined: Sep 30, 2009
Posts: 41

Hi Avery,

As you have started learning Java, along with the logic and syntax, it will be a really good practice to follow the Java coding standard.
You can check the following two links,

Welcome to JavaRanch !!!


“Men are only as good as their technical development allows them to be.”
Avery Jerauld

Joined: Aug 15, 2011
Posts: 28

There must be something wrong with my brain because no matter what, this stuff just isn't clicking for me. I've never had any problems like this. I may have to actually talk to someone to understand this because I read both those links you gave me and "Beginner's Programming of Java Guide for Dummies" and even though it did help, I'm still all kinds of confused when it comes to actually writing by myself.

I understand (mostly) what each "word" does, but I still have no clue how to put it all together.

Jon Avadis
Ranch Hand

Joined: Jul 20, 2011
Posts: 49

Are you using Eclipse?

If you are: When creating a new class, make sure to check the option "public static void main"
under "which method stubs do you wanna create".

Write you lines of code inside the main method (between the curly brackets of the main method).
You might wanna google about the main method to learn more.

Knowledge Reigns Supreme
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

if you are just a beginner, please DON'T use eclipse. You won't be learning java, you'll be learning eclipse. Further, many of the details you need to know/learn are hidden by it.

programming is like building with legos. you build some simple components, then click them together to build a bigger program, etc.

another approach folks often recommend is to not write a single line of code at first, but write down on paper what you want to do. you may have started with something like this:

1) get person's weight
2) get person's height
3) print ideal weight
4) print ideal height[/list]

now you go back and revise the list, putting in more details, revising just one at a time... possibly to something like this:

1a) ask person their weight
1b) get input from user
1c) validate the input
1d) if it is not ok, ask for input again

Now you can look at this. You know how to do 1a and 1b. 1c looks like it needs some more you expand it.

eventually you get to the point where you know how to write the java for each item.
I agree. Here's the link:
subject: First program by myself. Need help?
It's not a secret anymore!