File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes question on array and printing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question on array and printing " Watch "question on array and printing " New topic
Author

question on array and printing

lauren kris
Greenhorn

Joined: Jul 01, 2008
Posts: 8
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!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You've defined some methods to sort the array and print it, but you don't actually call them.


[Jess in Action][AskingGoodQuestions]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
lauren kris
Greenhorn

Joined: Jul 01, 2008
Posts: 8
I am not sure i completely understand what i am doing wrong or how to fix it?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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...
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
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 !


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
lauren kris
Greenhorn

Joined: Jul 01, 2008
Posts: 8
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
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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

Joined: Jul 01, 2008
Posts: 8
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...
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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

Joined: Jul 01, 2008
Posts: 8
I think I got it to work like you described... is this right?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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

Joined: Jul 01, 2008
Posts: 8
Yes!
printArray(array); does print the array out
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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

Joined: Jul 01, 2008
Posts: 8
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?
 
Don't get me started about those stupid light bulbs.
 
subject: question on array and printing
 
Similar Threads
How to display a sorted list after sorting.
I can NOT compile first class below
How would i break this down into seperate metods?
Sorting an array
String Input and Bubble Sort