# Thread: How to make this looping work?

1. Member
Join Date
Feb 2013
Posts
12
Rep Power
0

## How to make this looping work?

somebody convert this code to Java code (this is from Dev C++ codes)

Java 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);```

2. ## 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

3. Member
Join Date
Feb 2013
Posts
12
Rep Power
0

## Re: How to make this looping work?

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

4. ## Re: How to make this looping work?

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

5. Member
Join Date
Feb 2013
Posts
12
Rep Power
0

## Re: How to make this looping work?

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
Java Code:
```import java.io.*;
public class MP2Num13
{
public static void main (String Args[]) throws IOException
{
int abc[15], tot=0, n_count=1;
System.out.print("\n");
for(int x=0; x<15; x++)
{
System.out.print("Index [" + x + "]: ");
}
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]"

6. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## 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.

7. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## 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.

8. Member
Join Date
Feb 2013
Posts
12
Rep Power
0

## Re: How to make this looping work?

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]" :(((

9. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## Re: How to make this looping work?

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.

10. Member
Join Date
Feb 2013
Posts
12
Rep Power
0

## Re: How to make this looping work?

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

11. ## 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

12. Member
Join Date
Feb 2013
Posts
12
Rep Power
0

## Re: How to make this looping work?

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?

13. ## Re: How to make this looping work?

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

14. Member
Join Date
Feb 2013
Posts
12
Rep Power
0

## Re: How to make this looping work?

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

15. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

16. Member
Join Date
Feb 2013
Posts
12
Rep Power
0

## Re: How to make this looping work?

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

17. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## 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.

18. Member
Join Date
Feb 2013
Posts
12
Rep Power
0

## Re: How to make this looping work?

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.

19. ## Re: How to make this looping work?

Originally Posted by JanAlbertLam
Have you solved your little array issue yet? This forum doesn't spoonfeed anyone.

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•