• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

beginner / idiot

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must be the world's biggest idiot but I can't get even the most basic code (hello world) to compile. I'm on OSX and when I try to compile, terminal just comes out with "1 error" and I don't even know how to check what that error is. It's really embarrassing asking for help but my code looks fine, and I'm saving it in the right directory (at least I'm pretty sure I am - Library/Java). agh I don't have a clue what's going wrong.

Sorry sorry sorry. If I don't learn by doing I don't stand a chance. please don't gnaw on my flesh too much for this...

A.x
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can paste your code here. And you can also tell us how do you compile and run it. Because the information that you have provided is not enough.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whom are you calling an idiot???

Welcome to JavaRanch ( ) and I assure you, however bad you think your code is, we have seen worse

Hint: Somebody asked about errors yesterday and I found a website with compiler errors on: look here. See whether you can find your error on that website, then you can tell us how you sorted it out. If not, show us your code and we'll have a look at it.
 
Ariana Green
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's okay, I worked it out. Obviously it's println not printIn, duh. I did know that, honestly. Just a typo........... *blush*

So, it's compiling and running fine now. I was actually delighted when it worked! Sad, I know. I will be back with even more awful and embarrassing questions in the next few minutes / years / days, I absolutely guarantee it.

Thanks for the welcome and help. That link will come in handy
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done. We have all done that sort of thing, and all know what it feels like.
 
Ariana Green
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, more dumb questions...

I'm trying to write code from scratch because quite frankly I can't get anything I pick up online to compile. I need to learn by doing so this seems the best way. I understand the rationale behind methods, constructors etc but I'm struggling to pool it all together: most of the examples I read are just code snippetse, and the programs I find online to try are dubious to say the least but I I'm not smart enough to work out why (100 errors for a 5 line bit of code!? and this from a consultancy site... whatt??). So anyway...

I start with declaring the public class, then the public static void main stuff (is that required?), then I get a bit stuck. If I want to write a class that iterates through a loop until the conditions are met, how do I declare it? Do I set the variables before I type the method? Do I need a constructor, and if so, what exactly do I need to construct? And so forth.

In my head it's all got very muddled and complicated...

I *thought* I understood it fine until I actually started to write the stuff! (Yes, I know. Don't shout at me, at least I'm trying...)
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ariana Green wrote:OK, more dumb questions...

I'm trying to write code from scratch because quite frankly I can't get anything I pick up online to compile. I need to learn by doing so this seems the best way. I understand the rationale behind methods, constructors etc but I'm struggling to pool it all together: most of the examples I read are just code snippets that obviously won't execute, and the things I find online to try are dubious but I can't work out why (100 errors for a 5 line bit of code!? and this from a consultancy site... it must be ME making the mistake, right??). So anyway...

I start with declaring the public class, then the public static void main stuff (is that required?), then I get a bit stuck. If I want to write a class that iterates through a loop until the conditions are met, how do I declare it? Do I set the variables before I type the method? Do I need a constructor, and if so, what exactly do I need to construct? And so forth.

In my head it's all got very muddled and complicated...

I *thought* I understood it fine until I actually started to write the stuff! (Yes, I know. Don't shout at me, at least I'm trying...)



You're doing much better than when I started. Okay, you're getting the idea of writing Java code indeed. the 'public static main' stuff is required indeed. When you run the program, that's where the Java Virtual Machine starts from. Otherwise, if you've got a bunch of methods, how would it know what it should execute first.

>>> If I want to write a class that iterates through a loop until the conditions are met, how do I declare it?
You probably meant a function (or method) to make all conditions meet? You could have

Unless == Not if
Until == Not while

>>> Do I set the variables before I type the method?
Not necessarily. You only need to set local variables before you use them in your method.

>>> Do I need a constructor, and if so, what exactly do I need to construct?
Nope. Usually constructors are used to initialize class variables. You don't need to declare one. The compiler creates a default constructor for you.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Disagree:

But I know a lot of people will disagree with me. Look on this as my opinion.

You are quite right to write code from scratch, and to try copying code you find elsewhere. You are right to copy it by hand; even though you get compiler problems with spelling errors, you will learn more quickly. In my opinion, you learn nothing by copying-and-pasting code you have found elsewhere. And even better, copying code by hand gives you the opportunity to change it, then you can see what happens if . . .

You do realise that one error in a piece of code will show itself several times; if you spell the name of a variable wrongly when you declare it, then every time that variable is used, the compiler will perceive an error. So if you use the variable number 100 times in a class, but declared it as private int nummber; the compiler won't notice that "nummber" is wrong, but will complain 100 times that it can't find "number."

Although you can get away without writing constructors, it is probably good practice to write a constructor for every class you write. Never mind that you have a few lines of extra code writingIt gets you into the habit of writing a constructor every time. So when you need a constructor which actually does something, you know what a constructor looks like and you can writeand when it doesn't work, perceive the nasty error I have written in that last bit of code.

You need a main method somewhere to start off an application. I think you need to get into the habit of writing as little as possible in the main method. You will find some books, and even the Java Tutorials, where lots is written in the main method, and other books (eg Deitel and Deitel, Head First Java) where the main method is very short. I think a one-line main method including a line like new Foo(3).loop(); is the ideal. The reason books tend to write lots in the main method is that it saves space and you can see the control structure easily. but in my opinion (and people disagree with me, I know), it is more difficult to learn object-orientation then how to write a loop.

You can put all that lot together and get a class like this, which I have made by putting together snippets from earlier.You can find examples of loops in this section of the Java Tutorials. Start simple. If you start off by copying the loops from the Java Tutorials into the loop method, it is a start. Get them to work, then see what happens if you change them, eg change <= to <, or even >.

Bookmark the Java Tutorials; despite my reservation about code in the main method, once you find your way around the Tutorials you will be impressed how much useful material there is in them.

See if you can get what I have written to work. But beware. The error I mentioned earlier is still in there. I challenge you to find it.

Don't anybody tell her where it is; let her find it herself. If you don't find it reasonably quickly, I shall tell you.
 
Ariana Green
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sigh. Lateral thinkers do not make good programmers.

I first thought that the error might be a missing ; then I was wondering if the method (method? constructor, whatever it is) should have been something other than void as it's needed to, well, do something. I then thought that, well, it's an integer so the name "number" wouldn't work. Seems like I was wrong on every point! Either that or there's something else wrong anyway. The constructor name matches the class name so it's not that. Can a public, uh, thing call a private, uh, thing - good grief, I don't even know WHAT I'm talking about now -

So... in short, I couldn't fix it after ten minutes and gave up. Tell me, pleeease? Because I won't be able to sleep otherwise...

My understanding of Java is, as is now apparent, sketchy at best. At worst... well.

Yes, I figured out the bit about the compiler errors, they confuse the heck out of me. I managed to fix one problem in my code and twenty-nine errors disappeared at once, so I'm starting to understand that. And I even figured out how to work out which line the errors are on... yes, that took me several hours. As for the error messages themselves, I seem to have two common ones:

llegal character: \172 -and- illegal character: \8224

I don't know WHAT that means. I thought they were Unicode characters but that just comes up with random characters I don't even have in my code, so... huh?

It's a learning curve and I'm NOT going to quit, no matter how much I embarrass myself. I want to weep with joy every time I get a piece of code to actually run, my failure rate is that high.

Well, better get back to, I don't know, whatever it is I'm supposed to be doing right now.

Thanks all for the help..
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can work out what the characters are on the Unicode website. I put "172" in the box on top and it said to open the pdf for U0100, when I found this

Ų
0172 LATIN CAPITAL LETTER "U" WITH OGONEK
≡ 0055 "U" 0328 $̨

but if it was 172 decimal then it would come out as ¬ which is the logical "not" symbol which isn't used in Java and if it was 172 octal then it's "z" which is permissible in Java. \8224 appears to be Chinese.
The likeliest cause of getting these peculiar symbols is using a work processor. Make sure to use a decent text editor. Not Notepad. Try NotePad++ or Notepad2 if you are on Windows. You can set options like different colours for keywords, automatic indentation and bracket matching which make programming easier.
Note that you might get line numbers added to your code if you copy and paste off the screen on JavaRanch, which the compiler doesn't like. Copy the code to a decent text editor and go to the end of each line before where it says "illegal character" and push delete. That might get rid of characters you can't see between lines. If you see any suspicious tiny boxes on screen. get rid of them.

I shall have more to say . . .

The error is one "void" too many. I shall let you look for it; I shall be back in about ½ hour.
 
Ariana Green
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh. Well, I was close (not really, but telling myself that makes me feel better...)

I'm on OSX, and I use TextWrangler. It's designed for developing so I can't see why there'd be a problem with it. Deleting all the extra spaces behind the braces has so far removed half of the errors, but I've still got 50 remaining ;)

I'm not going to be able to give up on this one until I get it to compile.

Great learning curve...
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Copy some of the loops in the Java Tutorials page I showed you into the loop() method. Try changing the <= to < or even to >
Change the bit about 11 to number. You are now using the field in your Foo class.
Try these classesSee what happens with a do-while loop, and a while loop, when invoked by those 3 classes.

*******************************************************************************************

We all know what it is like not to be able to understand compiler errors. When I started I had several weeks when nothing seemed to work, and we old lags have been through it ourselves.

Lateral thinking is useful for programming, but it helps if duplicated with a spelling error. You need lateral thinking for some things, and literal thinking for others

******************************************************************************************

A problem we see quite commonly here on JavaRanch is what happens if you add a return type to a constructor. You will remember that constructors always omit the return type. But if you write "public void Foo(int number)" as I did earlier, you convert the constructor to a method, with a rather inappropriate name. (Actually using Foo for the name of this class was not a good idea.) Something will go wrong pretty quickly, and you will not be able to see what happened. Anyway delete that "void" and all should be well. Write public Foo(int number) not public void Foo(int number).

******************************************************************************************

Compilers are not that difficult to write, but error checking is difficult. I tried one once and managed to translate simple Eiffel code into C. But if it thought there was an error I would get this sort of output

Identifier i out of expected context in line 3.
Operator := out of expected context in line 3.
Identifier j out of expected context in line 3.
Operator + out of expected context in line 3.
Number 3 out of expected context in line 3.



Actually with that one correction, the Foo class should compile and run, but with the loop method empty will produce no result. Try the class in that state, then the FeeDemoMinus3 etc classes. Then start putting a body in the loop() method, and put it together bit by bit. Don't write more than about 5 lines of code at a time, before you try compiling, then another 5 lines. That means, when you find a new error, you have probably only got 5 lines of code to search through for the error!

**********************************************************************************

The bit about public and private is to do with "encapsulation," which is an important part of object-oriented programming. Keep the data hidden, by making "number" private. But provide a "public" constructor which allows any code to set up "number" sort of indirectly.

And good luck with it. I assure you, it will get better.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see you beat me to it by 1 minute. I am not familiar with TextWrangler, and have no idea why it is inserting extraneous characters.
 
Ariana Green
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I didn't think the void should have been there, but for the wrong reason

I think I just about understand the difference between methods and constructors.

So, I'm now down to one compiler error, down from 100. Deleting every single space and putting a 'fresh' one in has got rid of all of the Unicode errors, don't known what happened there (though I think I pasted it directly from the browser before tinkering with it which might explain it).

I wrote a for statement from the java tutorials to put into the loop and so far it hasn't been rejected so I'll leave that in - if this thing ever compiles it will be good to see it working!!! I have this thing about watching which bit of code does what, I tend to not know what's going on otherwise. SO I was determined to work this out for myself, but since there's only one remaining error message I think I will plead for mercy:



Because, quite frankly, I can't see ANYTHING wrong with that. EDIT: incidentally, it makes no difference to the compiler if I make it static or not, or if I shove an ; on the end before the brace... which is something I didn't know... perhaps someone could clarify that for me?

Oh, and this is a really dumb question, but what's the definition of a return type? Because I know, sort-of, what it kind-of is, but not enough to be completely sure...
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your main method has to be static or it won't work.

Most likely cause for that error, which you won't find on MindProd: too many { before you get there.

Addition: Count all the { at the beginnings of your class and methods, and all the } at their ends. You must have exactly 1 more { than } before you write "public static void main . . ."
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a method like public void loop(). The return type is void, which is a keyword, and that means the method does not return any particular information to wherever it was called from .

You have a method like public int getNumber(). You put return number; as the method body. The return type is now int, which means it returns an int number to wherever it was called from. (And int is also a keyword). You can return any type you can get a reference to. String, Foo, List, you name it, you create an object of it, you can return it. Or any primitive type, eg int.

A method which is declared as void must not return anything.
A method which is returned with a return type must return something of that type (or another type which can readily be converted to it).
A method which "forgets" to declare a return type won't compile.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am very sorry, but I appear to have missed out a "static" before one of the main methods I mentioned earlier.
 
Ariana Green
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I put the static back in before I posted, then took it out again because the same error came up regardless? I'm back up to five error messages now... the more I try to fix it the worse it seems to get.

I didn't know the thing about the braces though. Now, should I be declaring my main method first, before anything else? And, do you mean exactly one more open brace than a closed one? Because I have several pairs, but they will be closed.

Here's a weird thing: when I move the } to below the main method, I get three different compiler errors all saying "missing method body or declare abstract."

I'm absolutely flipping determined to work this one out but if I don't manage it within the hour, I'll post the whole lot as it stands right now. I don't know how I make a simple thing so complicated, honestly...
 
Ariana Green
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK I don't know which is more annoying, the fact that it won't work or the fact that I can't figure out why...

This is slightly modified from your original code so I might have done something blatantly wrong here:



And the compiler error:



I was hoping that I HAD specified a return type ops: ops: ops:

Oh dear. Maybe I should just bury the computer and go live in a cave somewhere.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm absolutely flipping determined to work this one out but if I don't manage it within the hour, I'll post the whole lot as it stands right now. I don't know how I make a simple thing so complicated, honestly...



Instead of posting the "whole lot", it might be better to post the error messages (and maybe some of the methods that the messages are referring to).


And BTW, the number of errors don't mean much. A single error can chain react to other errors. This is why you should only fix one or two errors (the first ones) and then recompile.

Henry
 
Ariana Green
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:the number of errors don't mean much. A single error can chain react to other errors. This is why you should only fix one or two errors (the first ones) and then recompile.



yes, I'm starting to notice that what I mean is that I fix one error and get a whole load of different ones in its place. I'm starting to understand why that is though. Thanks!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying to nudge you in the right direction: the last code you posted has three semicolons too many, and one return type too few (in the "loop" declaration).
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Ulf and Henry for helping.

We don't usually give out straight answers, but I have already posted a suggested solution, so I am happy to tell you.
At the end of the constructor heading and both method headings, you have a ; after the ) and before the {. Delete those three semicolons.
Add a return type to the loop method. Change public loop() to public void loop(). No ; there.

When you have done those things, it will compile and run.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something I forgot. Have you got a ; after the for(...) and before the {? Try running your code with that ; then take the ; out and recompile and run it again.

That additional ; is a common occurrence which is usually a mistake. The compiler and JVM interpret it as meaning for however many times do nothing then go to the next line.
 
Ariana Green
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I finally got it to work.

Right, that's given me plenty of material to try and digest. Especially with regards to the semicolons, it makes a lot of sense now I can understand why they're there (and when they shouldn't be). And, yes, I understand the actual process a lot more (pretend wisdom speech). And there is definitely sense in only changing a few things at a time...

I'm off to experiment. I think I've got enough information to not have to ask any more questions about constructors, loops or methods. Which should keep me going for a little while :roll:

Apologies for taking so long and just being generally slow.... ops: ops: ops:
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome. We are only too pleased to help people who are obviously trying to do some work.

And who aren't idiots
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wanted to say thanks to the widely experienced people who give (quite) some of their time to help beginners!

A short story I'd like to share: back in college, a friend of mine had decided to cut n paste snippets of code to
get his assignment done. In this code, he tried to call functions directly by class name (replaced the object reference
name with the class name, figuring that was the name that he would use in his program). This didn't work well
and he started to randomly try stuff. Seeing as main was static, he decided to make some other methods
static. And this worked, somewhat. Eventually with a bit more fumbling he figured out how to print employee
data on the screen without creating instances of his Employee class. He passed, because the program printed
somewhat correct outcome and with a bit of explaining from the teacher (who just got into java himself and figured
this would be a way one _could_ use staticness) my friend was ready for the next level course. The next level
course was more about OO fundamentals. One could pass this course with doing some UML right and so did
my friend. He also said he had no clue about what OO meant for programming. Luckily my friend never wanted
a job as programmer.

Obviously, to learn java, cutting and pasting code does not add much. It's like having a car that's missing a
part of the engine and you have all kinds of parts of which some even seem to fit. You have to get into the
details of the matter and start at a very basic level. Reserve time to work on it. And it's a good thing that people
here give hints (otherwise, it'd be copy/pasting the solution from their mind to yours - and you'd be nodding but
wondering what to do when you're actually about to write code).
 
reply
    Bookmark Topic Watch Topic
  • New Topic