• 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

what is the mistake inthis simple code

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ad
{
public static void main(String ar[])
{
char a[]=new char[5];
String s="hello';
for(int i=0 ;i<5;i++0
{
a[i]=s.charAt(i);}system.out.println(a[i]);
}
}
the javac is giving this msg:cannot resolve symbol
variable a[i]
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sindura kapur:
class ad
{
public static void main(String ar[])
{
char a[]=new char[5];
String s="hello';
for(int i=0 ;i<5;i++0
{
a[i]=s.charAt(i);}system.out.println(a[i]);
}
}
the javac is giving this msg:cannot resolve symbol
variable a[i]



the compiler tells you better, as he is not able to resolve a[i] in for loop syntax ,
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sindura,

Where did you define the variable 'i'?
What is the scope of 'i'?
Where do you access 'i'?

And yes,
Copy and paste the code from original location.PostRealCode
Please UseCodeTags
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had to put this into an IDE to notice it. Is the post exactly the same as your real code?

There are a number of problems:
(i) make sure Strings are enclosed by "my text". You have used "my text'.
(ii) The System.out uses a lower case 's' and needs to be upper case.
(iii) You've ended the for loop immediately after the s.charAt call so the variable i which is declared inside the for loop is not visible.

If all you want to do is create a char[] from a string then you are better using the String.toCharArray() method.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sindura Kapur,

The follwoing are the mistakes i noticed:

1. The String hello was not ended properly.

String s= "hello" ;

2.The sysout was not proper.

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

3.The local variable(i) wat you inside the for loop.Its life or visibility is only within the for loop
you cant refer it outside.

4.Try this way:


(or)



[edit]Add code tags. CR[/edit]
[ July 24, 2008: Message edited by: Campbell Ritchie ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a far more serious mistake, using "< 5" in the for loop. If you pass another word eg "Goodbye" or "Hiya" you can get errors, either with not printing all the word, or exceptions.

You ought either to use the for-each loop (enhanced for loop) like thisor use the length attribute of the arrayBoth those techniques will gaurd against both those problems. In this instance (assuming you are using Java 5 or 6) the for-each loop is preferable.

And, everybody, please remember the code button. I have edited one post, so you can see how much better it looks. And always use ctrl-c ctrl-v to post code with errors in, so we can see the exact mistake.
[ July 24, 2008: Message edited by: Campbell Ritchie ]
 
sindura kapur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot everybody you guys really helped but plaes explain the for each loop
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
I presume you have read the link I gave earlier? Here are a few more 1 (may be a bit complicated) 2 (nice and simple) 3 (needs registration).

Read those and see whether they help. I think they will. Note what it says in no 2 that it is "access only."
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic