-
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]"
-
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.
-
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
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
-
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
-
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 :((
-
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.
-
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
-
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
-
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 :((
-
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;
-
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.
-
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. :(((
-
Re: WHAT\'S THE RIGHT CODES FOR ACCESSING ARRAYS?
-
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
-
Re: WHAT'S THE RIGHT CODES FOR ACCESSING ARRAYS?
-
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.
-
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 :((
-
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.
-
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 :(((
-
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.
-
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 :((
-
Re: WHAT\'S THE RIGHT CODES FOR ACCESSING ARRAYS?
-
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
-
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? :(((
-
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)