• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the mistake inthis simple code

 
sindura kapur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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]
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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 ,
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • 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
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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.
 
vedha vas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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 ]
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks a lot everybody you guys really helped but plaes explain the for each loop
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic