Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why won't this for loop work?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why won't this for loop work?

    Here's the code:


    public class who{
    public static void main(String[] args){
    String s = "Who?";
    for(int j = s.length() - 1; j >= 0; j--);
    System.out.println(s.charAt (j));
    }
    }


    I'm running Dr.Java, and this is the error I get when I compile:

    1 error found:
    File: C:\Users\Victoria\who.java [line: 5]
    Error: C:\Users\Victoria\who.java:5: cannot find symbol
    symbol : variable j
    location: class who

    I cannot seem to figure out what I'm doing wrong here.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Why won't this for loop work?

    You have a semicolon at the end of the for loop line which terminates the loop prior to the println statement, and causes the variable j to go out of scope.

Similar Threads

  1. How to make for loop work?
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 8th, 2011, 01:14 PM
  2. I can't get this loop to work correctly
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 27th, 2011, 03:20 PM
  3. [SOLVED] Simple While loop wont work??
    By chuckie987 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2011, 01:58 PM
  4. Can a for loop work with random number?
    By humdinger in forum Loops & Control Statements
    Replies: 7
    Last Post: January 10th, 2010, 09:06 PM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM

Tags for this Thread