Help Anyone!!!! Asap Please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ToAsTeEJaVa
    New Member
    • Nov 2006
    • 7

    Help Anyone!!!! Asap Please

    I need to repeat a set of instructions but if the user enters a certain character or number e.g. 0; the program should stop...
    what methods can I use for this?

    I have tried to use a "for" method but there is no way to break the cycle!
    and so I have tried a "while" method too but our handbook fails to specify how to stop a program.

    please help me asap!
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by ToAsTeEJaVa
    I need to repeat a set of instructions but if the user enters a certain character or number e.g. 0; the program should stop...
    what methods can I use for this?

    I have tried to use a "for" method but there is no way to break the cycle!
    and so I have tried a "while" method too but our handbook fails to specify how to stop a program.

    please help me asap!
    I would use a 'while' loop.



    Check this out, and feel free to ask questions on anything you don't understand.

    Comment

    • ToAsTeEJaVa
      New Member
      • Nov 2006
      • 7

      #3
      I Hope you guys understand afrikaans 'cause Here is my code:
      ---------------------------------------------------------------------------------------------------------------------
      import java.io.*;

      public class TNOV05V3
      {
      public static void main(String[]args) throws IOException
      {
      BufferedReader in = new BufferedReader
      (new InputStreamRead er(System.in));

      for(int k =0;k<=5;k++) //using a for code
      {
      System.out.prin tln("Tik Groote in asb. 1 - 3: ");
      String sGroot = in.readLine();
      int iGroot = Integer.parseIn t(sGroot); //want program to end here if 0 is typed

      System.out.prin tln("Tik Hoeveelheid Extras in: 1 - 5");
      String sExtra = in.readLine();
      int iExtra = Integer.parseIn t(sExtra);


      NOV05V3 nov = new NOV05V3(iGroot, iExtra);

      System.out.prin tln("Groote : " + iGroot + " is: " + nov.geeGroote() );
      System.out.prin tln("Extras : " + iExtra + " is: " + nov.geeExtras() );
      System.out.prin tln("Koste is: " + nov.geeKoste()) ;
      }
      System.exit(0);

      }
      }

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Yeah, just change the for loop to a while loop, and for the expression, you just check it against iExtra. (That's what i would do, anyway)

        Comment

        • ToAsTeEJaVa
          New Member
          • Nov 2006
          • 7

          #5
          if I put the while in the "for" place it is out of scope range and thus does not find the symbol variable iExtra.

          any ideas?

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Right, it did - the variables iGroot and iExtra only exist inside that loop, and are undeclared at the initialization of the while loop. You need to declare them outside of the while loop.

            Also, I just read the comment that said //end here if iGroot is 0, so you can change the while conditional to compare to iGroot.

            From there it works, I got it to build successfully with those modifications.

            Comment

            Working...