# How to make this looping work?

• 02-10-2013, 08:42 AM
JanAlbertLam
How to make this looping work?
somebody convert this code to Java code (this is from Dev C++ codes)

Code:

```    int abc[15], tot=0, n_count=1;     p("\n");         for(int x=0; x<15; x++)     {             p("Index [%i]: ", x);             s("%i", &abc[x]);     }     p("\nNumbers in the even indexes are: \n");         for(int x=0; x<15; x++)     {             if(abc[x]%2==1)             {                     p("%i ", abc[x]);                     n_count++;             }     }     p("\n \nThe total stored in the odd indexes is: ");         for(int x=0; x<15; x++)     {             if(abc[x]%2==0)             {                     tot+=abc[x];             }     }     p("%i", tot);```
• 02-10-2013, 08:49 AM
JosAH
Re: How to make this looping work?
That snippet of code doesn't do anything with even indexes (0, 2, 4, etc) or odd indexes (1, 3, 5, etc); we can only guess what the functions s( ... ) and p( ... ) do (scanning and printing?); the loops do work.

kind regards,

Jos
• 02-10-2013, 09:37 AM
JanAlbertLam
Re: How to make this looping work?
Quote:

Originally Posted by JosAH
That snippet of code doesn't do anything with even indexes (0, 2, 4, etc) or odd indexes (1, 3, 5, etc); we can only guess what the functions s( ... ) and p( ... ) do (scanning and printing?); the loops do work.

kind regards,

Jos

this program works with Dev C++
what i'm trying to do is make this work in Java.

HELP ANYONE :(((((((
• 02-10-2013, 09:55 AM
JosAH
Re: How to make this looping work?
Quote:

Originally Posted by JanAlbertLam
this program works with Dev C++
what i'm trying to do is make this work in Java.

HELP ANYONE :(((((((

Help with what? Write the code for you? That is not how this forum works; you go first and if you're stuck, ask specific questions and we'll try to help you out.

kind regards,

Jos
• 02-10-2013, 10:02 AM
JanAlbertLam
Re: How to make this looping work?
Quote:

Originally Posted by JosAH
Help with what? Write the code for you? That is not how this forum works; you go first and if you're stuck, ask specific questions and we'll try to help you out.

kind regards,

Jos

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]"
• 02-10-2013, 12:07 PM
Ronin
Re: How to make this looping work?
Hi JanAlbertLam,

You still have not explained your problem. What is it that the code does/doesn't do which differs from what you want it to do?

Regards.
• 02-10-2013, 12:10 PM
Ronin
Re: How to make this looping work?
Looking over your code again and I don't like the print out on line 19. It looks as though this is making a call to a method which you have either not shown or defined.

Regards.
• 02-10-2013, 12:49 PM
JanAlbertLam
Re: How to make this looping work?
Quote:

Originally Posted by Ronin
Looking over your code again and I don't like the print out on line 19. It looks as though this is making a call to a method which you have either not shown or defined.

Regards.

Ronin,

kindly refer to my first post
when i had that code in C, it's working
but when i copy it a change them to Java's syntaxes, there's an error on "abc[15]" and "abc[x]" :(((
• 02-10-2013, 01:06 PM
Ronin
Re: How to make this looping work?
Quote:

Originally Posted by JanAlbertLam
there's an error on "abc[15]" and "abc[x]" :(((

I did see the original comment, but could have done with some specifiics about what error is thrown. Saying it doesn't work does not give us much to go on.
Looking back though I have spotted something. The array abc has not been initialized which is something you could get away with in C but Java is not as forgiving.

Regards.
• 02-10-2013, 02:04 PM
JanAlbertLam
Re: How to make this looping work?
Quote:

Originally Posted by Ronin
I did see the original comment, but could have done with some specifiics about what error is thrown. Saying it doesn't work does not give us much to go on.
Looking back though I have spotted something. The array abc has not been initialized which is something you could get away with in C but Java is not as forgiving.

Regards.

Here's the error thrown:

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
• 02-10-2013, 02:53 PM
JosAH
Re: How to make this looping work?
You can't declare and define arrays in the way it is done in C or C++; those are different languages so they do (or can do) things in a different way; neither one is a bug. Because your definition of the array failed the compiler complains about further use of it.

kind regards,

Jos
• 02-10-2013, 03:51 PM
JanAlbertLam
Re: How to make this looping work?
Quote:

Originally Posted by JosAH
You can't declare and define arrays in the way it is done in C or C++; those are different languages so they do (or can do) things in a different way; neither one is a bug. Because your definition of the array failed the compiler complains about further use of it.

kind regards,

Jos

so how am i suppose to do the same in Java? what is its equivalent syntax in java?
• 02-10-2013, 06:00 PM
JosAH
Re: How to make this looping work?
Quote:

Originally Posted by JanAlbertLam
so how am i suppose to do the same in Java? what is its equivalent syntax in java?

Are you serious? You're telling that you don't know how to create an array in Java? That's like trying to translate an English poem in Chinese while you don't speak Chinese and ask others to translate single sentences for you ... study some elementary Java tutorials.

kind regards,

Jos
• 02-11-2013, 03:31 PM
JanAlbertLam
Re: How to make this looping work?
Quote:

Originally Posted by JosAH
Are you serious? You're telling that you don't know how to create an array in Java? That's like trying to translate an English poem in Chinese while you don't speak Chinese and ask others to translate single sentences for you ... study some elementary Java tutorials.

kind regards,

Jos

i'm dead serious. i have the algo in mind but i can't convert Java codes :(((( i'm really just a newbie :(((
• 02-11-2013, 03:48 PM
Ronin
Re: How to make this looping work?
• 02-11-2013, 04:09 PM
JanAlbertLam
Re: How to make this looping work?
Quote:

Originally Posted by Ronin

i've gone into that already., it shows an defined value for the array., but what i want is how it works if user will be the one to input :(((
• 02-11-2013, 04:17 PM
Ronin
Re: How to make this looping work?
You will need to read up on the Scanner class which will accept user input from the keyboard. You could then assign the values read in to variables which are in turn placed in the array.

Regards.
• 02-12-2013, 02:24 AM
JanAlbertLam
Re: How to make this looping work?
Quote:

Originally Posted by Ronin
You will need to read up on the Scanner class which will accept user input from the keyboard. You could then assign the values read in to variables which are in turn placed in the array.

Regards.