Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java for loop question

Options
  • 18-01-2009 3:17am
    #1
    Registered Users Posts: 2,708 ✭✭✭


    How does one initialise an int in a for loop if the value is unknown. Say for instance it would be entered through keyboard input.


Comments

  • Registered Users Posts: 17,727 ✭✭✭✭Sherifu


    If you take it in before the for loop, you wouldn't initialise it. You'd just leave that part of the for loop blank.


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    for(int i = 0 ; i < valueReadFromKb ; i++){
    ...
    //do stuff here
    ...
    }


  • Registered Users Posts: 2,708 ✭✭✭ScissorPaperRock


    for(int i = 0 ; i < valueReadFromKb ; i++){
    ...
    //do stuff here
    ...
    }

    Yeah I could create another int and initialise that one, and update that one.
    But what I was wondering was if I could convert the outside while loop of this to a for loop - where numOfStars is entered through keyboard

    int numOfStars = scanner.nextInt();
    int countMax = numOfStars;
    int row = numOfStars;

    while(numOfStars>0)
    {
    for(row = numOfStars; row<=countMax; row++)
    {
    System.out.print("*");
    }
    System.out.println(" ");
    numOfStars--;
    row=numOfStars;
    }



    If I leave that part of the loop blank it doesn't work, unless Im doing something wrong

    Thanks for the help!


  • Registered Users Posts: 17,727 ✭✭✭✭Sherifu


    I was talking about something like;

    for(; numOfStars<=countMax; numOfStars++)

    but I see you want to use numOfStars outside the for loop.


  • Registered Users Posts: 2,708 ✭✭✭ScissorPaperRock


    Sherifu wrote: »
    If you take it in before the for loop, you wouldn't initialise it. You'd just leave that part of the for loop blank.

    Ah that worked - i didn't realise I had to put in the ; to leave it blank

    Cheers!


  • Advertisement
  • Registered Users Posts: 2,708 ✭✭✭ScissorPaperRock


    Sherifu wrote: »
    I was talking about something like;

    for(; numOfStars<=countMax; numOfStars++)

    but I see you want to use numOfStars outside the for loop.


    Yeah I'm using it outside the for loop but it's been initialised outside the loop beforehand so it seems ok to leave it blank in the for loop. It works anyway! :)


  • Registered Users Posts: 2,708 ✭✭✭ScissorPaperRock


    Can anyone explain why it doesn't work the same when I take the "row = numOfStars" out of the second for loop, even though I've previously initialised it as such.

    Thanks again


  • Closed Accounts Posts: 852 ✭✭✭blackgold>>


    ever hear of a debugger?:rolleyes:


  • Registered Users Posts: 2,708 ✭✭✭ScissorPaperRock


    ever hear of a debugger?:rolleyes:

    Yep! But this whole programming thing is new to me and I wouldn't be too sure how to use one. I thought they showed errors, whereas this isn't an error per se, it's a matter of the code working in a manner other than I intended when I format it differently.

    But from thinking about it a bit more I've realised that it's behaving differently when I convert it to a for loop because the sequence has changed. i.e.

    if I had taken the row = numOfStars out of the for loop while the numOfStars-- was moved to a for loop (rather than at the end of a while loop) then the numOfStars decrement is happening after the row=numOfStars which is not what I needed.

    You learn something new everyday!


  • Closed Accounts Posts: 852 ✭✭✭blackgold>>


    try programing without a language its easier:)
    maybe your spending to much time on java and not enough planning your solution.


  • Advertisement
  • Registered Users Posts: 2,708 ✭✭✭ScissorPaperRock


    try programing without a language its easier:)
    maybe your spending to much time on java and not enough planning your solution.

    My solution?

    I just completed a module on Java, and quite enjoyed it. So I was just experimenting a little.

    Maybe you spend too much time judging and not enough time saying something useful


  • Closed Accounts Posts: 852 ✭✭✭blackgold>>


    :D


Advertisement