• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array sorting(evens and odds)  RSS feed

 
Malcolm Bibins
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I want to do is write an application that reads in 10 non-negative intergers, and assigns numbers to either 'oddArray' or 'evenArray'. I also want to display each array's elements and their sum. (System.out....evenArray: 2,4,6,...etc & Sum =" ") As well as displaying a single array, in event that all of the numbers entered are entirely even or odd.

Here is what I have so far, and I am very stumped.



Heres what the output produces:
[I@7afc02c8
Even numbers consist of:
2
2
2
2
2
2
2
2
2
2
The sum of these numbers is: 2


Thanks for your time, guys.
 
Shashank Gollapudi
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Malcolm,

Did you really intend to read a number single time and insert that into an array for 10 times ?

Secondly, for getting the correct value for your sum use the line

evenSum = evenSum + evenArray[num]; on line number 35,


 
Malcolm Bibins
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Shashank Gollapudi,

Thanks for the sum bit.

I'm trying to read in the 10 intergers from lines 14-15 by making it loop 10 times.

Say that I input these following intergers: 2 5 8 10 7 15 12 4 6 9

I'd want all of the numbers to be displayed, whether it be both arrays, or just the single even/odd array(all even numbers/all odd).
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem, as Shashank Gollapudi has already hinted at, is your input of the integers is outside of the loop so you will only ever read the first integer.
 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also the reason you are getting a garbage value in the beginning is because you are trying to print an array using System.out.print. To print an array you need a loop.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vineeth Menon wrote:To print an array you need a loop.

Or use:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Your problem, as Shashank Gollapudi has already hinted at, is your input of the integers is outside of the loop so you will only ever read the first integer.

And just to add to Tony and Shashank's good advice, it's a really good idea to break down your problem into smaller pieces and put them in different methods. Whenever one of us sees a class that has all its code in main(), we know that sooner or later the coder is likely to run into problems.

In this particular case, I'd suggest a:
public static int getNegativeNumber() { ...
method.

Or, probably even better:
public static int getNegativeNumber(Scanner s) { ...

HIH

Winston
 
Malcolm Bibins
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you guys are saying. Yet, I'm still uncertain as how to change things around, so that the first integer isn't displayed repeatedly.

Am I moving line 15?
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I wouldn't advise that. If you're at the point where you don't understand your code, then I (and a lot of others here) would advise you to toss it out and start again.

Only this time, don't try to write the whole thing in one go. Just write one part at a time. It looks to me like the first thing you have to do is to get ten integers from the user. So just write that to start with, and get it working right. You can put the integers into the arrays later -- that would be your second step.
 
Stuie Clarky
Ranch Hand
Posts: 98
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it just me, or are there no braces enclosing the while loop in the code fragment from the OP?

Edit:
Got it working and although there are no braces, I agree with Winston that getting code out of main is not only good practice, but you can make the code clearer and neater. My rough work made it three methods:

Getting and storing the input numbers in the correct array
Printing the array contents
Printing the sum of arrays

I don't think its an optimal solution, but its neat and gets the correct results.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are right. Of course with the really poor formatting who knows where the braces are?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!