• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question on array and printing

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created code to print random numbers entered unsorted and then sorted. But it will not print the last line- the sorted values. I am not sure what i am doing wrong? Thanks!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've defined some methods to sort the array and print it, but you don't actually call them.
 
lowercase baba
Posts: 13089
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 main method ends without calling the sort or print methods... basically, once you finish the printing of the unsorted array, your program ends without ever running the sortArray method.
 
lauren kris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure i completely understand what i am doing wrong or how to fix it?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a program is like a recipe. imagine if i said to you "follow these direction EXACTLY to make a cake", and gave you this:

1) combine ingredients.
2) bake
3) take out of oven
4) you're done.

If you follow these directions EXACTLY, your cake will never be iced, even if i give you a recipe for the icing.

That's what you've done. if you follow the code in your main() method EXACTLY, and stop doing things when your main method ends, you never go into your code that sorts the array, or prints the array.

here's your code reformatted slightly:

what you need to to is call your methods. if i change my recipe from above to:

1) combine ingredients.
2) bake
3) take out of oven
4) make icing*
5) ice cake
6) you're done

*to make icing, do the following:
blah blah blah

you see how at step 4, you really leave the main set of instructions, and jump down to the sub-section?

Try changing your code from this:



to this:



instead of writing the code to print the array in my main, i'm using the code you wrote in the printArray method by calling the method. I can pass this method ANY array (technically, only an array that contains "int"s). i can call that method a hundred or a thousand times, and i only have to write it once...

let me know if that helps...
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lauren,

Is there any particular need for writing the sort method yourself?
You can use the method Arrays.sort from java.util.Arrays.

Still if you need to write the method yourself, I think the algorithm is not right. You can have a look at Sorting algorithms for help.

All the Best !
 
lauren kris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred, your analogy was helpful! Thank you!
I tried changing the code like you said but it does not print the array anymore. And my sorted values are mixed up too.
Here is what I get for output:

Enter the size of the array (3 to 10)
3
Enter 3 values:
Enter value for element 0:
4534
Enter value for element 1:
3
Enter value for element 2:
-768
The unsorted values...
The sorted values...3
The sorted values...-768


When I should be getting
Unsorted values: 4534 3 -768
Sorted values: -768 3 4534

Ive been trying to play around with the code but cant seem to get it right?!




Sachin, I am required to sort through the numbers "manually" per say and look at ones next to each other and compare and then sort in order, if that makes sense. i wish i could just use arrays.sort!!!
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let's back up a little, and not worry about sorting just now. let's just see if we can get the printArray method to work (i didn't actually test the code with my suggested change... it was said more to try and spark the idea in your head).

Here is your 'printArray' method, and a main that is as simple as it can get. I did change the println statement just a little. if you want the method to be generic, it shouldn't state 'the sorted array is', because you might not be passing it a sorted array.


Try running this and see what happens... you'll find a slight problem with the output. You need to remember that an array of size 5 has it's elements indexed 0-4...

See if you can figure out the problem here, and make changes in your code to get it to properly print an array you pass in. once you do that, we can look at your sort...
 
lauren kris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont have specific numbers for the array (ie: 1, 2, 3, 4) I have to ask the user to enter how many numbers they want to list, and then they list them.

So i have the array:


And then ask them for each number individually :


So I am not really sure how this works since I do not have set numbers like you showed...
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The advice fred is giving, and I concur, is to not worry about how you're getting the array values just yet. First worry about the printArray() method, make sure that its working correctly before you move on to solving the problem of creating a user defined array. If the printArray() method doesn't work with hard coded values, it won't work with values taken from the user input. If the printArray() method does work, you don't have to worry about it any more, you can just use it for any array you want to print to the console.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what we're doing is called 'unit testing'. we want to make sure our printArray() method is ROCK SOLID. the idea is you can write another dummy program that builds an array somehow... it doesn't matter how... and you use that to test your printArray() method to within an inch of its life. once you know it is ready, as Garrett said, you can use it anywhere you want.

write a completely new program, something like this;



The idea here is you have your print array method, and your main() method here will do NOTHING but test your printArray method. Note that one of my cases won't work - that's ok, you want things to fail when they should fail. (ok, not the BEST example, since it won't even compile...).

anyway, once you KNOW your printArray() method works, you can drop it (cut-n-paste please, don't try to do it by hand) in your REAL program, and never worry about it again.

does that make sense?
[ July 04, 2008: Message edited by: fred rosenberger ]
 
lauren kris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I got it to work like you described... is this right?

 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i would not have the 'printArray()' method print "the unsorted values". the method should do exactly, and only, what the name says - print an array. Why? because it doesn't care (or even know) if the array is sorted or not. it doesn't matter. so, i'd take that System.out.println out of the printArray() method, and put in in your main just before i CALL the printArray() method.

aside from that, you tell me... Does it print your array?
 
lauren kris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes!
printArray(array); does print the array out
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. now that you're sure your printArray() method works, you can drop it into your actual program any place you want to print an array.

I would now write a test class to sort your array. I would suggest in your test class, you NOT manually input them each time using the scanner, but simply hard-code them like in my example. test 3-4 different arrays. have at least one be already sorted so that you can be sure your method won't unsort them.

this is called iterative programming. you take little, teeny tiny steps at at time. you write VERY little code each time (sometimes as little as one line) before you compile, run, and test it. that way you are confident at each step things work.
 
lauren kris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So i should write a new class (ie: TestClass) and use the same code to create the array and try sorting it and printing it there?
 
reply
    Bookmark Topic Watch Topic
  • New Topic