Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Why is the output diffrent when using brackets

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Why is the output diffrent when using brackets

    So I'm kinda confused by this thing i've encountered last night. When i write my code like this

    public class test {
    	public static void main(String[] args) {
    		int[] field = {2, 20, 3, 13, 15, 3, 9, 10, 1, 5, 8};  
       int count = 0;
       for(int i = 0; i<field.length; i++) {
    	   if(i% 2 != 0) {
    		   count++;
    	   }
       }
     
     
       System.out.println(count);
    }
    }

    To get the odd numbers in this array the count value is 5,but when I write it like this the output is 7

    public class test {
    	public static void main(String[] args) {
    		int[] field = {2, 20, 3, 13, 15, 3, 9, 10, 1, 5, 8};  
       int count = 0;
       for(int i = 0; i<field.length; i++) {
    	   if(field[i]% 2 != 0) {
    		   count++;
    	   }
       }
     
     
       System.out.println(count);
    }
    }

    Obviously the second code is right, but why is the first code not working properly, why does the field[i] make such a big diffrence?

    Thanks!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why is the output diffrent when using brackets

    why does the field[i] make such a big diffrence?
    field[i] is the value in the field array at index i. field[i]'s value has no relationship to the value in i.

    The first code only looks at the values in i.
    The second code looks at the values in the field array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    arhzz (September 22nd, 2020)

  4. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Why is the output diffrent when using brackets

    Great answer thanks!

Similar Threads

  1. [SOLVED] why casting int to String is not possible through brackets method
    By voltaire in forum Java Theory & Questions
    Replies: 2
    Last Post: May 2nd, 2010, 04:00 PM
  2. sync two diffrent kind of threads?
    By adamruss in forum Threads
    Replies: 1
    Last Post: January 10th, 2010, 08:59 PM
  3. help writeing to a jtextarea from a diffrent class
    By mdstrauss in forum AWT / Java Swing
    Replies: 6
    Last Post: October 6th, 2009, 09:56 AM
  4. merging two tables from two diffrent htmls files using java
    By sukant_at in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 1st, 2009, 05:13 AM
  5. merging two tables from two diffrent htmls files using java
    By sukant_at in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 7th, 2009, 12:48 PM