3 Replies - 777 Views - Last Post: 13 May 2012 - 09:15 PM Rate Topic: -----

#1 kappa03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-October 11

Java question mark operator - using with <

Posted 13 May 2012 - 07:24 PM

I am trying to print arrays according to two boolean settings, showCost and showBoard.

Cost is a single value at the start of an array, and board is the rest of the array.

My problem is that although "j = showCost ? 0:1" works fine, the compiler complains about "j < showBoard ? n+1:1". It says I can't use the ? operator with <. Is there any way around this?

for(i = 0; i < popSize; i++) {
            for(j = showCost ? 0:1; j < showBoard ? n+1:1; j++) {
               System.out.print(population[i][j]);
            }
            System.out.println();
         }



Is This A Good Question/Topic? 0
  • +

Replies To: Java question mark operator - using with <

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Java question mark operator - using with <

Posted 13 May 2012 - 08:26 PM

It's called the ternary conditional operator or the if-then-else statement.

State in plain English what you're trying to do in that for loop statement. This is my best translation of what you've currently written:

for(j = showCost ? 0:1; j < showBoard ? n+1:1; j++)

if showCost is true, j = 0, else j = 1 ;

if showBoard is true, j < n + 1, else j < 1 ;

increment j

So you want j to start from either 0 or 1. That seems reasonable. Then you want j max to be either n or 0. I'm thinking there's a better, simpler way to write that for statement.
Was This Post Helpful? 1
  • +
  • -

#3 kappa03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-October 11

Re: Java question mark operator - using with <

Posted 13 May 2012 - 08:46 PM

Thanks, yea you got it right, I wanted the starting j to equal 0 or 1, and to keep printing items in the array until either n or 0, depending on booleans.

I ended up creating a new variable jMax = showBoard ? n+1:1
Putting jMax into the loop declaration worked fine.

However, I'm still confused why the less-than operator won't work with the ternary operator. To me it seems that the ternary operator is just a method that should return an int, right? And it's correct to have "int < int"...
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Java question mark operator - using with <

Posted 13 May 2012 - 09:15 PM

Congrats on arriving at a solution. I did some experimenting. You can actually get what you were trying to do to work doing something like (I changed the first part for my own simple example. replace with your own):

for ( int j = 0 ; j < ( showBoard ? n+1:1 ) ; j++ )
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1