out of bounds error

i'm trying to get this code to count down from 60 to 40 and then from 100 to 90.

i attempted to make an empty array, put the appropriate numbers in the array and then put the numbers from the array into an empty string.

but i get an out of bounds error. what do i need to change?

thanks

class CounterSS

{

publicstaticvoid main(String[] args)

{

System.out.println(counter (60, 40, 2) );

//count down from 60 to 40 in twos.

System.out.println(counter (100, 90, 1) );

//count down from 100 to 90 in ones.

}

publicstatic String counter(int z,int x,int c)

{

String countnums ="";

if(z > x){

int s = z - x;

int[] count =newint[s];//create an empty array

int k = 0;

while(k < count.length){

k++;

count[k] = z;

z = z -c;

}

for(int r = 0; r< count.length; r++)

{

countnums = countnums + count[r];

}

}

return countnums;

}

}

java.lang.ArrayIndexOutOfBoundsException: 20

at CounterSS.counter(CounterSS.java:32)

at CounterSS.main(CounterSS.java:8)

Exception in thread "main"

**** JDK Commander: E n do fj a v ae x e c u t i o n****

[2328 byte] By [mark_8206a] at [2007-11-26 19:58:01]
# 1
Move k++ to the end of the while loop.Kaj
kajbja at 2007-7-9 22:53:19 > top of Java-index,Java Essentials,New To Java...
# 2
> Move k++ to the end of the while loop.thanks, it's workingbut why does that make a difference anyway?
mark_8206a at 2007-7-9 22:53:19 > top of Java-index,Java Essentials,New To Java...
# 3
The while checked if k was still less than the length of the array, and then you increasd k before you used it. So k was outside of the valid range. You are now using k before you increase it.Kaj
kajbja at 2007-7-9 22:53:19 > top of Java-index,Java Essentials,New To Java...
# 4
This is an alternative,count[k++] = z;
kajbja at 2007-7-9 22:53:19 > top of Java-index,Java Essentials,New To Java...
# 5

i suppose i should know this but when counting downwards, all lines miss the last number.

for example, the first line counts down to 42 and not 40.

is the problem with the while or for loop? or maybe both?

class Counter

{

public static void main(String[] args)

{

System.out.println(counter (60, 40, 2) );

//count down from 60 to 40 in twos.

System.out.println(counter (100, 90, 1) );

//count down from 100 to 90 in ones.

System.out.println(counter (100, 80, 10) );

//count down from 100 t0 80 in tens.

}

public static String counter(int z, int x, int c)

{

String countnums = "";

if(z > x) {

int s = z - x; s = s / c;

int[] count = new int[s]; //create an empty array

int k = 0;

while (k < count.length) {

count[k] = z;

z = z -c;

k++;

}

for(int r = 0; r< count.length; r++)

{

countnums = countnums + count[r]+" ";

}

}

return countnums;

}

}

60 58 56 54 52 50 48 46 44 42

100 99 98 97 96 95 94 93 92 91

100 90

**** JDK Commander: E n do fj a v ae x e c u t i o n****

mark_8206a at 2007-7-9 22:53:19 > top of Java-index,Java Essentials,New To Java...
# 6

> i suppose i should know this but when counting

> downwards, all lines miss the last number.

> for example, the first line counts down to 42 and not

> 40.

> is the problem with the while or for loop? or maybe

> both?

int s = z - x; s = s / c; // problem here

It's also ugly to put both statements on one line. Either put them on two lines, or combine them:

int s = (z-x)/c; // Still a problem in calculation

Since you are missing exactly one element with your calculation of s, what do you think you need to add to make it correct?

Say z = 5 and x = 1 and c = 1. You know that's 5 numbers. But, your formula for s says:

int s = (5 - 1) / 1 = 4;

Say z = 20 and x = 11 and c = 1. You know that's 10 numbers. But, your formula for s says:

int s = (20 - 11) / 1 = 9;

Does that help?

doremifasollatidoa at 2007-7-9 22:53:19 > top of Java-index,Java Essentials,New To Java...
# 7
yeah that helps, thanks a lot.i'll forget about changing the for and while loops.
mark_8206a at 2007-7-9 22:53:19 > top of Java-index,Java Essentials,New To Java...
# 8
> yeah that helps, > thanks a lot.> > i'll forget about changing the for and while loops.You're welcome. Your loops look okay to me.
doremifasollatidoa at 2007-7-9 22:53:19 > top of Java-index,Java Essentials,New To Java...