how do i use a forloop to increment array indices? In Java

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
Darktalyn1
DBB Admiral
DBB Admiral
Posts: 1699
Joined: Thu Mar 23, 2000 3:01 am

how do i use a forloop to increment array indices? In Java

Post by Darktalyn1 »

i have a character array with 4 indices called

char[] pegs = new char[4];


i want to use a for loop that will go through and perform the same action on each of the indices

however when i type

for(pegs = 0; pegs <=3; pegs++){

//all the code goes in here

}

it tells me that im using incompatible types. So, i realize I'm not that much of a coder ... i guess i can see why it's not working - it's a character array, not int. The question is ... how do i do it then?!

Thanks in advance
User avatar
fliptw
DBB DemiGod
DBB DemiGod
Posts: 6458
Joined: Sat Oct 24, 1998 2:01 am
Location: Calgary Alberta Canada

Post by fliptw »

why do you want to use chars arrays? Java does have a String object you know.

anyways, you can't increments arrays, which is what pegs++ is doing, but pegs[i++](where i is an int), incrementing the index of the array is perfectly legal.

something like:

Code: Select all

for(int i =0; i<=3; i++){
pegs[i]='l';
...
}
read thru the API documentation, you'll lots of nifty stuff.
User avatar
Grendel
3d Pro Master
3d Pro Master
Posts: 4390
Joined: Mon Oct 28, 2002 3:01 am
Location: Corvallis OR, USA

Post by Grendel »

Pegs is your pointer to the data -- you need an index variabl like

cahr[] pegs = new char[4] ;

for ( int i = 0 ; i <=3 ; ++i )
{
pegs = ... ;
}

Note: the for ( int i .. is C++ syntax dunno if java will do it. Split it out of the for if not.
User avatar
DCrazy
DBB Alumni
DBB Alumni
Posts: 8826
Joined: Wed Mar 15, 2000 3:01 am
Location: Seattle

Post by DCrazy »

Grendel, Java's Array objects are not the same thing as C/C++ arrays, in that they are not pointers to blocks of memory. They are actual objects (Java.lang.Array). Java will do the int i = 0; in the for loop, but I'm not entirely sure that it has the prefix increment operator (++i). Just as valid to use i++ anyway.
Darktalyn1
DBB Admiral
DBB Admiral
Posts: 1699
Joined: Thu Mar 23, 2000 3:01 am

Post by Darktalyn1 »

Hey thanks guys you're right. that did work ! Now I just have to figure out how to use the forloop with a modulo operator...
User avatar
fliptw
DBB DemiGod
DBB DemiGod
Posts: 6458
Joined: Sat Oct 24, 1998 2:01 am
Location: Calgary Alberta Canada

Post by fliptw »

the modulus operator returns the remainder of the division of two integers. so 5 % 2 would return 1, and 4 % 2 would return 0.

you are pretty much walking thru each of the indexes of the pegsMatched array, so you can condense those three blocks of code into one loop.
User avatar
DCrazy
DBB Alumni
DBB Alumni
Posts: 8826
Joined: Wed Mar 15, 2000 3:01 am
Location: Seattle

Post by DCrazy »

This is one of those Mastermind games, isn't it? You have to guess a code of four colored pegs, and it tells you which ones are in the right spot, which ones are out of place, and which ones aren't even in the code?
Post Reply