0

for some reason my code isn't breaking even though I write Quit multiple time the loop continues and It keeps asking me for messages even though it shouldn't so what the solution please? for some reason my code isn't breaking even though I write Quit multiple time the loop continues and It keeps asking me for messages even though it shouldn't so what the solution please?


package project;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import org.bson.Document; 
import com.mongodb.MongoClient; 
import com.mongodb.MongoCredential; 
import java.util.Scanner;

public class ConnectToMongo { 
   
   public static void main( String args[] ) {  
      
      // Creating a Mongo client 
      MongoClient mongo = new MongoClient( "localhost" , 27017 ); 
     
      // Creating Credentials 
      MongoCredential credential; 
      credential = MongoCredential.createCredential("sampleUser", "myDb","password".toCharArray()); 
      System.out.println("Connected to the database successfully");  
      
      //Accessing the database 
      MongoDatabase database = mongo.getDatabase("myDb"); 
      //we drop it la nma7e klshi b albo to prevent errors
      database.drop();
      database = mongo.getDatabase("myDb");
      
      //Creating a collection 
      database.createCollection("Messages"); 
      System.out.println("Collection messages created successfully");
      database.createCollection("Users"); 
      System.out.println("Collection users created successfully");
      
      // printing the names of the collections that are created in the database
      for (String name : database.listCollectionNames()) { 
          System.out.println(name); 
       }
      
      //create an object to access the collection
      MongoCollection<Document> Users = database.getCollection("Users");
      MongoCollection<Document> messages = database.getCollection("Messages");
      user user1 = new user("User1","123");
      user user2 = new user("User2","456");
      
      user1.insertToCollection(Users, 1);
      user2.insertToCollection(Users, 2);
      
      //Terminal system 
      Scanner scan1 = new Scanner(System.in);
      
      String m1,m2;
      int s=1;
      while(s==1) {
          System.out.println();
          //date taba3 l msg b m1
          m1 = createMessage(user1,user2,scan1,messages);
          System.out.println();
          //btjib l msg mn l database w btaamela print w bthot read true
          sendMessage(m1,messages);
              
          m2 =createMessage(user2,user1,scan1,messages);
          System.out.println();
          sendMessage(m2,messages); 
          
          if(m1=="Quit") {
              break; //Here is the Problem!!!!!!!!!!!!!!!!!!!!!!!!
          }
      }
      
      scan1.close();
      Users.drop();
      messages.drop();
    } 
   
   
   public static String createMessage(user Sender, user Receiver, Scanner scan, MongoCollection<Document> messages) {
       System.out.print(Sender.getUsername() +": ");
       String messageContent = "";
       wait(2000);
       messageContent = messageContent +scan.nextLine();
       Messages message = new Messages(Sender, Receiver, messageContent);
       message.insertToCollection(messages);
       return message.getSentTime().toString();
   }
   
   //hon aam njib l msg mn l database lahata nhoto bl terminal 
   public static void sendMessage(String dateOfMessage, MongoCollection<Document> messages) {
     boolean read = true;  
     Document m =  messages.findOneAndUpdate(Filters.eq("ID", dateOfMessage), Updates.set("Read Status", read));
     String content = m.getString("Message");
     String Sender = m.getString("Sender");
     String Receiver = m.getString("Receiver");
     
    
     System.out.println("Dear "+Receiver+", you have been sent a message from "+Sender+" stating the following: \n"+ content);
     

   }
   
  // to wait for the user to type 
   public static void wait(int ms){
       try
       {
           Thread.sleep(ms);
       }
       catch(InterruptedException ex)
       {
           Thread.currentThread().interrupt();
       }
   }
}

for some reason my code isn't breaking even though I write Quit multiple time the loop continues and It keeps asking me for messages even though it shouldn't so what the solution please? for some reason my code isn't breaking even though I write Quit multiple time the loop continues and It keeps asking me for messages even though it shouldn't so what the solution please?

  • 1
    `if(m1=="Quit")` [How do I compare strings in Java?](https://stackoverflow.com/q/513832) – Pshemo May 22 '22 at 20:34
  • 1
    This suggests that you have another problem beside one already mentioned. Check what exactly is value of `m1` before `if(m1=="Quit")` (maybe there is some additional character like space, or case doesn't match - note `a` != `A`). Try with `if("Quit".equalsIgnoreCase(m1.trim()))` instead of `if(m1=="Quit")`. – Pshemo May 22 '22 at 20:41
  • 1
    @Pshemo yes you're right m1 had a different value than "Quit" ! thank you I know how to solve it now! – Ali Haydar May 22 '22 at 20:56

0 Answers0