Results 1 to 7 of 7

Thread: For loop error

  1. #1
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default For loop error

    I have been having some trouble with a for loop statement, here is my code:

    //Here is my multiplication table solution

    public class MultiplicationTable
    {

    public static void main(String args[])
    {

    //Here I'll declare the variables and constants
    final int MIN_ROW = 1;
    final int MAX_ROW = 9;
    final int MIN_COL = 2;
    final int MAX_COL = 8;
    int firstNumber = 2;
    int secondNumber = 1;
    int theAnswer = 0;

    System.out.print("CSC-116 Homework Eight - Multiplication Table for N equals 2 through 8");


    for (firstNumber >= MIN_COL; firstNumber <= MAX_COL; firstNumber++)
    {
    theAnswer = firstNumber * secondNumber;
    ++secondNumber;
    System.out.print("Hello");
    }
    }
    }
    And here is the error I get:
    MultiplicationTable.java:21: not a statement
    for (firstNumber >= MIN_COL; firstNumber <= MAX_COL; firstNumber++)
    Thank you very much for your help.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    The first part of a for loop is not a condition.

    A common for loop may look like this:

    Java Code:
    for(int i = 0; i < 10; i++)
    {
    //code
    }
    You'll notice "int i = 0" initializes the counter to be used.

    Also, remember you can use the && (and) or || (or) to use multiple conditions,

    Java Code:
    boolean example = true;
    for(int i = 0; i < 10 && example; i++)
    {
    //code
    }
    Last edited by AndrewM16921; 04-04-2009 at 07:53 PM.

  3. #3
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question What loop should I use?

    Iwant to set a loop but am not experienced enough to work out which kind.

    I want to set a maximum of 12 records to be stored in a .txt file, should I be using a while loop?

  4. #4
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Quote Originally Posted by dbashby View Post
    Iwant to set a loop but am not experienced enough to work out which kind.

    I want to set a maximum of 12 records to be stored in a .txt file, should I be using a while loop?
    Create a new thread and I'll answer this.

  5. #5
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Java Code:
    for (firstNumber >= MIN_COL; firstNumber <= MAX_COL; firstNumber++)
    No, you just take the ">" off the loop and that's should work )))

  6. #6
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    OK, I'll try this, thanks

  7. #7
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    Quote Originally Posted by Webuser View Post
    Java Code:
    for (firstNumber >= MIN_COL; firstNumber <= MAX_COL; firstNumber++)
    No, you just take the ">" off the loop and that's should work )))
    Wow I wasn't even thinking, I thought he was using two conditions. But yeah, that seems to be what he did wrong.

Similar Threads

  1. Loop Help
    By HeavyD in forum New To Java
    Replies: 7
    Last Post: 09-22-2010, 09:55 PM
  2. help error in while loop
    By iPetey in forum New To Java
    Replies: 3
    Last Post: 04-03-2009, 03:56 AM
  3. Replies: 3
    Last Post: 01-23-2009, 08:53 AM
  4. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 04:28 PM
  5. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 10:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •