• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Sorting 10 Double Arrays

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everybody,

I'm trying to sort 10 inputted numbers (double precision) using the Array.sort() method. I'm actually kind of proud that I made it this far on my own, but I have ran into a wall.

I can get the 10 numbers inputted, but the output is ten 0.0s; now (and this how I know I am learning some things) I'm fairly certain that the variable number is not storing the numbers inputted by the user otherwise I wild be seeing the program work correctly.

So my question is why isn't number storing the inputs?





As always, thank you for the help.

Kris
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are prompting the user for numbers, but where are you adding them to the array? Adding values to an array is convoluted. I suggest you use a Collection such as an ArrayList which has an .add() method for putting those values in the collection. It's much simpler.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kris French wrote:I can get the 10 numbers inputted, but the output is ten 0.0s;


You should always post the code you are using. The code you posted won't output anything because you have an infinite loop* at line 8.

*Or possibly a very long running loop - it may end when i reaches Integer.MAXVALUE and is then incremented.
 
Marshal
Posts: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Infinite.
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops...thank you Joanne. There were a few changes made when I was researching how to accomplish this. I do have to use the Array.sort() method.

Here is where I was when I thought I was on the right track:

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your lines 9 and 10 ask the user to input a number...but you still are not doing anything with it. You have to tell java exactly where you want to save it, else it basically gets thrown away.
 
Campbell Ritchie
Marshal
Posts: 79928
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And how are you filling the array? If you don't put values into the array, it will contain ten copies of 0.0.

Don't write 10 twice. Second time round you want to use the length of the array. So write numbers.length. Call the array numbers rather than number.
Why are you using JOptionPane for keyboard input? It is very old‑fashioned practice.
Use a for‑each loop to display the array. Or if you have to use the Arrays class, it has a method for converting an array to a String. You might as well use that.
 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kris French

Kindly review your code



In the above for loop you are just parsing the number to a double. You should store it in the array and as sheriff says try using for each loop for displaying the contents. If you are sure that only 10 elements will be present use arrays else go for the flexible arraylist
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your help. I knew nothing was getting stored in the array, but I just needed to figure out why. After some digging, it looked like using the Scanner was the way to go. I imported the Scanner utility and added the appropriate line of code to the program, but then I kept getting array.index.outofbounds exception. I did some reading on why this happens and it turned out that in the first loop, I was using and when I switched to the program worked as it was intended.

Now my next challenge is to get the Number to count. for example; "Enter 10 numbers" then the program will print Number 1, Number 2, etc. Anyway, here is the program. Again thank you:



Kris
 
Campbell Ritchie
Marshal
Posts: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done working out the <= bit

I don't like <= and >=. They are not wrong, but < and > are much easier to understand, so try to use < and > whenever possible. There is a customary form for a for loop to traverse an array:-Use that form as a basis for all for loops with arrays. That always works and never throws an out of bounds Exception. For traversing an array where it is not necessary to reassign the elements, always use a for‑each loop, officially called an “enhanced for statement”.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please indent the code correctly. Having { and } on the same line makes code very difficult to read, even more so with all those spaces.
Don't use return. Much simpler to have the swap method like this
 
Marshal
Posts: 8953
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Parameters "int i, int j" are not necessary, otherwise I missed something.
 
Campbell Ritchie
Marshal
Posts: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, i and j are necessary. They represent the two indices to swap.
 
Liutauras Vilda
Marshal
Posts: 8953
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, didn't check full code.
Please omit my previous post.
 
Lookout! Runaway whale! Hide behind this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic