# WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• February 10th, 2013, 04:46 AM
JanAlbertLam
WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Code :

```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]"
• February 10th, 2013, 06:23 AM
Norm
Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Quote:

the error for this part of the code
Please copy the full text of the error messages and paste it here.
• February 10th, 2013, 06:35 AM
JanAlbertLam
Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Quote:

Originally Posted by Norm
Please copy the full text of the error messages and paste it here.

MP2Num13.java:13: error: incompatible types
^
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
• February 10th, 2013, 06:54 AM
Norm
Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?
You left off the other error messages on this line:
Code :

` 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
• February 10th, 2013, 08:00 AM
JanAlbertLam
Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Quote:

Originally Posted by Norm
You left off the other error messages on this line:
Code :

` int abc[15], tot=0, n_count=1;`

what exactly does that mean? :(

--- Update ---

i just tried., and it didn\'t work :((
• February 10th, 2013, 08:01 AM
Norm
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.
• February 10th, 2013, 08:22 AM
JanAlbertLam
Re: WHAT\'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Quote:

Originally Posted by Norm
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
• February 10th, 2013, 09:33 AM
Norm
Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Your compiler should give you error messages on this line:
Code :

` 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
• February 11th, 2013, 06:33 AM
JanAlbertLam
Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Quote:

Originally Posted by Norm
Your compiler should give you error messages on this line:
Code :

` 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 :((
• February 11th, 2013, 06:47 AM
javaiscool
Re: WHAT\'S THE RIGHT CODES FOR ACCESSING ARRAYS?
@JanAlbertLam

You\'re getting errors in:

Code :

`int abc[15], tot=0, n_count=1;`

because you\'re declaring variables of different types on one line.

To fix that line, break it up into separate lines, with the correct type for each variable.

Code :

```int[] abc = new int[15]; int tot=0, n_count=1;```
• February 11th, 2013, 06:51 AM
Norm
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.
• February 11th, 2013, 07:22 AM
JanAlbertLam
Re: WHAT\'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Quote:

Originally Posted by Norm
@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.

Code :

```  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. :(((
• February 11th, 2013, 07:32 AM
javaiscool
Re: WHAT\'S THE RIGHT CODES FOR ACCESSING ARRAYS?
.
• February 11th, 2013, 07:37 AM
JanAlbertLam
Re: WHAT\'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Quote:

Originally Posted by javaiscool
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:

Code :

```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
• February 11th, 2013, 07:39 AM
javaiscool
Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?
.
• February 11th, 2013, 07:43 AM
Norm
Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Quote:

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.
• February 11th, 2013, 08:21 AM
JanAlbertLam
Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Quote:

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

Quote:

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 :((
• February 11th, 2013, 08:26 AM
Norm
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:
Quote:

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.
• February 11th, 2013, 09:29 AM
JanAlbertLam
Re: WHAT\'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Quote:

Originally Posted by Norm
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.

Code :

```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 :(((
• February 11th, 2013, 09:34 AM
Norm
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.
• February 11th, 2013, 06:09 PM
JanAlbertLam
Re: WHAT\'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Quote:

Originally Posted by Norm
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 :((
• February 11th, 2013, 06:22 PM
javaiscool
Re: WHAT\'S THE RIGHT CODES FOR ACCESSING ARRAYS?
.
• February 11th, 2013, 06:30 PM
Norm
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:
Quote:

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:
Quote:

Index [14]: 5

Numbers in the even indexes are:
5 5 5 5 5 5 5 5
• February 11th, 2013, 08:41 PM
JanAlbertLam
Re: WHAT\'S THE RIGHT CODES FOR ACCESSING ARRAYS?
Quote:

Originally Posted by Norm
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? :(((
• February 11th, 2013, 08:48 PM
Norm
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)
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last