0

I have been struggling with my while loop and was wondering if you guys can see the fault in my logic.

The concept of the project

Creating a machine that will permanently loop through a bunch of code for the rest of its existence. Basically I am making an automatic cat feeder that will dispense food at certain times during the day. THAT IS WHY THE LOOP MUST BE PERMANENT.

Here is my basic code so far:

    DateFormat dateFormat = new SimpleDateFormat("HH:mm");                                                                                    // 1.

    Calendar cal = Calendar.getInstance();                                                                                                                      // 2.

    String CurrentTime  = dateFormat.format(cal.getTime());                                                                                 // 3. 

    jTextArea2.setText(CurrentTime);


    String FeedTimeMorning              = "06:00";
    String FeedTimeSnack                = "19:07";
    String FeedTimeMidday               = "12:30";




    boolean TempFeed = false;


     while(TempFeed=false)

     {

     if (FeedTimeMorning.equals(CurrentTime)) { txaOne.setText("FeedCats"+" " +CurrentTime);}


     if (FeedTimeSnack.equals(CurrentTime)){txaOne.setText("FeedCats"+" " +CurrentTime);}


     if(FeedTimeMidday.equals(CurrentTime)){txaOne.setText("FeedCats"+" " +CurrentTime);}

When it comes to the designated time the text "Feed Cats " does not appear.

Any help would be appreciated.

1 Answers1

4

One equals (=) is assignment while two (==) is equality, this

while(TempFeed=false)

should be one of

while(TempFeed==false)

or the shorter boolean negation (omitting the = entirely) like

while(!TempFeed)

With one = it assigns false to TempFeed and evaluates to false (which means the loop is never entered).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • @erickson Think I might have asked something similar to a question with an answer, Still appreciate the answers from everyone – The Noobie Coder Mar 15 '16 at 17:38
  • @TheNoobieCoder I hope my answer helped; you might also have better luck with cron or (if you must use Java) [quartz scheduler](https://quartz-scheduler.org/). – Elliott Frisch Mar 15 '16 at 17:40