Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

First program by myself. Need help?

 
Avery Jerauld
Greenhorn
Posts: 28
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Executable code is allowed only inside methods and constructors, not in the body of the class.

And welcome to the Ranch!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Seriously.

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.
 
Joydeep Ghatak
Ranch Hand
Posts: 43
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

http://java.sun.com/docs/codeconv/html/CodeConventions.doc3.html
http://java.sun.com/docs/codeconv/CodeConventions.pdf

Welcome to JavaRanch !!!

Thanks,
Joydeep
 
Avery Jerauld
Greenhorn
Posts: 28
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 detail...so you expand it.


eventually you get to the point where you know how to write the java for each item.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic