Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Find the the longest decreasing row of numbers in a vector

1. ## Find the the longest decreasing row of numbers in a vector

Just started to work at this problem.

Any suggestions?

```
public class LongestDecreasing {
public static void main(String[] args) {
int N = args.length;
int[] a = new int[N];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(args[i]);
}

// solve problem here

System.out.println(...);
}
}```

--- Update ---

For a vector of integers a,....., a[N-1] define the number of integers from i to j as sequence of a[i], a[i+1],...,a[j] when i<=j  Reply With Quote

3. ## Re: Find the the longest decreasing row of numbers in a vector

You can find help with the code tags on the Announcements page

"Any suggestions?"
Sure, start by explaining the problem you are having, perhaps what the code is supposed to do vs what it actually does. Then ask a question which you think may help you to understand how to proceed  Reply With Quote

4. ## The Following User Says Thank You to jps For This Useful Post:

einar123 (September 22nd, 2013)

5. ## Re: Find the the longest decreasing row of numbers in a vector

Excuse me. Code tags? I did highlight. Do you mean like explanation in the code itself? Meesa don't understand this.

My problem is I dont know where start really. Not asking for soloution, just guidance. Somebody mentioned temp, I don't even know what that is or what it has to do with this code, whatever...

The code is supposed to find the longest decreasing numbers in a vector (>=) given from a command line, count them and print that number.  Reply With Quote

6. ## Re: Find the the longest decreasing row of numbers in a vector

[java=highlight] is not a valid code tag. I edited the post and fixed it for you. See the link for proper tags.

Start with "How would you find the answer without a computer?" ... if someone were to hand you a sheet of paper with the numbers, what steps would you take to find the answer?  Reply With Quote

7. ## The Following User Says Thank You to jps For This Useful Post:

einar123 (September 22nd, 2013)

8. ## Re: Find the the longest decreasing row of numbers in a vector

I think I'm almost there.

How can I fix this code so that it works?

```public class VectorDecreasing {
public static void main(String[] args) {
int N = args.length;
int[] a = new int[N];
int acounter=1;
int bcounter=1;
for (int i = 1; i <= a.length; i++) {
a[i] = Integer.parseInt(args);
if (a[i-1]>=a[i])
{
bcounter++;
}
else
{
bcounter=1;
}

if( bcounter>acounter)
{
bcounter=acounter;
}
{
System.out.print(bcounter);
}}
}
}```

Error:
111 Exception in thread "main" java long ArreyIndexOutofBoundsExeption 4 at VectorDecreasing.main(VectorDecreasing.java:8)  Reply With Quote

9. ## Re: Find the the longest decreasing row of numbers in a vector

for (int i = 1; i <= a.length; i++)

<= a.length : get rid of the '='.  Reply With Quote

10. ## Re: Find the the longest decreasing row of numbers in a vector

args should be = i  Reply With Quote

11. ## Re: Find the the longest decreasing row of numbers in a vector

Hello.
Do you know about arrays in java?
If I have an array say 'a' of ten integers. Then these elements are accessible as a, a, ... , a.
In general if an array has n elements these elements are accessed as , , ..., [n-1].
But in your program you are doing a[n] which is incorrect. Hence the exception.

Syed.  Reply With Quote

12. ## The Following User Says Thank You to syedbhai For This Useful Post:

einar123 (September 23rd, 2013)

13. ## Re: Find the the longest decreasing row of numbers in a vector

GregBrannon. The System.out print...I'm just not cutting it  Reply With Quote

14. ## Re: Find the the longest decreasing row of numbers in a vector Originally Posted by einar123 GregBrannon. The System.out print...I'm just not cutting it
I have no idea what this means. Don't be timid about adding a few extra words that actually explain what you're trying to say.  Reply With Quote

15. ## The Following User Says Thank You to GregBrannon For This Useful Post:

einar123 (September 23rd, 2013)

16. ## Re: Find the the longest decreasing row of numbers in a vector

hahahah I'l admit that I'm forum timid  Reply With Quote

17. ## Re: Find the the longest decreasing row of numbers in a vector Originally Posted by einar123 Error:
111 Exception in thread "main" java long ArreyIndexOutofBoundsExeption 4 at VectorDecreasing.main(VectorDecreasing.java:8)
This tutorial will explain in detail why you get the error, and help explain what the others are saying  Reply With Quote

18. ## The Following User Says Thank You to jps For This Useful Post:

einar123 (September 23rd, 2013)

19. ## Re: Find the the longest decreasing row of numbers in a vector

My problem now is that I dont ,,take'' the number in a0. But I feel like I'll have to have i=1 because if I have i=0 then I'm out of bounds....but then again I'm out of bounds at the other end...  Reply With Quote

20. ## Re: Find the the longest decreasing row of numbers in a vector Originally Posted by einar123 if I have i=0 then I'm out of bounds....
The index for an array starts with 0. See my previous post and visit the tutorial  Reply With Quote

21. ## The Following User Says Thank You to jps For This Useful Post:

einar123 (September 23rd, 2013)

22. ## Re: Find the the longest decreasing row of numbers in a vector

Ok. Would you give correct for doing it this way?

```
public class LongestDecreasing {
public static void main(String[] args) {
int N = args.length;
int[] a = new int[N];
int bcounter=1;
int acounter=1;
for (int i = 0; i < a.length-1; i++) {
a[i] = Integer.parseInt(args[i]);

if(a[i]>=a[i+1])
{
bcounter++;
}
else
{
acounter++;
}

if(acounter>bcounter)
{
bcounter=acounter;
}
{

}

}
System.out.print(bcounter);
}
}```

--- Update ---

Perhaps the question should be, is there another way of doing this?

--- Update ---

Ok, this is not correct.  Reply With Quote

23. ## Re: Find the the longest decreasing row of numbers in a vector

This tutorial shows a loop with an array.
Start at the tutorial I posted previously and work your way through, it is very useful

Add some print statements to see the values of the variables in question.
i, a.length, a.length-1
Get some numbers that have meaning, and see what is happening  Reply With Quote

24. ## Re: Find the the longest decreasing row of numbers in a vector

Ok. Is this as bad as the last one?

And is'nt this the: We solve your homework forum?

```package forrit;

public class Longesdecreasingttt {

public static void main(String[] args) {
int N = args.length;
int[] a = new int[N];
int downcounter=1;
int upcounter=1;
for (int i = 0; i < N; i++) {
a[i] = Integer.parseInt(args[i]);
}

{
for(int i=0;i<N-1;i++)

if (a[i]>=a[i+1])
{
downcounter++;
}
else
{
upcounter++;
if (upcounter>downcounter)
{
downcounter=1;
}

}

System.out.print(downcounter);
}
}
}```

--- Update ---

Ahh-sorry did'nt see that last post.Yeah I'll look through that.

--- Update ---

Mr.Jps can you please fix the double posting problem.

--- Update ---

I'm wondering, is this correct?  Reply With Quote

25. ## Re: Find the the longest decreasing row of numbers in a vector

It looks like you solved the problems you were having with arrays. Does the program do what it's supposed to do? How are you testing it?  Reply With Quote

26. ## Re: Find the the longest decreasing row of numbers in a vector

Mr.Jps. I find it so difficult to read this kind a stuff...I'll always get the sleepreading syndrome we see this, and just give up. Sort of like when I read my terrible tabing(syntax) in me last post muhhahah.

--- Update ---

Yeah. I put trough like Jeliot. Seems to working.

My professor puts the code throug all kinds of sequences like {1,1,-9,11, ,} {0,0,0} whatever. He's a shark, but nice shark. And he gives us zero for not fullfilling the standard.

Anyway I trying allsorts of stuff also from the command...  Reply With Quote

27. ## Re: Find the the longest decreasing row of numbers in a vector

Mr.Jps. I find it so difficult to read this kind a stuff...I'll always get the sleepreading syndrome we see this, and just give up.
IMO, if you want to be successful as a programmer, I think you need to get over this. So much of learning to program is inspired by self-motivated interest and exploration, largely through printed matter, that results in skills that are practiced to death and ultimately self taught. If you can't stay awake through this important process, then you should think about pursuing another field.  Reply With Quote

28. ## Re: Find the the longest decreasing row of numbers in a vector

Ok. Does anyone in Java-universe know why this code is giving 5 for this sequence:
0,0,5,79,8,8,7,4

--- Update ---

Ahh-sorry-that is correct?!

--- Update ---

I'm wondering. This is a correct code for a problem in a popular textbook. Should'nt we like...wipe some of it of?

--- Update ---

Or at least I think it is correct

--- Update ---

Ok lads. Now I have to prove this code with loop invariant?! Do you know anything about that?  Reply With Quote

29. ## Re: Find the the longest decreasing row of numbers in a vector

No this is not the "we solve your homework forum" this is the "we help you understand what you need to know so you can do your own homework forum".

The double posts will concatenate into one post with an update in a short time after posting, just ignore it.

Do you know what an invariant is? What invariant was given in the instructions (directly or indirectly)?  Reply With Quote

30. ## The Following User Says Thank You to jps For This Useful Post:

einar123 (September 23rd, 2013)

31. ## Re: Find the the longest decreasing row of numbers in a vector

Thanks jps. I think directly. But my program is not correct. I'll put up a new post.

--- Update ---

The invariant is supposed to be something like this:

1.F-->I
2.I ^ not R}S{I}
3.I ^ not R=>E

--- Update ---

What is wrong with me code?

For this sequence:{1,3,3,5,4,2,3} cracks it up.

```

public class Verkefni2t {

public static void main(String[] args) {
int N = args.length;
int[] a = new int[N];
int downcounter=1;
int upcounter=1;
for (int i = 0; i < N; i++) {
a[i] = Integer.parseInt(args[i]);
}

{
for(int i=0;i<N-1;i++)

if (a[i]>=a[i+1])
{
downcounter++;
}
else
{
upcounter++;
if (upcounter>downcounter)
{
downcounter=1;
}

}

System.out.print(downcounter);
}
}
}```

Error: at Java.lang.NumberFormatException.forInputString(unk nown Source)
at javalanginteger.parseInt(unknownSource)
at...[one more line of the same as the above]
at Verkefni2t.main java10

--- Update ---

What is going on with that downcounter over there? That is the problem, right there!  Reply With Quote

32. ## Re: Find the the longest decreasing row of numbers in a vector

Do you understand what causes the exception to be thrown? If not search Java.lang.NumberFormatException
If you do, then look at the code to see why the exception was thrown.  Reply With Quote

33. ## The Following User Says Thank You to jps For This Useful Post:

einar123 (September 23rd, 2013)

34. ## Re: Find the the longest decreasing row of numbers in a vector

Ok. I have a string in the array?

--- Update ---

Whitespace?

--- Update ---

It's the commas!

--- Update ---

I dont see it?! Something in line 10...

But why does the ''code give'' 1 for the sequence {1 3 3 5 4 2 3}
and {1 2 2 3}?  Reply With Quote