Welcome to Emulationworld

Forum Index | FAQ | New User | Login | Search

Make a New PostPrevious ThreadView All ThreadsNext Thread*Show in Threaded Mode


SubjectFirst java program! Can you help with one small syntax problem? Reply to this message
Posted byLordEvilElmo
Posted on11/25/03 09:33 AM



Hey all - finally got around to trying out Java at a basic level - I have a problem though, my first program - a basic array search program is limited by the hard coding of a for loop. I want to make it so that the array legnth is used instead of the known ammount of entires...

Heres what I have!

for (int i =0; i < computerarray[]; i++)
{
if (input .equals(computerarray))
{
System.out.print("\t*\tFound record '" +input +"' in Array record[" +i +"]\t\t*\n");
a++;
}
else
{
System.out.print("\t*\tRecord '" +input +"' not found in Array record[" +i +"]\t*\n");
}
}

Now, I know that I am mixing my types here - a string with an int - is tere any way to compare the 2?

Thanks!

When twilight dims the skies above
Recalling thrills of our love
There's one thing I'm certain of
Return...
I will...
to old...
BRAZIL.



SubjectRe: First java program! Can you help with one small syntax problem? new Reply to this message
Posted byfinaldave
Posted on11/26/03 04:18 PM



> Hey all - finally got around to trying out Java at a basic level - I have a
> problem though, my first program - a basic array search program is limited by
> the hard coding of a for loop. I want to make it so that the array legnth is
> used instead of the known ammount of entires...

To find the length of an array you do computerarray.length, that's what you are after here I believe.
For strings you can do string.length() as well.


>
> Heres what I have!
>
> for (int i =0; i ))
> {
> System.out.print("\t*\tFound record '" +input +"' in Array record[" +i
> +"]\t\t*\n");
> a++;
> }
> else
> {
> System.out.print("\t*\tRecord '" +input +"' not found in Array record[" +i
> +"]\t*\n");
> }
> }
>
> Now, I know that I am mixing my types here - a string with an int - is tere any
> way to compare the 2?
>
> Thanks!
>
> When twilight dims the skies above
> Recalling thrills of our love
> There's one thing I'm certain of
> Return...
> I will...
> to old...
> BRAZIL.
>

I'll tell you, that's one thing I do like about Java, the size of a object is nicely packaged with the object itself. In C/C++ it's very messy, and you can't really return an array and then go array.length on it :-(

(Mind you that's only 1 thing I like about Java vs. C++ and 99 other things I hate in Java versus C++, so it doesn't count for too much!)


You learn something old everyday...



SubjectRe: First java program! Can you help with one small syntax problem? new Reply to this message
Posted byLordEvilElmo
Posted on11/27/03 02:11 PM



> To find the length of an array you do computerarray.length, that's what you are
> after here I believe.

Thats absolutey perfect :) Thanks mate!

> (Mind you that's only 1 thing I like about Java vs. C++ and 99 other things I
> hate in Java versus C++, so it doesn't count for too much!)

lol :)

When twilight dims the skies above
Recalling thrills of our love
There's one thing I'm certain of
Return...
I will...
to old...
BRAZIL.



SubjectRe: First java program! Can you help with one small syntax problem? new Reply to this message
Posted bygalibert
Posted on11/27/03 03:35 PM



> I'll tell you, that's one thing I do like about Java, the size of a object is
> nicely packaged with the object itself. In C/C++ it's very messy, and you can't
> really return an array and then go array.length on it :-(

In C, yes. In C++, you don't return an array, you return a vector.

OG.





Previous ThreadView All ThreadsNext Thread*Show in Threaded Mode