0

I'm using Netbeans 8.0.2 with 64bit JDK-1.8 on Windows 10 x64 machine (also tested on Ubuntu 15.04) This is a file from a chat system which checks operation (signin/signout) and subsequently, checks in onlineusers.txt if user exists, if not it writes user to file.
Edit
My program is writing somewhere because it can execute all 3 if-else blocks successfully, but the problem is the text file to which I have set path, is still blank.

P.S. - for writing in file I've already tried method of creating temp file writing to it, deleting old, renaming temp. That didn't worked out on Netbeans (I tried it without IDE and it did work)

Here's my code, I've been trying with parameters - user=nick&oper=signin

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        System.out.println("Users.java Start");
        String onlineUserPath = getClass().getResource("/textfiles/onlineusers.txt").getPath();
        File onlineUsers = new File(onlineUserPath);
        BufferedReader reader = new BufferedReader(new FileReader(onlineUsers));

        ArrayList<String> tempUsers = new ArrayList<>();
        boolean userExist = false;
        String s;
        String user = request.getParameter("user");
        String operation = request.getParameter("oper");
        tempUsers.add(user); //uncommented

        if ("signin".equals(operation)) {
            while ((s = reader.readLine())!=null) {
                if (s.equals(user)) { //edited
                    userExist = true;
                    out.print("userexist");
                    break;
                } else {
                    tempUsers.add(s);
                    out.print("signin");
                }
            }
        } else if("signout".equals(operation)) {
            while ((s = reader.readLine()) != null) {
                if (s.equals(user) { //edited
                    out.print("signout");
                    userExist = true;
                    continue;
                }
                tempUsers.add(s);
            }

            if (!userExist) 
                out.print("usernotfound");

        } else if ("showmsg".equals(operation)) {
             userexist = true; //added
            while ((s = reader.readLine()) != null) {
                out.print(s + "<br/>");
            }
        }

        //Writing            
        if (!userExist) {
            FileWriter writer = new FileWriter(onlineUsers, false);
            for (String add : tempUsers) {    
                writer.write(add);
                System.out.println(add);
            }
            writer.close(); 
        }

        reader.close();
        System.out.println("User.java code complete");
    } catch(Exception e) {
        System.out.println("Exception: " + e);
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Nick Warke
  • 91
  • 6
  • This doesn't have anything to do with writing a file, or chat, or Netbeans either. – user207421 Aug 27 '15 at 03:22
  • @EJP Please read my edit again. I want to know why my text file is blank when program is working perfectly. Where is my data being stored? – Nick Warke Aug 27 '15 at 11:06

1 Answers1

0

Given the parameters you've provided, the relevant code section is as follows:

String user = request.getParameter("user");
String operation = request.getParameter("oper");

if ("signin".equals(operation)) {
    while ((s = reader.readLine()) != null) {
        if (s == user) {
            userExist = true;
            out.print("userexist");
            break;
        } else {
            tempUsers.add(s);
            out.print("signin");
        }
    }
}

You know you're not getting any of the calls to print() in this section, so you're never entering the positive branch of the if statement. The problem is how you're checking if the user you've just read from the file matches the user passed in as a request parameter.

You must use String.equals() to check for String equality, as outlined in this answer.

Community
  • 1
  • 1
Edd
  • 3,724
  • 3
  • 26
  • 33