Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
java code looping through an array

why is my code here not working im trying to print out every second number in the sequence as hello

public class Generalizations {
    public static void main(String[] args) {


        for(int i=0;i<10;i++)
            System.out.println(i);
        if (i%2==0){
            System.out.println("hello");
        }
    }
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

To print every second element, print inside the condition as shown below:

for(int i=0;i<10;i++) {//esnure the brace here       
   if (i%2 == 0) {
       System.out.println(i);//prints every second element
   }
}
over 4 years ago · Santiago Trujillo Report

0

You missed the curly braces of for loop.

public class Generalizations {
    public static void main(String[] args) {


        for(int i=0;i<10;i++){
            System.out.println(i);
            if (i%2==0){
                System.out.println("hello");
            }
        }
        }
    }
over 4 years ago · Santiago Trujillo Report

0

use this

public class Generalizations {
public static void main(String[] args) {


    for(int i=0;i<10;i++){
        System.out.println(i);
        if (i%2==0){
            System.out.println("hello");
        }
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!