wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Why i can't print this array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why i can Watch "Why i can New topic
Author

Why i can't print this array?

ahmad mayahi
Greenhorn

Joined: May 10, 2009
Posts: 19
Hi
Why this code is not working?
I can't set a number to the array!!

jittu goud
Ranch Hand

Joined: Mar 30, 2007
Posts: 46
well you are trying to assign a integer type to String type ....with out typecasting
ahmad mayahi
Greenhorn

Joined: May 10, 2009
Posts: 19
Is not compiled.
Error is:
jittu goud
Ranch Hand

Joined: Mar 30, 2007
Posts: 46
with the given code only error you should get is ...incompatible types...

may be you have more errors than what you have pasted here...

paste your complete code
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41060
    
  43
Arrays in Java are 0-indexed, not 1-indexed. So an array of length 3 has indexes 0 ... 2, not 1 ... 3.


Ping & DNS - my free Android networking tools app
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
ahmad mayahi wrote:Is not compiled.
Error is:


You have your loop set to $i <= Str.length, which is causing your errror in Str[$i]. Your array index will end at Str.length-1, so when $i = Str.length still goes through the loop Str[$i] will be out of bounds.

EDIT: darn, not fast enough....


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
ahmad mayahi
Greenhorn

Joined: May 10, 2009
Posts: 19
Complete code:
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
ahmad mayahi wrote:Complete code:


Also, it looks like you are assigning values to the whole array, but are printing just the first value. Not sure if that is what you are planning, just an FYI.
jittu goud
Ranch Hand

Joined: Mar 30, 2007
Posts: 46
for (int $i = 0; $i <= Str.length; $i++)



as said by others the array runs from 0 to n-1

you should say $i < Str.length;

.............


i guess this is what you wanted to do.... though $i is variable name $ doesnt make any difference to variable....$i or normal 'i' would make the same output
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296

now the code compiles and runs fine. the error is !) we have to type cast to string 2) Array out of bound is run time error bcz you trying to asign the value to the out of index when you put the index as [3] you can assign only up to 0....2 because array index starts from 0 to length -1


Creativity is nothing but Breaking Rules
ahmad mayahi
Greenhorn

Joined: May 10, 2009
Posts: 19
Thanks alot :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why i can't print this array?
 
Similar Threads
string array
String to Array of String
Once more about Array
how to check whether a word is a palindrome
String and StringBuffer