Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why i can't print this array?

 
ahmad mayahi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Why this code is not working?
I can't set a number to the array!!

 
jittu goud
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well you are trying to assign a integer type to String type ....with out typecasting
 
ahmad mayahi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is not compiled.
Error is:
 
jittu goud
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays in Java are 0-indexed, not 1-indexed. So an array of length 3 has indexes 0 ... 2, not 1 ... 3.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
ahmad mayahi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Complete code:
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
ahmad mayahi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic