wood burning stoves*
The moose likes Beginning Java and the fly likes what is the mistake inthis simple code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is the mistake inthis simple code" Watch "what is the mistake inthis simple code" New topic
Author

what is the mistake inthis simple code

sindura kapur
Greenhorn

Joined: Jul 14, 2008
Posts: 22
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 ,


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
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

Joined: Jun 14, 2008
Posts: 96
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

Joined: Jul 22, 2008
Posts: 10
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 ]

Regrads,<br />Vedha.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Jul 14, 2008
Posts: 22
thanks a lot everybody you guys really helped but plaes explain the for each loop
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the mistake inthis simple code