Why i cannot quit this while loop(java)?

I am new here and i got into trouble about my coding, i hope someone could help me break this loop, cause no matter what i type, it always in the loop.My code is as follow:

    boolean flag = true;
    while(flag)
    {
        System.out.println("Please enter the movie's title:");
        title = s.nextLine().trim();
        if(title.trim().equals(""))
        {
            System.out.println("Please do not enter an empty title!");
        }
        else 
        {
            for(Movie m: movies)
            {
                if(title.equals(m.getTitle().trim()))
                {
                    System.out.println("Title already exists!");
                }
                else
                {
                    m.setTitle(title);
                    flag = false;
                    break;
                }
            }
        }
    }

5
Leave a Reply

avatar
5 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

You could use an little known feature of Java labeled breaks. Example from the officials docs: class BreakWithLabelDemo { public static void main(String[] args) { int[][] arrayOfInts = { { 32, 87, 3, 589 }, { 12, 1076, 2000, 8 }, { 622, 127, 77, 955 } }; int searchfor = 12; int i; int j = 0; boolean foundIt = false; search: for (i = 0; i < arrayOfInts.length; i++) { for (j = 0; j < arrayOfInts[i].length; j++) { if (arrayOfInts[i][j] == searchfor) { foundIt = true; break search; } } } if (foundIt) { System.out.println("Found " +… Read more »

Jason
Guest

The break leaves for loop, but you still need one more break to exit while loop.

Jason
Guest
if(title.trim().equals(""))
    {
        System.out.println("Please do not enter an empty title!");
         flag=flase;
    }

and here also

 if(title.equals(m.getTitle().trim()))
            {System.out.println("Title already exists!"); 
flag=false;
break;

 }

Jason
Guest

There’s a small logical problem. When you start entering the movie names, you are gonna stop input/entering the values at some point, right?. The program must also know that so add a condition check title.trim().equals(“break”) boolean flag = true; while(flag) { System.out.println("Please enter the movie's title:"); title = s.nextLine().trim(); if(title.trim().equals("")) { System.out.println("Please do not enter an empty title!"); } else if(!title.trim().equals("break")) { for(Movie m: movies) { if(title.equals(m.getTitle().trim())) { System.out.println("Title already exists!"); } else { m.setTitle(title); flag = false; break; } } }else{ flag = false; break; } } The above code should do the trick. Let me know if the… Read more »

Jason
Guest

initially when you enter your first title there are no objects inside “movies” sequence which “for” loop can go through, thus I believe your code initially never enters the “for” loop and continues.

I recommend use a if(movies == null) statement before for loop to check for empty sequence.