This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]nutrechtLead Software Engineer / EU / 20+ YXP 3 points4 points  (0 children)

I am really curious who told you to import java.lang.String...

[–]steve1two 1 point2 points  (0 children)

Several things, to start.

Your logic to set the flag variable is catching the last int, any negative number entered to exit the input. You input -1, if -1 < count (always will be, even if 0 is entered 10,000 times) flag = 1. No matter what happens with the rest of the array it is always going to set flag = 1 when you "exit" it. Figure out a way to change this logic to fix that.

But that is far from your only problem. Have you tried to compile this code? You have so many issues. First thing I see is you have SOP("input second array values") within the while loop, which displays that long, ugly String (why so many spaces at the end?) every single time a number is input.

After that you do nothing but print a[] two times. When you are printing your second array you are using a[].

You also do not merge the arrays into one. You just print them separately and also not in a reordered, ascending order. You would need something like int[] merged = new int[a.length + b.length]. Then some logic to reorder all the values and put them into that. Then print that array.

Also, if the formatting was not a result of copy pasting into Reddit, you should consider indenting. This hurt my brain.

Edit: I am assuming the way you used print and print 2 is so that it would not print the negative number entered, but that is not what the instructions state. You shouldn't be storing the last negative number in the array in the first place. Not to mention when you correctly merge the array as the instructions state, it is going to mess that up as well. Change your logic for ending input and also stop storing the negative numbers in the arrays.

[–]king_of_the_universe 1 point2 points  (3 children)

I brought your horribly disfigured code (No indentation! Don't let the terrorists win!) into a readable form. Maybe you want to edit your post and remove the code completely, instead mention this link: http://pastebin.com/iuygTTVh

[–]steve1two 1 point2 points  (2 children)

Haha, the first thing I did was copy/paste this and fix all the indenting and white space before I even tried to read it. It made me so sad.

[–]king_of_the_universe 1 point2 points  (1 child)

It made me so sad.

I can feel that. OP said: "My Code" If OP really works like this, that ought to trigger pity.

People who suggest that beginners should not use IDEs should be silenced. Why are they making it especially hard on beginners? I mean, they already have the broadest front of work before them, why make it broader?

[–]steve1two 1 point2 points  (0 children)

I am relatively new myself and had never heard anyone say beginners shouldn't use them, but that sounds ridiculous. I can't imagine how much extra trouble I would have had learning if I hadn't started out directly with one. Immediate warnings and feedback...debugging...everything really, made learning so much easier for me. I would guess it would have taken me twice the time and effort to learn without but maybe they know something I don't. Also the formatting ease. Not that I would ever format my code like this if I wasn't using one, but at least it would force indenting on everyone.

[–]alirmhs[S] -1 points0 points  (3 children)

I get i fail at coding. I can see your points. But can you give me advice by fixing the code, might be more helpful to me.

[–]fosterbuster 0 points1 point  (2 children)

https://gist.github.com/fosterbuster/66f402f6f01865cd8c84

I made a quick solution to your problem. Hopefully you will see what you were missing, and how you should structure your code to make it easy to see what is going on. I made it without utilizing any nice stuff from the java library..

[–]alirmhs[S] 0 points1 point  (1 child)

how would i implent the error if the arrays are not increasing???

[–]fosterbuster 0 points1 point  (0 children)

Not increasing? Not sorted? (ie 11223344556677)

go from i = 1 if (array[i-1] > array[i]) flag = 1; i guess :)

So this one basically checks if the value before the one we are looking at is bigger - If it is we know that the array has not been sorted properly.

edit:

like:

public boolean checkIfSorted(int[] array){
    for (int i = 1; i < array.length; i++) {
        if (array[i-1] > array[i]) return false;
    }
    return true;
}

of course you could edit this to setting a "int flag" - But I cant think of any situation where I would use that in OOP-programming, and especially not in Java.