Search code examples
javatimewhile-loopreal-time-javano-database

How to fix while loop error login logout system (no database) (also is there a code about session time)?


I create a basic login and logout system (no database) with the only username given for my school homework, but I have problems with while loops and also session time.

I tried to copy and paste a group of codes to different parts of this main code so that I can get my expected outcome but it turns out to be a bit faulty. I tried to search on the internet about session time but I got nothing.

  while (login =  true){
      try {
          System.out.println("Enter your name:");
          String name = cue.nextLine();
          System.out.println("--------------------");
          System.out.println("");
          System.out.println("Date and Time of Login:");
          System.out.println(dtf.format(now));
          System.out.println("");
          System.out.println("--------------------");
          System.out.println();
          System.out.println("Enter your name to log out:");
          String logout = cue.nextLine();
          System.out.println("");

          if (logout.equals(name)){

              System.out.println("--------------------");
              System.out.println("");
              System.out.println("Date and Time of Logout:");
              System.out.println(dtf.format(now));
              System.out.println("Session Time:");
              /*can you also please tell me what code to tell the gap between the 
              login time and log out time?*/
              System.out.println("");
              System.out.println("--------------------");
              login = false;
            } else {
                login = true;
            }  
      } catch (Exception e){
          cue.nextLine();
      } finally{
          System.out.println("Do you want to register again? 0 for yes and 1 for no");
          int no = cue.nextInt();
          if (no==0) {
              login = true;
          } else if (no==1) {
                System.exit(1);
          } else {
              System.out.println("1 or 0 only!");
          }   
      }
}
  • This must be the expected output:

if the name is correct:

Enter your name:
nmae
--------------------

Date and Time of login:
2019/02/03 16:38:46

--------------------

Enter your name to log out:
nmae

--------------------

Date and Time of logout:
2019/02/03 16:38:46
Session Time:
(This must show how many minutes and seconds a client uses the program)

--------------------
Do you want to register again? 0 for yes and 1 for no
0
Enter your name:

incorrect and it is corrected later on:

Enter your name:
name
--------------------

Date and Time of login:
2019/02/03 16:38:46

--------------------

Enter your name to log out:
nmae

Enter your name to log out:
name

but it turns out that on second loop, third loop and so on, the program asks me to "enter your name to log out" and "Do you want to register again? 0 for yes and 1 for no" instead. The "enter your name" part prints instead of asking my name.

and then when I enter 0 to exit, this error showed up C:\Users\DELL\AppData\Local\NetBeans\Cache\10.0\executor-snippets\run.xml:111: The following error occurred while executing this line: C:\Users\DELL\AppData\Local\NetBeans\Cache\10.0\executor-snippets\run.xml:94: Java returned: 1


Solution

  • To get difference beetwen login time and logout time you can use Duration class from java8:

    loginTime = LocalDateTime.now();
    ...
    logoutTime = LocalDateTime.now();
    
    Duration.between(loginTime, logoutTime).getSeconds();