views:

131

answers:

3
package hw3;

public class Main {
    public static void main(String[] args) {
        final int NumberOfElements = 1000;
        int[] num = new int[NumberOfElements];
        int var = 0;
        //create input
        java.util.Scanner input = new java.util.Scanner(System.in);
        for (int i = 0; i < NumberOfElements; i++) {
            System.out.print("Enter any positive number or enter 0 to stop: ");
            num[i] = input.nextInt();
            var++; 
            if (num[i] == 0)

                break;

            }
             Arrays.sort( num, 0, var);
             int i;
             for (i = 0; i < var; i++) {
             System.out.print("   " + num[i]);


        }
    }
}

Write a Java program reading a sequence of positive integers entered one per line. The program stops reading when integer ‘0’ is entered. The program will sort and output them into an ascending numerical order.For example: 5 1 7 12 36 8 0 Output: 1 5 7 8 12 36

A: 

Here your application is reading int, you can check that the last one is != from 0. And break the loop.
If you don't like using break, you can still add a condition to your for.

For sorting an array, there is the Arrays.sort() solution.

And to print them you'll need another loop, but beware as you can have less than 1000 items in your array you can't simply print the whole array.
You'll need to find a way to count the number of elements you added in the array.


After your edit:

Good your application seems to work here is what I got :

Mac-Makkhdyn:~ Makkhdyn$ java hw3.Main
Enter any positive number or enter 0 to stop: 1
Enter any positive number or enter 0 to stop: 2
Enter any positive number or enter 0 to stop: 3
Enter any positive number or enter 0 to stop: 4
Enter any positive number or enter 0 to stop: 5
Enter any positive number or enter 0 to stop: 0
Mac-Makkhdyn:~ Makkhdyn$

The 0 you have must be from somewhere else. Isn't your actual code slightly different from the one you posted here ?


Resources :

Colin Hebert
hm. it seems to work..
CuriousStudent
System.out.print("Accending order number : "); Arrays.sort(num); for(i = 0;i < num.length;i++){ System.out.print(" " + num[i]); }
CuriousStudent
As I said, you can't print the whole array, because there is a (huge) possibility that it isn't entirely filled.
Colin Hebert
hm...so i have to somehow change the array to the number of times the user inputted a number?
CuriousStudent
Or as I said above, you could count the number of elements you put in your array.
Colin Hebert
@CuriousStudent, you'll need to "remember" how many numbers were entered. :)
st0le
sorry reason why I can't do that is because I do not know how. Is there a link you can point me to? My prof didn't teach it to us he just gave us the assignment and told us to figure it out somehow.
CuriousStudent
@st0le this program is for an user to input the data so I can't predict how many numbers he/she is going to enter
CuriousStudent
i mean i can do something like this System.out.print ("How many digits are you going to enter? "); int x = input.nextInt(); int[] num = new int [x]; but i do not think that is the most efficient way.
CuriousStudent
You can declare your own int as a counter, and increment it by one each time you get an answer.
Colin Hebert
Okay I will attempt to do that. thank you
CuriousStudent
Another thing, you should really get your "sorting and printing" part out of the for loop.
Colin Hebert
am i on the right track? int answers; for (answers = 0; answers == i; answers++){ System.out.print ("the number of answers entered is " +answers);
CuriousStudent
@CuriousStudent, i actually meant to store the value of `i` in another variable when you break out of the loop. Colin's Suggestion works too :)
st0le
@st0le I dont know how; i'm basically trying to do this assignment by reading the book and online :( ....we only had 3 lectures so far
CuriousStudent
@CuriousStudent, you don't need to. But since you're a beginner, i'd suggest you do it as simple as possible, make another variable `count` initialize it with the size of the array. now, in the loop, before the `break` statement, add `count = i`...finally, display all values of array between `0..count` :) hope it helps
st0le
@CuriousStudent, Did you learn about how you can declare variables ? In this case you can create a new one, before the loop. And you add 1 to this variable (`var = var+1`) each time you add a number to your array. This way you know how many elements there is.
Colin Hebert
@CuriousStudent, i spotted a problem, that'll occur...since your Array is of size `1000`, its initialised to `0`...Now, `Arrays.sort()` will sort the `entire` array. (ie all 1000 elements) even though the user might have entered just 10 values...the array will end up with a bunch of 0's in the front.
st0le
yeah lol 0 0 0 0 0 3 2 1...i get something like that. okay i'll try both ways @colin @st0le
CuriousStudent
okay cool the counting is working now; to the next issue lol the sorting
CuriousStudent
@CuriousStudent, use `Arrays.sort( arr[], from, to)` :)
st0le
LOL I dont know how to use it like that. Arrays.sort( arr[], [0], [var]); ??http://www.roseindia.net/java/beginners/array.shtml doesn't teach that haha.
CuriousStudent
in the book I see you can do swapping but boy is that complicated
CuriousStudent
@st0le, why "from", "to" ? Just Arrays.sort(array) is enough. You only have to get the n last elements later.
Colin Hebert
@colin but when i do that it only prints out zeros
CuriousStudent
When you do your loop for printing elements. Instead of starting from 0, start from the `array.length - number of addedElements` and go to the end of the array ;)
Colin Hebert
run:Enter any positive number or enter 0 to stop: 7Enter any positive number or enter 0 to stop: 4Enter any positive number or enter 0 to stop: 2Enter any positive number or enter 0 to stop: 0 0 2 4 7 BUILD SUCCESSFUL (total time: 11 seconds)How do I take out that zero?
CuriousStudent
In the example that the prof gave us he didn't have the zero in the output :(
CuriousStudent
@Colin, not really...what if the input contains negative values? edit: i just realized, op's states it's all positive....cool, then it'll work!! :)
st0le
A: 

num[i] holds the last number you are reading. So you have to compare it against 0 and if it is 0, you have to end the loop. Read about branching statements to find out how to do that.

It is probably also good for you to read about control flow statements in general.

Felix Kling
weird now i copy paste it and it seems to work...okay i guess now its on to the next problem which is adding the arrays.sort() without printing out 1000 characters....
CuriousStudent
+1  A: 
Nivas
Wow Thank you I didn't even look at the bottom of the page till now. Thanks!!
CuriousStudent
"If you don't want this, you should check the input and add it to the array only if it is not 0. "how do I do that is it something like !=0...?
CuriousStudent
you can accept the answer if it works for you :-) (In SO, when you get an answer for your question, and you are satisfied with it, you "Accept" it by clicking the tick mark near the answer. See the ***"How do I ask questions here?"*** section in http://stackoverflow.com/faq)
Nivas
I have added an example, see the code in the first for loop: `if (n == 0) break; else num[i] = n;`
Nivas
+1 Nicely answered homework question.
st0le
if (num[i] != 0) continue; else break;still includes the zero
CuriousStudent
okay i signed up for an account just to accept it
CuriousStudent
:-) but make sure this is what you need before you accept it. Take your time.
Nivas
No not `if (num[i] != 0)`. You should get it in a temporary variable first, check if it is `0` and add to array only if it is not. See the for loop in my answer (just before step 2) completely
Nivas
i dont think i am suppose to include the zero....do I have to drastically change the code for it to work ? oh yeah i should probably mention that he said we should be able to do it with 2 for and 1 if statement
CuriousStudent
@CuriousStudent, perhaps you did not understand what I said. The code I have given already **does not include** the zero. Try reading the answer completely, and understanding the code I have given. The code to ignore the zeros is in the first for loop of my answer
Nivas
Oh I understand it works! okay Now on to problem 2 of my homework lol. It's due in 6 hours I guess i'm not sleeping tonight considering its 2:24am already.
CuriousStudent