Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"copy"(or create refrences) selected elements from one array to another

 
Alexandra Fard
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a problem with my java class. The red marked code gets wrong. If I type in 4 children (2 above 4 and 2 younger than 4) the system.out.println write 4 older than 4. Hope someone knows what I mean and can explain for me what I'm doing wrong. I have not the code for the Child class in this message just the main class (the problem class).


/Alexandra
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not understanding the problem. You create an array of length 4, then print its length and get 4. That should be the expected case. The only problem with the lines in red and how they perform is the string text. You haven't counted how many children are above or under 4 yet, so you can't print that amount out. What they text should be is 'The number of children that might possibly be under 4.'

What you need to do is StopCoding (<- link) and use a pencil and paper to figure out how you should figure out the count of children above four, and the count of children under four. Visualize what you would do if the kids were in a line and you were able to speak to them just one at a time. How would you solve problem then? Right out the steps in your native tonque (not code) then make each step shorter, more precise, and probably the number of steps more. Once you are sure you could hand your instructions to a ten year old and expect him to be able to follow them and come to the correct conclusion, then you can start coding.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49783
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
You should always use the code button; I have been back to your post with the code button, and you can see how much better it looks .  Note: coloured text cannot be used in code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic