-2

user have to write an email in multiple lines and stop the input when ".." (two dots) are entered by the user. then the email should be saved to the variable but the variable saves the last input which are the two dots. this is my code any changes?

BufferedReader inl = new BufferedReader(new InputStreamReader(System.in));
String email_data;

System.out.println("Data: ");

do{
    email_data = inl.readLine();
} while(email_data != "..");
azro
  • 53,056
  • 7
  • 34
  • 70
04k
  • 175
  • 2
  • 8

1 Answers1

0

Append the input to your variable instead of overriding it.

Also, don't use '!=' or '==' on Strings - use the .equals() method instead.

BufferedReader inl = new BufferedReader(new InputStreamReader(System.in));
String email_data = "";
String input;
System.out.println("Data: ");

do{
    input = inl.readLine();
    if (!input.equals("..")) {
      email_data += input;
    }
} while(!input.equals(".."));
Riaan Nel
  • 2,425
  • 11
  • 18
  • seems good, but it doesnt exit the loop – 04k Dec 24 '19 at 10:58
  • That's because email_dat is a combined String now, not just the last input. See the code above. It's a bit messy, but should work. You can clean it up by creating an infinite loop - e.g. while(true) and then breaking out if you get '..'. – Riaan Nel Dec 24 '19 at 11:09