help me through this program

publicclass Reverse{

int count;

publicvoid forward(char inp[] ){

for (count=0 ;inp[count] !='\0'; count++){//error

System.out.print(inp[count]);

}

System.out.print(" | ");

}

publicvoid backward(char inp[]){

while(count != 0){

System.out.print(inp [count]);

count--;

}

}

}

publicclass MainMethod{

publicstaticvoid main(String args[]){

char[] input ={'s','a','u','r','a','b','h'};

Reverse call =new Reverse();

call.forward(input);

call.backward(input);

}

}

i am getting an error. i am not getting through the solution

__

ERROR:

run:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7

at Reverse.forward(Reverse.java:6)

at MainMethod.main(MainMethod.java:5)

__

there is some problem in the 'for' loop.

help me

[2233 byte] By [saurabh@univa] at [2007-11-27 0:38:21]
# 1
Do you have a C background?
DrLaszloJamfa at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 2
ya
saurabh@univa at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 3

Java is quite a bit different from C. Nul chars are not needed or typically used.

They do not appear in Strings as terminators.

void forward(char[] s) {

for(int i=0; i < s.length; ++i)

System.out.println(s[i]);

}

//or

void forward(char[] s) {

for(char ch : s)

System.out.println(ch);

}

DrLaszloJamfa at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 4

That's because you can't use unequal to '\0' as terminating char check, you'll need to use the length of the arrays, so like this:

for (int i = 0; i < inp.length; i++) {

// do stuff

}

You should use Strings btw, it's standard to use that instead of char arrays, unless you want an array of chars that isn't a string.

Simeona at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 5
still the error is therenow its in method backwardin the while loopthe error is the same
saurabh@univa at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 6
What is count's initial value? I suggest you make it a localvariable, if you haven't already. And as another poster hassuggested, prefer String to char[], Unless you have a specificreason for needing to use an array.
DrLaszloJamfa at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 7
Try using a for loop in backwards as well. Only difference is it will be a reverse loop. You start your index at the end of the array (think carefully about this) and decrement instead of increment.
floundera at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 8
its the length of char inp
saurabh@univa at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 9
> its the length of char inpRecall that if an array v has length 10, its elements are v[0] to v[9]. There is no v[10].
DrLaszloJamfa at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 10

i got that, but....... public void backward(String inp[]){

int count;

for(count=inp.length; count >=0 ; count--) {

System.out.print(inp [count]);

}

}

the error is the same

the loop is going out of scope

saurabh@univa at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 11
Recall that if an array v has length 10, its elements are v[0] to v[9]. There is no v[10].
DrLaszloJamfa at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 12
You start your index at the end of the array (think carefully about this)Do we need to spell it out for you any clearer? You are not starting at the correct location.
floundera at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 13
thanksi was just doing the blunder
saurabh@univa at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 14
at last tell me one more thing how to convert between data typeeg.if i have String[] argsand i want to convert it into char, using for loop. how can i do this?
saurabh@univa at 2007-7-11 22:49:14 > top of Java-index,Java Essentials,Java Programming...
# 15
You want to convert an array of Strings into what, exactly? Wouldn't it be more useful to leave them as Strings?
DrLaszloJamfa at 2007-7-21 19:50:32 > top of Java-index,Java Essentials,Java Programming...
# 16
its necessary to convert String into char to get this output of the same programsaurabh | hbaruas
saurabh@univa at 2007-7-21 19:50:32 > top of Java-index,Java Essentials,Java Programming...
# 17
I suggest you convert your methods to take Strings, not char [ ].
DrLaszloJamfa at 2007-7-21 19:50:32 > top of Java-index,Java Essentials,Java Programming...
# 18
in String how can we revert the string
saurabh@univa at 2007-7-21 19:50:32 > top of Java-index,Java Essentials,Java Programming...
# 19
> in String how can we revert the stringSimilar to arrays, except one accesses characters with method charAt, and length is given by *method* length: http://java.sun.com/javase/6/docs/api/java/lang/String.html
DrLaszloJamfa at 2007-7-21 19:50:32 > top of Java-index,Java Essentials,Java Programming...
# 20
Or you could cheat and use a StringBuffer ;)
floundera at 2007-7-21 19:50:32 > top of Java-index,Java Essentials,Java Programming...