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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    import java.io.*;
    public class MP2Num13
    {
        public static void main (String Args[]) throws IOException
        {
            int abc[15], tot=0, n_count=1;
            BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
            System.out.print("\n");
            for(int x=0; x<15; x++)
                {
                    System.out.print("Index [" + x + "]: ");
                    abc[x]=Integer.parseInt(br.readLine());
                }
            System.out.print("\nNumbers in the even indexes are: \n");
            for(int x=0; x<15; x++)
            {
                    if(abc[x]%2==1)
                    {
                        System.out.print(xx(x) + " ");
                        n_count++;
                    }
                }
            System.out.print("\n \nThe total stored in the odd indexes is: ");
            for(int x=0; x<15; x++)
                {
                    if(abc[x]%2==0)
                    tot+=abc[x];
                }
                System.out.print(tot + "\n");
        }
    }

    -i'm stuck with the error for this part of the code: "abc[15]" and "abc[x]"


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    the error for this part of the code
    Please copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error messages and paste it here.
    MP2Num13.java:13: error: incompatible types
    abc[x]=Integer.parseInt(br.readLine());
    ^
    required: int[]
    found: int
    MP2Num13.java:29: error: bad operand types for binary operator '+'
    tot+=abc[x];
    ^
    first type: int
    second type: int[]
    2 errors

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    You left off the other error messages on this line:
     int abc[15], tot=0, n_count=1;


    See the tutorial for how to define an array:
    http://docs.oracle.com/javase/tutori...ts/arrays.html
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Quote Originally Posted by Norm View Post
    You left off the other error messages on this line:
     int abc[15], tot=0, n_count=1;
    what exactly does that mean?

    --- Update ---

    i just tried., and it didn't work

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    When I compile the posted code I get several errors on the line in my post. I was wondering why you didn't get those errors and include them in your post. What version of the javac command are you using? Enter:
    javac -version
    to get its version.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Quote Originally Posted by Norm View Post
    When I compile the posted code I get several errors on the line in my post. I was wondering why you didn't get those errors and include them in your post. What version of the javac command are you using? Enter:
    javac -version
    to get its version.
    this is my error:

    MP2Num13.java:31: error: bad operand types for binary operator '+'
    tot+=abc[x];



    my version is 1.7.0

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Your compiler should give you error messages on this line:
     int abc[15], tot=0, n_count=1;

    What is printed out on the console when you enter:
    javac -version

    I get: javac 1.7.0
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Quote Originally Posted by Norm View Post
    Your compiler should give you error messages on this line:
     int abc[15], tot=0, n_count=1;

    What is printed out on the console when you enter:
    javac -version

    I get: javac 1.7.0
    it's not giving me the said error I JUST NEED MORE HELP

  10. #10
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    B

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    @JanAlbertLam Please post the current version of the code you are working with so we can be sure we are working with the same code. I get errors on the line I posted and don't understand why you do not get errors there.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Quote Originally Posted by Norm View Post
    @JanAlbertLam Please post the current version of the code you are working with so we can be sure we are working with the same code. I get errors on the line I posted and don't understand why you do not get errors there.
     
    import java.io.*;
    public class MP2Num13
    {
    	public static void main (String Args[]) throws IOException
        	{
            	int[] abc = new int[15];
    		int tot=0, n_count=1;
            	BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
            	System.out.print("\n");
            	for(int x=0; x<15; x++)
                	{
                    	System.out.print("Index [" + x + "]: ");
                    	abc[x]=Integer.parseInt(br.readLine());
                	}
            	System.out.print("\nNumbers in the even indexes are: \n");
            	for(int x=0; x<15; x++)
            	{
                    	if(abc[x]%2==1)
                    	{
                        		System.out.print(abc[x] + " ");
                        		n_count++;
                    	}
                	}
            	System.out.print("\n \nThe total stored in the odd indexes is: ");
            	for(int x=0; x<15; x++)
                	{
                    	if(abc[x]%2==0)
                    	tot+=abc[x];
                	}
                	System.out.print(tot + "\n");
    	}
    }


    -this is my codes so far. and it's not returning the correct results. (

  13. #13
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    .

  14. #14
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Quote Originally Posted by javaiscool View Post
    Your errors are gone and it compiles; now you just have to solve the math problem. When I enter all 1's in your program, the sum prints as zero. So, you're not summing properly. Also, try inputting a "2" for each even number, and a "1" for each odd. You'll see that you're determining what's even/odd incorrectly in:

    System.out.print("\nNumbers in the even indexes are: \n");
            	for(int x=0; x<15; x++)
            	{
                    	if(abc[x]%2==1)
                    	{
                        		System.out.print(abc[x] + " ");
                        		n_count++;
                    	}
                	}

    A_NUMBER <modulus> 2 = 1 is for odds; <modulus> 0 is for evens. (Divide an even number by 2, zero remainder; odd number by 2, 1 remainder.)

    can you help me with syntax for that instance? HELPS WILL BE GREATLY APPRECIATED THANKS

  15. #15
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    .

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    syntax for that instance
    Can you explain what the problem is? Post the program's output and add some comments saying what is wrong and show what the output should be.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    C:\Users\Jan Albert\Desktop\MP - Java (021113)\MP2>java MP2Num13

    Index [0]: 1
    Index [1]: 2
    Index [2]: 3
    Index [3]: 4
    Index [4]: 5
    Index [5]: 6
    Index [6]: 7
    Index [7]: 8
    Index [8]: 9
    Index [9]: 0
    Index [10]: 1
    Index [11]: 2
    Index [12]: 3
    Index [13]: 4
    Index [14]: 5

    Numbers in the even indexes are:
    5 5 5 5 5 5 5 5

    The total stored in the odd indexes is: 35
    -for this instance, it should display this:
    Numbers in the even indexes are:
    1 3 5 7 9 1 3 5
    The total stored in the odd indexes is: 26

    C:\Users\Jan Albert\Desktop\MP - Java (021113)\MP2>java MP2Num13

    Index [0]: 5
    Index [1]: 2
    Index [2]: 4
    Index [3]: 7
    Index [4]: 3
    Index [5]: 1
    Index [6]: 8
    Index [7]: 6
    Index [8]: 3
    Index [9]: 1
    Index [10]: 4
    Index [11]: 2
    Index [12]: 5
    Index [13]: 9
    Index [14]: 0

    Numbers in the even indexes are:
    0 0 0 0 0 0 0 0

    The total stored in the odd indexes is: 0
    --here, it displays all zeros

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Thats strange. I get this when I execute the code. I've changed the code to take its input from a String vs console:
    Numbers in the even indexes are:
    1 3 5 7 9 1 3 5

    The total stored in the odd indexes is: 32
    If you have changed the code, you need to post the code you are working with.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Quote Originally Posted by Norm View Post
    Thats strange. I get this when I execute the code. I've changed the code to take its input from a String vs console:


    If you have changed the code, you need to post the code you are working with.
    import java.io.*;
    public class MP2Num13
    {
    	public static void main (String Args[]) throws IOException
        	{
            	int abc1=0;
    		int[] abc = new int[15];
    		int tot=0, n_count=1;
            	BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
            	System.out.print("\n");
            	for(int x=0; x<15; x++)
                	{
                    	System.out.print("Index [" + x + "]: ");
                    	abc1=Integer.parseInt(br.readLine());
                	}
            	System.out.print("\nNumbers in the even indexes are: \n");
            	for(int x=0; x<15; x++)
            	{
                    	if(x%2==0)
                    	{
                        		System.out.print(abc1 + " ");
                        		n_count++;
                    	}
                	}
            	System.out.print("\n \nThe total stored in the odd indexes is: ");
            	for(int x=0; x<15; x++)
                	{
                    	if(x%2==1)
                    	tot+=abc1;
                	}
                	System.out.print(tot + "\n");
    	}
    }

    -this is the code i'm currently running it with., pretty BIG favor if you can correct the codes for me., this is keeping me insane since feb 8 (

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Look at what value is being printed for the "even indexes" loop? Is that the value you want to see?
    Same with the next loop. What is in abc1?

    Look at the code in post#12 for getting the values from the array.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Quote Originally Posted by Norm View Post
    Look at what value is being printed for the "even indexes" loop? Is that the value you want to see?
    Same with the next loop. What is in abc1?

    Look at the code in post#12 for getting the values from the array.
    this is really wrecking my brain ( it's jumbling in my head

  22. #22
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    .

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Slow down and look at what the program outputs and then look at the code to see why.
    Look at the last number you entered and what the sum was:
    Index [14]: 0

    Numbers in the even indexes are:
    0 0 0 0 0 0 0 0

    The total stored in the odd indexes is: 0
    Or look at the last number you entered and what was printed:
    Index [14]: 5

    Numbers in the even indexes are:
    5 5 5 5 5 5 5 5
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Quote Originally Posted by Norm View Post
    Slow down and look at what the program outputs and then look at the code to see why.
    Look at the last number you entered and what the sum was:

    Or look at the last number you entered and what was printed:

    - how do i correct this? (

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

    Use the elements from the array like in post#12 and not the variable that the user's input is being read into.

    Look at how to use arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. need help with loops and codes..
    By akocnivek in forum Loops & Control Statements
    Replies: 1
    Last Post: October 19th, 2012, 03:02 AM
  2. Help me please with my codes
    By sara92 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 12th, 2012, 12:52 PM
  3. Looking for help on two codes:
    By captianjaax in forum What's Wrong With My Code?
    Replies: 24
    Last Post: September 21st, 2011, 12:49 AM
  4. Please Help What's Wrong with my Codes!
    By bertzki10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2011, 11:03 AM
  5. Accessing Arrays all At Once HELP
    By scottgilliland2003 in forum Member Introductions
    Replies: 0
    Last Post: December 13th, 2010, 10:25 AM