• Post Reply Bookmark Topic Watch Topic
  • New Topic

More trouble when using an arrays on learning website “Learneroo" (Java)  RSS feed

 
Alex Drummond
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Java challenge on this codecademy style learning website 'Learneroo' is:
Given an array of integers ar, find and print the largest sum of two consecutive integers. For example, given {1,2,4,5,2,6}, print 9, the sum of 4+5.

My attempt embedded inside this code:



The output is a crazy long number! Any help would be much appreciated.

Josh
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something wrong with that loop if it does not throw an Exception.
Go through your array with a pencil and paper and count the numbers of the elements you are adding. Then you can work out how far to iterate the array.

You are not adding successive elements, but the same element twice and 1. What is the very big number you are getting printed out?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you get the 0 from? How do you know there will not be negative elements?
 
Alex Drummond
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks again Campbell for your awesome comments! It's really appreciated. When you said:
Go through your array with a pencil and paper and count the numbers of the elements you are adding. Then you can work out how far to iterate the array.

The reason I was using ar.length to set the iteration was that my code has to work for a variety of different sample arrays they give me. Or were you getting at something else with that comment?

Also when you said:
You are not adding successive elements, but the same element twice and 1. What is the very big number you are getting printed out?
This was my fault for not explaining the challenge in full it explains that: "The people of Zumbania share a lot with their next-door neighbors, so wealth can be measured by looking at pairs of people. You would like to find the richest pair on each street in Zumbania." So they are looking for consecutive pairs of indices in the array hence my code:


You make a good point about the negative input. I actually have no idea what I could initiate it to in order to make sure I don't accidentally initialise it to something higher than the largest pair.

Most likely I'm missing some really basic logic here, I'm new to java and programming so I can't thank you enough for your patience and support.

The number its spitting out is:
1 2 4 5 2 6 9 1321141151521 for an array containing {1,2,4,5,2,6,9};
But the others sample arrays they give below the first one are recording nothing:
{-11,10,-6,2,3,-2,4,-3,6,5} my output for these comes back blank.
{10,20,10,40,50,30,70,100} my output for these comes back blank.

Josh
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pair = (ar[i] + (ar[i]+1));

this says to add together three things (although add the 2nd and third together, then add that to the first)
  • ar[i]
  • ar[i]
  • 1


  • I suspect what you WANT to do is add TWO things:
  • ar[i]
  • <the next element after ar[i]>


  • What is the next element after (say) ar[2]? is it ar[2] + 1? or is it something else?
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Using ar.length is correct. It is far better to use that than trying a number, because as you say that permits different lengths of arrays.
    But what is going to happen when you reach the last iteration of that loop?

    And Fred has given a hint about what I meant about adding. I shall try your code. I see you are using negative values; I would expect you would get the largest value still at 0 for such input.

    Did you know there is a smallest‑ever int number? You can find out about it in several places, e.g. the Integer class, Java® Language Specification (usually called JLS), your book, etc etc. It is absolutely impossible to do int arithmetic and get a value smaller than that smallest‑possible value.
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is something weird about that program. And it isn't the part you wrote. Put some print statements in the main() method and print every time you create a new array.
    Also change the System.out.print(...) call in line 13 to System.out.println(...) and see what happens.
     
    Stefan Evans
    Bartender
    Posts: 1837
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just as a matter of feedback, because I see you have made a few of these posts.
    The code examples you are following all seem to follow the same structure of

    - static main method that reads input from keyboard
    - run a calculation "doStuff:
    - print the output.

    I know you have marked the main method as "not your code", but personally, I find that interaction frustrating.
    Having to type the values each time, and then manually chack the answer against what is printed?
    We can be much smarter than that! It's called unit testing. And for java, we use JUnit.

    Basic steps:
    - change the doStuff method to return the answer rather than print it as part of the code
    - write a basic junit test case that invokes your doStuff() method.
    - run your test. You no longer have to type the values in each time!

    If you are using an IDE junit should already be available. But here is a quick example of what I am getting at:


    No, I haven't fixed the issue in your code. That is still in there :-)
    Maybe I'm putting too much on somebody just learning, but this approach (to me ) seems much better than using the template they have set up.

     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Possibly, since the template the provide appears incorrect or at best confusing, but another approach is to see if OP can't find out what is wrong with the template.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!