If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

I really strongly suggest you use the book to at least cover the basics. Once you understand classes and methods you suddenly have the entire Java libraries written by Sun available to you! Until then, you arein danger of just staggering on, never really understanding the behaviour or the consequences of what you're writing.

Originally posted by ArchAngel
2. I want to create a large selection of words (about 50)... one of these will be chosen at random...
what would be the best way to go about this?

the only solution I came up with was a switch but I'm sure there would be better ways

selecting all the text on a web page, dumping it into a good text editor and find/replacing out all the \n and \r, then dump the resulting single-line text into a string variable.. then myString.split(" ")ting it when you star thte program and keeping the array returned, using it like:
myArray[Math.random()*myArray.length]

Originally posted by nothing
I have a problem where if I see some code and how it works I can generally adapt it to what I want even If I have no idea how it really works or even what its called.

but that's never the best teacher..

See I don't even know what a method actually is, but I constantly use them... same with arrays, but I don't really actually know how they work, I still use them though.

heres a quick run down:

programs are like recipes. they are list of well defineed instructions
programs are broken down into modules (in object orientation) or classes (as we call them in java)
modules are broken down into methods (or functions as we call them in vb/c)
a method is a list of instructions with a particular purpose. the name of the method gives some indication as to purpose

now suppose we are reading a recipe and is says "beat eggs" - we know how to do that, we know to get a whisk, stick it in the eggs, move it with our arm.. so we can call this process beating eggs. its a method. no recipe book in the world will write "get whisk, stick in eggs, move arm" over and over again.. thye jsut write "beat eggs"
so if you find yourself pasting the same thing over and over into a program, you can make a load of instructions into a method that will save time and effort, and also mean you only have to change one place if you want to make a change. so a method defines a common set of instructions, but what if we want to change the behaviour.. we use arguments.
a recipe book might say "beat eggs ferociously, beat eggs to within an inch of their life, beat eggs for 5 minutes, beat eggs until light and fluffy, beat eggs until a smooth yellow is produced"
the same process of beating eggs, but you have some additional argument that further refine how you will carry out the instructions

methods are the same.. System.out.println() is a method. it ALWAYS prints something to the dos box. but WHAT it prints depends on what you supply as an argument.

indexOf ALWAYS looks in a string, for the occurrence of some letter (or another string).. but you have to tell it what string to look for
myName = "Matt"
myName.indexOf("t");//returns 2 (M=0, a=1, t=2..)
myName.indexOf("m");//returns -1 (not found, case sensitive you see)

methods might have a return value as you see above.. if your mum tells you to beat eggs, youre going to tell her when it's done, or maybe if you failed to manage it

just like this:

int x = 2+2;
2+2 is evaluated and spits out 4, which goes into x and is stored there, the same applies for methods that return a value:

we dont have to use the return value.. sometimes a method does something, and so long as it does it, we dont care for the return value so we dont have to catch it. in indexOf's case, this is pretty dumb not to, because it is the result we want. you may come across enumerations in the future.. they are used to pull results out of databases and things like that. in a typical enumaration, you call a method called nextElement() which moves a pointer on by one,a nd returns what it is pointing to
suppsoe we had 5 elements A B C D E
initially the pointer is positioned before A, so calling next() moves it to A and A is returned..

but suppose we actually only wanted C.. we want to ignore A and B that get returned:
next() //returns a
next() //returns b
Object theWantedElement = next() //returns C, and stores it

in the first 2, we are solely using next() for its ability to move the pointer, in the third, we capture what it returns too.. so sometimes you might want to ignore a return for good reason (usually becaus eit is not of interest)

-----------

arrays, well, you know what a variable is. its aname you can use to refer toa value. arrays are no different really.. they are just a variable but the name has one important fact; it can be changed programmatically

using your example:

word1 = "hello"
word2 = "world"
word3 = "this"
word4 = "rocks"

these are all separate variables, and you CANT do this:

for i = 1 to 4 do{
print( word+i )
}

(the loop is written in pseudocode for easy reading; you dont write java loops like that)
why? word resolves to.. well.. nothing
and i resolves to a number.. but java doesnt resolve i to a number, then stick it on the end of word (e.g. word1) and then go off and look for word1

instead, you need to define an array, which has a 2 part name:

String[] word = new word[4];

now, our array itself is called word, and each element within that array is referred to by word[a number]
you cant change the WORD part of the name programmatically.. but you can change the number part of the name
word[0], word[1], word[2]

making our loop like this:

for i = 1 to 4 do{
print( word[i] )
}

java WILL resolve i to a number, and stick it in the square brackets.. and then look up which string applies.

it often helps to think of arrays as a load of buckets tied together. they have a common name, e.g. "theBuckets" "theWords" and to get the contents of a particular bucket you say the name of the whole lot, followed by the number of the one you want.. like an address, you say the name of the whole street, plus the number of the house you want

each position in our array refers to a thing in memory somewhere.. things in memory usually have names, like:

String word1 = "banana"
word1 is the name

with arrays the name of the array, plus the index, is actually the whole name of a value:

words[27] = "banana"
words[27] is the name of the variable holding banana
words is the name of the array..

theres different ways of looking at these things, but i find it helps to consider arrayName[index] as jsut the complete name of some variable.. with the knowledge that i can modify that name programmatically, just like i would add a number to a string:

thanks alot for your help, I'm actually getting it now. Just one more annoying question, that should be easier to explain to me now.

For a program in the assignment I'm comparing a char variable with a string from a textfield. It says they are incompatible. For numbers from text fields, I've just parsed it (I don't really know how parsing works though, I just adapted other code again.) But I'm having trouble with dealing with a character. At the moment you can enter anything in the text field but I'm going to make it so that only one character can be entered.

Well you can't compare them for precisely the reason it says, they are incompatible. A string is an object much like any other, a char is a primitive type variable (like an int or a boolean). What you need to do is convert the string to a char array (the String class has a method for doing this), then you can access the individual chars in the string.