Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9169199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:48:58+00:00 2026-06-17T15:48:58+00:00

I was recently programming and ran into an issue using the ? : operand.

  • 0

I was recently programming and ran into an issue using the ? : operand. Here’s my code.

    Random rand = new Random();
    for(int x = 0; x < 3; x++) {
        rand.nextInt(1) == 0 ? vertShip(board) : horizShip(board);
    }

My compiler throws me an error stating that the left hand side of the line (rand.nextInt(1) == 0 ) must be a variable. I’ve tried variants such as

    Random rand = new Random();
    int a = rand.nextInt(1);
    for(int x = 0; x < 3; x++) {
        a == 0 ? vertShip(board) : horizShip(board);
    }

or if statements in the left hand side but they don’t fix the problem. Would anyone be able to help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:48:59+00:00Added an answer on June 17, 2026 at 3:48 pm

    Not every expression is a statement. Use an if statement here. See Section 14.8 Expression Statements in the Java SE 7 Java Language Specification.

    Certain kinds of expressions may be used as statements by following
    them with semicolons.

    ExpressionStatement:
        StatementExpression ;
    
    StatementExpression:
        Assignment
        PreIncrementExpression
        PreDecrementExpression
        PostIncrementExpression
        PostDecrementExpression
        MethodInvocation
        ClassInstanceCreationExpression
    

    Examples of expression statement for each of the above:

    x = y;
    ++x;
    --x
    x++;
    x--;
    fn(); // Or donkey.fn();, etc.
    new Donkey(this);
    

    What you can’t do is:

    b ? f() : g();
    f() + g();
    

    However, if you’re dead set on obfuscating your code, I guess you could write:

    fn(a == 0 ? vertShip(board) : horizShip(board));
    (a == 0 ? vertShip(board) : horizShip(board)).fn();
    

    (I think. I don’t have a compiler to hand and wouldn’t usually write such code.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently started programming my first Cocoa app. I have ran into a problem
During Flex programming, I recently ran into several questions ( about box models ,
I'm a BizTalk newbie, and recently ran into some issue where the biztalk orchestration
I'm new to web programming and recently started getting into PHP. I wrote a
I was recently studying the book named Advanced Linux Programming and I ran into
I recently started game programming on the iPhone using Cocos2d and Box2d. So here's
I have recently started programming in WPF and bumped into the following problem. I
I recently followed a way of programming for Android using Scala and Eclipse ,
I have been recently digging into Mobile Programming, I practically tried out the J2ME
I recently started programming in WinForms using C#. I have a requirement where I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.