Results 1 to 19 of 19
Thread: How to make this looping work?
- 02-10-2013, 07:42 AM #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);
- 02-10-2013, 07:49 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-10-2013, 08:37 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 12
- Rep Power
- 0
- 02-10-2013, 08:55 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: How to make this looping work?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-10-2013, 09:02 AM #5
Member
- Join Date
- Feb 2013
- Posts
- 12
- Rep Power
- 0
Re: How to make this looping work?
i'm stuck with the error for this part of the code "abc[15]" and "abc[x]"Java 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"); } }
- 02-10-2013, 11:07 AM #6
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
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, 11:10 AM #7
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
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, 11:49 AM #8
Member
- Join Date
- Feb 2013
- Posts
- 12
- Rep Power
- 0
- 02-10-2013, 12:06 PM #9
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
Re: How to make this looping work?
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, 01:04 PM #10
Member
- Join Date
- Feb 2013
- Posts
- 12
- Rep Power
- 0
Re: How to make this looping work?
- 02-10-2013, 01:53 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-10-2013, 02:51 PM #12
Member
- Join Date
- Feb 2013
- Posts
- 12
- Rep Power
- 0
- 02-10-2013, 05:00 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: How to make this looping work?
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-11-2013, 02:31 PM #14
Member
- Join Date
- Feb 2013
- Posts
- 12
- Rep Power
- 0
- 02-11-2013, 02:48 PM #15
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
Re: How to make this looping work?
- 02-11-2013, 03:09 PM #16
Member
- Join Date
- Feb 2013
- Posts
- 12
- Rep Power
- 0
- 02-11-2013, 03:17 PM #17
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
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, 01:24 AM #18
Member
- Join Date
- Feb 2013
- Posts
- 12
- Rep Power
- 0
- 02-12-2013, 06:50 AM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Allow YOUR work to make a DIFFERENCE!!
By erictgroup in forum Jobs OfferedReplies: 0Last Post: 06-01-2011, 11:19 PM -
Make it work !
By PhQ in forum New To JavaReplies: 6Last Post: 09-20-2010, 08:22 AM -
How do i make this work What am i doing Wrong.
By Ramaan in forum New To JavaReplies: 2Last Post: 03-01-2010, 11:36 PM -
Make the Button Work
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 1Last Post: 02-27-2010, 10:52 AM -
Can't make JTable work -- please help!!
By cagalli83 in forum Advanced JavaReplies: 0Last Post: 02-13-2008, 09:31 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks