• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why i can't print this array?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Why this code is not working?
I can't set a number to the array!!

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is not compiled.
Error is:
 
jittu goud
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Complete code:
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot :-)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic