Results 1 to 19 of 19
Like Tree2Likes
  • 1 Post By Ronin
  • 1 Post By JosAH

Thread: How to make this looping work?

  1. #1
    JanAlbertLam is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Exclamation 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    JanAlbertLam is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to make this looping work?

    Quote Originally Posted by JosAH View Post
    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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to make this looping work?

    Quote Originally Posted by JanAlbertLam View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    JanAlbertLam is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Exclamation Re: How to make this looping work?

    Quote Originally Posted by JosAH View Post
    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;
    		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]"

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default 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. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default 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. #8
    JanAlbertLam is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to make this looping work?

    Quote Originally Posted by Ronin View Post
    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. #9
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: How to make this looping work?

    Quote Originally Posted by JanAlbertLam View Post
    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. #10
    JanAlbertLam is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to make this looping work?

    Quote Originally Posted by Ronin View Post
    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
    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

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    JanAlbertLam is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to make this looping work?

    Quote Originally Posted by JosAH View Post
    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. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to make this looping work?

    Quote Originally Posted by JanAlbertLam View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    JanAlbertLam is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to make this looping work?

    Quote Originally Posted by JosAH View Post
    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. #15
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: How to make this looping work?

    DarrylBurke likes this.

  16. #16
    JanAlbertLam is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to make this looping work?

    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. #17
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default 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. #18
    JanAlbertLam is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to make this looping work?

    Quote Originally Posted by Ronin View Post
    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.
    sample codes please :((((

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to make this looping work?

    Quote Originally Posted by JanAlbertLam View Post
    sample codes please :((((
    Have you solved your little array issue yet? This forum doesn't spoonfeed anyone.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Allow YOUR work to make a DIFFERENCE!!
    By erictgroup in forum Jobs Offered
    Replies: 0
    Last Post: 06-01-2011, 11:19 PM
  2. Make it work !
    By PhQ in forum New To Java
    Replies: 6
    Last Post: 09-20-2010, 08:22 AM
  3. How do i make this work What am i doing Wrong.
    By Ramaan in forum New To Java
    Replies: 2
    Last Post: 03-01-2010, 11:36 PM
  4. Make the Button Work
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 1
    Last Post: 02-27-2010, 10:52 AM
  5. Can't make JTable work -- please help!!
    By cagalli83 in forum Advanced Java
    Replies: 0
    Last Post: 02-13-2008, 09:31 AM

Posting Permissions

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