vendredi 29 juin 2018

How do I fix the logic? [duplicate]

This question already has an answer here:

I have a program where I am trying to make an account and store it in a text file to be used in another program, the problem is that there is a problem with the if statements but I don't know what it is.

Here is the code:

import java.io.*;
import java.util.*;

public class Log_In_User_and_Pass {
public static void main(String[] args) {


    Scanner lineReader = new Scanner(System.in);
    BufferedWriter bw = null;
      try {

          File file = new File("C:\\Users\\home\\Desktop\\UserAndPassStorage.txt");

         /* This logic will make sure that the file 
          * gets created if it is not present at the
          * specified location*/
          if (!file.exists()) {
             file.createNewFile();
          }


          System.out.println("Would you like to create a new account? (Yes/No)");
          String response = lineReader.nextLine();

          if(response == "Yes" || response == "yes" || response == "No" || response == "no") {
              if(response == "Yes" || response == "yes") {
                  System.out.println("Please put a username: ");
                  String username = lineReader.nextLine();
                  System.out.println("Please put a password: ");
                  String password = lineReader.nextLine();
                  FileWriter fw = new FileWriter(file);
                  bw = new BufferedWriter(fw);
                  bw.write("Username: ");
                  bw.write(username);
                  bw.newLine();
                  bw.write("Password: ");
                  bw.write(password);
                  System.out.println("Account created.");
              }

              else {
                  System.out.println("Okay.");
              }
          }

          else {
              System.out.println("That is not one of the options.");
          }

      } catch (IOException ioe) {
       ioe.printStackTrace();
    }
    finally
    { 
       try{
           lineReader.close();
           if(bw!=null) {
         bw.close();}
       }catch(Exception ex){
           System.out.println("Error in closing the BufferedWriter"+ex);
        }
    }
}
}

And here is the output of the program:

Would you like to create a new account? (Yes/No)
Yes
That is not one of the options.

It should be going to the part where you make a username and password but it doesn't so I am really confused. Thanks for the help.

Aucun commentaire:

Enregistrer un commentaire