Problem with Login and reading user information into text filed when the user logged in [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • Scanner on text file hasNext() is infinite

    4 answers




I can't figure out what is the problem, I need you pros to help me with this please. I need it to login by reading their username and password in text file, and load those user information into the text field that store in the text file too. For now my code problem is when I click on Login nothing happen.



Here's my text file information :



jack123
jackabc
jc@gmail.com
Jack
123123123
012344567
no1,taman abc.


And here is my Login code :



import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.EventObject;
import java.util.Scanner;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public class LoginButton {


@FXML
private TextField txtfUsername;

@FXML
private TextField txtfPassword;

@FXML
private Button btnLogin;

@FXML
private Button btnRegister;

@FXML
void setOnLogin(ActionEvent event1) throws IOException{

String username = txtfUsername.getText();
String password = txtfPassword.getText();
boolean grantAccess = false;
File f = new File("D:\Apps\Esclipse\Assignment\src\application\UserData.txt");
try {
Scanner read = new Scanner(f);
int noOfLines=0;
while(read.hasNextLine()){
noOfLines++;
}

for(int i=0; i<noOfLines; i++){
if(read.nextLine().equals(username)){
i++;
if(read.nextLine().equals(password)){
grantAccess=true;
read.close();
break;
}
}
}
if(grantAccess){

Parent root1 = FXMLLoader.load(getClass().getResource("UserGUI.fxml"));
Scene scene1 = new Scene(root1, 750, 600);
Stage stage1 = (Stage) ((Node) event1.getSource()).getScene().getWindow();

stage1.setTitle(" Register ");
stage1.setScene(scene1);
stage1.show();

}
else{
System.exit(0);
}
}
catch (FileNotFoundException e) {

e.printStackTrace();
}
}





@FXML
private void setOnRegister(ActionEvent event) throws IOException {

Parent root = FXMLLoader.load(getClass().getResource("RegisterGUI.fxml"));
Scene scene = new Scene(root, 750, 600);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

stage.setTitle(" Register ");
stage.setScene(scene);
stage.show();

}

}


Register code :



import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class RegisterButton {

public String Email, Username, Password, Name, IC, Phone, Address;

@FXML
private TextField txtfEmailR, txtfUsernameR, txtfPasswordR ,txtfNameR, txtfICR, txtfPhoneR;

@FXML
private TextArea txtaAddressR;

@FXML
private Button btnRegisterR;

@FXML
private Button btnExit;

@FXML
private Button btnBack;


@FXML
private void OnBack(ActionEvent event) throws IOException {

Parent root = FXMLLoader.load(getClass().getResource("LoginGUI.fxml"));
Scene scene = new Scene(root, 750, 600);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

stage.setTitle(" Welcome ");
stage.setScene(scene);
stage.show();

}


@FXML
void OnExit(ActionEvent event) {

Platform.exit();

}

@FXML
void OnRegister(ActionEvent e) {

String line = txtfEmailR.getText() +txtfUsernameR.getText() +txtfPasswordR.getText() +txtfNameR.getText() +txtfICR.getText() +txtfPhoneR.getText() +txtaAddressR.getText() +"n";

FileWriter file_writer;
try {
file_writer = new FileWriter("D:\Apps\Esclipse\Assignment\src\application\UserData");
BufferedWriter buffered_Writer = new BufferedWriter(file_writer);
buffered_Writer.write(line);
buffered_Writer.flush();
buffered_Writer.close();

System.out.println("Registed.");
System.exit(0);

}catch (IOException E) {
System.out.print("Error is ");
}



}
}









share|improve this question













marked as duplicate by fabian java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 18:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    0
    down vote

    favorite













    This question already has an answer here:




    • Scanner on text file hasNext() is infinite

      4 answers




    I can't figure out what is the problem, I need you pros to help me with this please. I need it to login by reading their username and password in text file, and load those user information into the text field that store in the text file too. For now my code problem is when I click on Login nothing happen.



    Here's my text file information :



    jack123
    jackabc
    jc@gmail.com
    Jack
    123123123
    012344567
    no1,taman abc.


    And here is my Login code :



    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.EventObject;
    import java.util.Scanner;

    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Node;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Alert;
    import javafx.scene.control.Alert.AlertType;
    import javafx.scene.control.Button;
    import javafx.scene.control.TextField;
    import javafx.scene.input.MouseEvent;
    import javafx.stage.Stage;

    public class LoginButton {


    @FXML
    private TextField txtfUsername;

    @FXML
    private TextField txtfPassword;

    @FXML
    private Button btnLogin;

    @FXML
    private Button btnRegister;

    @FXML
    void setOnLogin(ActionEvent event1) throws IOException{

    String username = txtfUsername.getText();
    String password = txtfPassword.getText();
    boolean grantAccess = false;
    File f = new File("D:\Apps\Esclipse\Assignment\src\application\UserData.txt");
    try {
    Scanner read = new Scanner(f);
    int noOfLines=0;
    while(read.hasNextLine()){
    noOfLines++;
    }

    for(int i=0; i<noOfLines; i++){
    if(read.nextLine().equals(username)){
    i++;
    if(read.nextLine().equals(password)){
    grantAccess=true;
    read.close();
    break;
    }
    }
    }
    if(grantAccess){

    Parent root1 = FXMLLoader.load(getClass().getResource("UserGUI.fxml"));
    Scene scene1 = new Scene(root1, 750, 600);
    Stage stage1 = (Stage) ((Node) event1.getSource()).getScene().getWindow();

    stage1.setTitle(" Register ");
    stage1.setScene(scene1);
    stage1.show();

    }
    else{
    System.exit(0);
    }
    }
    catch (FileNotFoundException e) {

    e.printStackTrace();
    }
    }





    @FXML
    private void setOnRegister(ActionEvent event) throws IOException {

    Parent root = FXMLLoader.load(getClass().getResource("RegisterGUI.fxml"));
    Scene scene = new Scene(root, 750, 600);
    Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

    stage.setTitle(" Register ");
    stage.setScene(scene);
    stage.show();

    }

    }


    Register code :



    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;

    import javafx.application.Platform;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Node;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.TextArea;
    import javafx.scene.control.TextField;
    import javafx.stage.Stage;

    public class RegisterButton {

    public String Email, Username, Password, Name, IC, Phone, Address;

    @FXML
    private TextField txtfEmailR, txtfUsernameR, txtfPasswordR ,txtfNameR, txtfICR, txtfPhoneR;

    @FXML
    private TextArea txtaAddressR;

    @FXML
    private Button btnRegisterR;

    @FXML
    private Button btnExit;

    @FXML
    private Button btnBack;


    @FXML
    private void OnBack(ActionEvent event) throws IOException {

    Parent root = FXMLLoader.load(getClass().getResource("LoginGUI.fxml"));
    Scene scene = new Scene(root, 750, 600);
    Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

    stage.setTitle(" Welcome ");
    stage.setScene(scene);
    stage.show();

    }


    @FXML
    void OnExit(ActionEvent event) {

    Platform.exit();

    }

    @FXML
    void OnRegister(ActionEvent e) {

    String line = txtfEmailR.getText() +txtfUsernameR.getText() +txtfPasswordR.getText() +txtfNameR.getText() +txtfICR.getText() +txtfPhoneR.getText() +txtaAddressR.getText() +"n";

    FileWriter file_writer;
    try {
    file_writer = new FileWriter("D:\Apps\Esclipse\Assignment\src\application\UserData");
    BufferedWriter buffered_Writer = new BufferedWriter(file_writer);
    buffered_Writer.write(line);
    buffered_Writer.flush();
    buffered_Writer.close();

    System.out.println("Registed.");
    System.exit(0);

    }catch (IOException E) {
    System.out.print("Error is ");
    }



    }
    }









    share|improve this question













    marked as duplicate by fabian java
    Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 8 at 18:58


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:




      • Scanner on text file hasNext() is infinite

        4 answers




      I can't figure out what is the problem, I need you pros to help me with this please. I need it to login by reading their username and password in text file, and load those user information into the text field that store in the text file too. For now my code problem is when I click on Login nothing happen.



      Here's my text file information :



      jack123
      jackabc
      jc@gmail.com
      Jack
      123123123
      012344567
      no1,taman abc.


      And here is my Login code :



      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Path;
      import java.nio.file.Paths;
      import java.util.EventObject;
      import java.util.Scanner;

      import javafx.event.ActionEvent;
      import javafx.fxml.FXML;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.Node;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.Alert;
      import javafx.scene.control.Alert.AlertType;
      import javafx.scene.control.Button;
      import javafx.scene.control.TextField;
      import javafx.scene.input.MouseEvent;
      import javafx.stage.Stage;

      public class LoginButton {


      @FXML
      private TextField txtfUsername;

      @FXML
      private TextField txtfPassword;

      @FXML
      private Button btnLogin;

      @FXML
      private Button btnRegister;

      @FXML
      void setOnLogin(ActionEvent event1) throws IOException{

      String username = txtfUsername.getText();
      String password = txtfPassword.getText();
      boolean grantAccess = false;
      File f = new File("D:\Apps\Esclipse\Assignment\src\application\UserData.txt");
      try {
      Scanner read = new Scanner(f);
      int noOfLines=0;
      while(read.hasNextLine()){
      noOfLines++;
      }

      for(int i=0; i<noOfLines; i++){
      if(read.nextLine().equals(username)){
      i++;
      if(read.nextLine().equals(password)){
      grantAccess=true;
      read.close();
      break;
      }
      }
      }
      if(grantAccess){

      Parent root1 = FXMLLoader.load(getClass().getResource("UserGUI.fxml"));
      Scene scene1 = new Scene(root1, 750, 600);
      Stage stage1 = (Stage) ((Node) event1.getSource()).getScene().getWindow();

      stage1.setTitle(" Register ");
      stage1.setScene(scene1);
      stage1.show();

      }
      else{
      System.exit(0);
      }
      }
      catch (FileNotFoundException e) {

      e.printStackTrace();
      }
      }





      @FXML
      private void setOnRegister(ActionEvent event) throws IOException {

      Parent root = FXMLLoader.load(getClass().getResource("RegisterGUI.fxml"));
      Scene scene = new Scene(root, 750, 600);
      Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

      stage.setTitle(" Register ");
      stage.setScene(scene);
      stage.show();

      }

      }


      Register code :



      import java.io.BufferedWriter;
      import java.io.FileWriter;
      import java.io.IOException;

      import javafx.application.Platform;
      import javafx.event.ActionEvent;
      import javafx.fxml.FXML;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.Node;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.TextArea;
      import javafx.scene.control.TextField;
      import javafx.stage.Stage;

      public class RegisterButton {

      public String Email, Username, Password, Name, IC, Phone, Address;

      @FXML
      private TextField txtfEmailR, txtfUsernameR, txtfPasswordR ,txtfNameR, txtfICR, txtfPhoneR;

      @FXML
      private TextArea txtaAddressR;

      @FXML
      private Button btnRegisterR;

      @FXML
      private Button btnExit;

      @FXML
      private Button btnBack;


      @FXML
      private void OnBack(ActionEvent event) throws IOException {

      Parent root = FXMLLoader.load(getClass().getResource("LoginGUI.fxml"));
      Scene scene = new Scene(root, 750, 600);
      Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

      stage.setTitle(" Welcome ");
      stage.setScene(scene);
      stage.show();

      }


      @FXML
      void OnExit(ActionEvent event) {

      Platform.exit();

      }

      @FXML
      void OnRegister(ActionEvent e) {

      String line = txtfEmailR.getText() +txtfUsernameR.getText() +txtfPasswordR.getText() +txtfNameR.getText() +txtfICR.getText() +txtfPhoneR.getText() +txtaAddressR.getText() +"n";

      FileWriter file_writer;
      try {
      file_writer = new FileWriter("D:\Apps\Esclipse\Assignment\src\application\UserData");
      BufferedWriter buffered_Writer = new BufferedWriter(file_writer);
      buffered_Writer.write(line);
      buffered_Writer.flush();
      buffered_Writer.close();

      System.out.println("Registed.");
      System.exit(0);

      }catch (IOException E) {
      System.out.print("Error is ");
      }



      }
      }









      share|improve this question














      This question already has an answer here:




      • Scanner on text file hasNext() is infinite

        4 answers




      I can't figure out what is the problem, I need you pros to help me with this please. I need it to login by reading their username and password in text file, and load those user information into the text field that store in the text file too. For now my code problem is when I click on Login nothing happen.



      Here's my text file information :



      jack123
      jackabc
      jc@gmail.com
      Jack
      123123123
      012344567
      no1,taman abc.


      And here is my Login code :



      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Path;
      import java.nio.file.Paths;
      import java.util.EventObject;
      import java.util.Scanner;

      import javafx.event.ActionEvent;
      import javafx.fxml.FXML;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.Node;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.Alert;
      import javafx.scene.control.Alert.AlertType;
      import javafx.scene.control.Button;
      import javafx.scene.control.TextField;
      import javafx.scene.input.MouseEvent;
      import javafx.stage.Stage;

      public class LoginButton {


      @FXML
      private TextField txtfUsername;

      @FXML
      private TextField txtfPassword;

      @FXML
      private Button btnLogin;

      @FXML
      private Button btnRegister;

      @FXML
      void setOnLogin(ActionEvent event1) throws IOException{

      String username = txtfUsername.getText();
      String password = txtfPassword.getText();
      boolean grantAccess = false;
      File f = new File("D:\Apps\Esclipse\Assignment\src\application\UserData.txt");
      try {
      Scanner read = new Scanner(f);
      int noOfLines=0;
      while(read.hasNextLine()){
      noOfLines++;
      }

      for(int i=0; i<noOfLines; i++){
      if(read.nextLine().equals(username)){
      i++;
      if(read.nextLine().equals(password)){
      grantAccess=true;
      read.close();
      break;
      }
      }
      }
      if(grantAccess){

      Parent root1 = FXMLLoader.load(getClass().getResource("UserGUI.fxml"));
      Scene scene1 = new Scene(root1, 750, 600);
      Stage stage1 = (Stage) ((Node) event1.getSource()).getScene().getWindow();

      stage1.setTitle(" Register ");
      stage1.setScene(scene1);
      stage1.show();

      }
      else{
      System.exit(0);
      }
      }
      catch (FileNotFoundException e) {

      e.printStackTrace();
      }
      }





      @FXML
      private void setOnRegister(ActionEvent event) throws IOException {

      Parent root = FXMLLoader.load(getClass().getResource("RegisterGUI.fxml"));
      Scene scene = new Scene(root, 750, 600);
      Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

      stage.setTitle(" Register ");
      stage.setScene(scene);
      stage.show();

      }

      }


      Register code :



      import java.io.BufferedWriter;
      import java.io.FileWriter;
      import java.io.IOException;

      import javafx.application.Platform;
      import javafx.event.ActionEvent;
      import javafx.fxml.FXML;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.Node;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.TextArea;
      import javafx.scene.control.TextField;
      import javafx.stage.Stage;

      public class RegisterButton {

      public String Email, Username, Password, Name, IC, Phone, Address;

      @FXML
      private TextField txtfEmailR, txtfUsernameR, txtfPasswordR ,txtfNameR, txtfICR, txtfPhoneR;

      @FXML
      private TextArea txtaAddressR;

      @FXML
      private Button btnRegisterR;

      @FXML
      private Button btnExit;

      @FXML
      private Button btnBack;


      @FXML
      private void OnBack(ActionEvent event) throws IOException {

      Parent root = FXMLLoader.load(getClass().getResource("LoginGUI.fxml"));
      Scene scene = new Scene(root, 750, 600);
      Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

      stage.setTitle(" Welcome ");
      stage.setScene(scene);
      stage.show();

      }


      @FXML
      void OnExit(ActionEvent event) {

      Platform.exit();

      }

      @FXML
      void OnRegister(ActionEvent e) {

      String line = txtfEmailR.getText() +txtfUsernameR.getText() +txtfPasswordR.getText() +txtfNameR.getText() +txtfICR.getText() +txtfPhoneR.getText() +txtaAddressR.getText() +"n";

      FileWriter file_writer;
      try {
      file_writer = new FileWriter("D:\Apps\Esclipse\Assignment\src\application\UserData");
      BufferedWriter buffered_Writer = new BufferedWriter(file_writer);
      buffered_Writer.write(line);
      buffered_Writer.flush();
      buffered_Writer.close();

      System.out.println("Registed.");
      System.exit(0);

      }catch (IOException E) {
      System.out.print("Error is ");
      }



      }
      }




      This question already has an answer here:




      • Scanner on text file hasNext() is infinite

        4 answers








      java javafx fxml






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 18:41









      Jack

      13




      13




      marked as duplicate by fabian java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 8 at 18:58


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by fabian java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 8 at 18:58


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          I am guessing you are in an infinite while loop since you are calling read.hasNextLine(), but you aren't actually reading the line, so you're stuck at the first line. You will probably have to put a readLine() in your while loop and then go back to the beginning again to parse the lines.



          Edit:



          After reading your code, I don't really see a reason for you to have the number of lines, so you could do everything in your first while loop like this:



                  Scanner read = new Scanner(f); 
          //int noOfLines=0;
          while(read.hasNextLine()){
          if(read.nextLine().equals(username)){
          if(read.nextLine().equals(password)){
          grantAccess=true;
          read.close();
          break;
          }
          }
          }


          but if you need to know the number of lines for some reason, you could do it like this:



                  Scanner read = new Scanner(f); 
          int noOfLines=0;
          while(read.hasNextLine()){
          read.nextLine();
          noOfLines++;
          }
          read.close();
          read = new Scanner(f);
          for(int i=0; i<noOfLines; i++){
          if(read.nextLine().equals(username)){
          i++;
          if(read.nextLine().equals(password)){
          grantAccess=true;
          read.close();
          break;
          }
          }
          }


          Also, a potential issue I can see with your code is you might get incorrect results if someone's password is someone else's username.






          share|improve this answer























          • Something like this? try { BufferedReader read = new BufferedReader(new FileReader("D:\Apps\Esclipse\Assignment\src\application\UserData.txt")); int noOfLines=0; while(read.readLine() == null){ noOfLines++; }
            – Jack
            Nov 9 at 16:42










          • I've edited my post with a couple of possible solutions
            – Justin
            Nov 9 at 18:38










          • Okay! fixed Thank you! can I ask another question hope you could help me, after the login I need the rest of the data in text file to load into the User profile GUI. Any idea?
            – Jack
            Nov 10 at 16:55










          • When you find the correct username and password, you could keep reading more lines and store those values in variables and display them where you want.
            – Justin
            Nov 13 at 18:51










          • How to store the rest of the lines in text file and call to display in another class?
            – Jack
            Nov 14 at 1:28


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          I am guessing you are in an infinite while loop since you are calling read.hasNextLine(), but you aren't actually reading the line, so you're stuck at the first line. You will probably have to put a readLine() in your while loop and then go back to the beginning again to parse the lines.



          Edit:



          After reading your code, I don't really see a reason for you to have the number of lines, so you could do everything in your first while loop like this:



                  Scanner read = new Scanner(f); 
          //int noOfLines=0;
          while(read.hasNextLine()){
          if(read.nextLine().equals(username)){
          if(read.nextLine().equals(password)){
          grantAccess=true;
          read.close();
          break;
          }
          }
          }


          but if you need to know the number of lines for some reason, you could do it like this:



                  Scanner read = new Scanner(f); 
          int noOfLines=0;
          while(read.hasNextLine()){
          read.nextLine();
          noOfLines++;
          }
          read.close();
          read = new Scanner(f);
          for(int i=0; i<noOfLines; i++){
          if(read.nextLine().equals(username)){
          i++;
          if(read.nextLine().equals(password)){
          grantAccess=true;
          read.close();
          break;
          }
          }
          }


          Also, a potential issue I can see with your code is you might get incorrect results if someone's password is someone else's username.






          share|improve this answer























          • Something like this? try { BufferedReader read = new BufferedReader(new FileReader("D:\Apps\Esclipse\Assignment\src\application\UserData.txt")); int noOfLines=0; while(read.readLine() == null){ noOfLines++; }
            – Jack
            Nov 9 at 16:42










          • I've edited my post with a couple of possible solutions
            – Justin
            Nov 9 at 18:38










          • Okay! fixed Thank you! can I ask another question hope you could help me, after the login I need the rest of the data in text file to load into the User profile GUI. Any idea?
            – Jack
            Nov 10 at 16:55










          • When you find the correct username and password, you could keep reading more lines and store those values in variables and display them where you want.
            – Justin
            Nov 13 at 18:51










          • How to store the rest of the lines in text file and call to display in another class?
            – Jack
            Nov 14 at 1:28















          up vote
          0
          down vote



          accepted










          I am guessing you are in an infinite while loop since you are calling read.hasNextLine(), but you aren't actually reading the line, so you're stuck at the first line. You will probably have to put a readLine() in your while loop and then go back to the beginning again to parse the lines.



          Edit:



          After reading your code, I don't really see a reason for you to have the number of lines, so you could do everything in your first while loop like this:



                  Scanner read = new Scanner(f); 
          //int noOfLines=0;
          while(read.hasNextLine()){
          if(read.nextLine().equals(username)){
          if(read.nextLine().equals(password)){
          grantAccess=true;
          read.close();
          break;
          }
          }
          }


          but if you need to know the number of lines for some reason, you could do it like this:



                  Scanner read = new Scanner(f); 
          int noOfLines=0;
          while(read.hasNextLine()){
          read.nextLine();
          noOfLines++;
          }
          read.close();
          read = new Scanner(f);
          for(int i=0; i<noOfLines; i++){
          if(read.nextLine().equals(username)){
          i++;
          if(read.nextLine().equals(password)){
          grantAccess=true;
          read.close();
          break;
          }
          }
          }


          Also, a potential issue I can see with your code is you might get incorrect results if someone's password is someone else's username.






          share|improve this answer























          • Something like this? try { BufferedReader read = new BufferedReader(new FileReader("D:\Apps\Esclipse\Assignment\src\application\UserData.txt")); int noOfLines=0; while(read.readLine() == null){ noOfLines++; }
            – Jack
            Nov 9 at 16:42










          • I've edited my post with a couple of possible solutions
            – Justin
            Nov 9 at 18:38










          • Okay! fixed Thank you! can I ask another question hope you could help me, after the login I need the rest of the data in text file to load into the User profile GUI. Any idea?
            – Jack
            Nov 10 at 16:55










          • When you find the correct username and password, you could keep reading more lines and store those values in variables and display them where you want.
            – Justin
            Nov 13 at 18:51










          • How to store the rest of the lines in text file and call to display in another class?
            – Jack
            Nov 14 at 1:28













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          I am guessing you are in an infinite while loop since you are calling read.hasNextLine(), but you aren't actually reading the line, so you're stuck at the first line. You will probably have to put a readLine() in your while loop and then go back to the beginning again to parse the lines.



          Edit:



          After reading your code, I don't really see a reason for you to have the number of lines, so you could do everything in your first while loop like this:



                  Scanner read = new Scanner(f); 
          //int noOfLines=0;
          while(read.hasNextLine()){
          if(read.nextLine().equals(username)){
          if(read.nextLine().equals(password)){
          grantAccess=true;
          read.close();
          break;
          }
          }
          }


          but if you need to know the number of lines for some reason, you could do it like this:



                  Scanner read = new Scanner(f); 
          int noOfLines=0;
          while(read.hasNextLine()){
          read.nextLine();
          noOfLines++;
          }
          read.close();
          read = new Scanner(f);
          for(int i=0; i<noOfLines; i++){
          if(read.nextLine().equals(username)){
          i++;
          if(read.nextLine().equals(password)){
          grantAccess=true;
          read.close();
          break;
          }
          }
          }


          Also, a potential issue I can see with your code is you might get incorrect results if someone's password is someone else's username.






          share|improve this answer














          I am guessing you are in an infinite while loop since you are calling read.hasNextLine(), but you aren't actually reading the line, so you're stuck at the first line. You will probably have to put a readLine() in your while loop and then go back to the beginning again to parse the lines.



          Edit:



          After reading your code, I don't really see a reason for you to have the number of lines, so you could do everything in your first while loop like this:



                  Scanner read = new Scanner(f); 
          //int noOfLines=0;
          while(read.hasNextLine()){
          if(read.nextLine().equals(username)){
          if(read.nextLine().equals(password)){
          grantAccess=true;
          read.close();
          break;
          }
          }
          }


          but if you need to know the number of lines for some reason, you could do it like this:



                  Scanner read = new Scanner(f); 
          int noOfLines=0;
          while(read.hasNextLine()){
          read.nextLine();
          noOfLines++;
          }
          read.close();
          read = new Scanner(f);
          for(int i=0; i<noOfLines; i++){
          if(read.nextLine().equals(username)){
          i++;
          if(read.nextLine().equals(password)){
          grantAccess=true;
          read.close();
          break;
          }
          }
          }


          Also, a potential issue I can see with your code is you might get incorrect results if someone's password is someone else's username.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 18:38

























          answered Nov 8 at 18:53









          Justin

          51128




          51128












          • Something like this? try { BufferedReader read = new BufferedReader(new FileReader("D:\Apps\Esclipse\Assignment\src\application\UserData.txt")); int noOfLines=0; while(read.readLine() == null){ noOfLines++; }
            – Jack
            Nov 9 at 16:42










          • I've edited my post with a couple of possible solutions
            – Justin
            Nov 9 at 18:38










          • Okay! fixed Thank you! can I ask another question hope you could help me, after the login I need the rest of the data in text file to load into the User profile GUI. Any idea?
            – Jack
            Nov 10 at 16:55










          • When you find the correct username and password, you could keep reading more lines and store those values in variables and display them where you want.
            – Justin
            Nov 13 at 18:51










          • How to store the rest of the lines in text file and call to display in another class?
            – Jack
            Nov 14 at 1:28


















          • Something like this? try { BufferedReader read = new BufferedReader(new FileReader("D:\Apps\Esclipse\Assignment\src\application\UserData.txt")); int noOfLines=0; while(read.readLine() == null){ noOfLines++; }
            – Jack
            Nov 9 at 16:42










          • I've edited my post with a couple of possible solutions
            – Justin
            Nov 9 at 18:38










          • Okay! fixed Thank you! can I ask another question hope you could help me, after the login I need the rest of the data in text file to load into the User profile GUI. Any idea?
            – Jack
            Nov 10 at 16:55










          • When you find the correct username and password, you could keep reading more lines and store those values in variables and display them where you want.
            – Justin
            Nov 13 at 18:51










          • How to store the rest of the lines in text file and call to display in another class?
            – Jack
            Nov 14 at 1:28
















          Something like this? try { BufferedReader read = new BufferedReader(new FileReader("D:\Apps\Esclipse\Assignment\src\application\UserData.txt")); int noOfLines=0; while(read.readLine() == null){ noOfLines++; }
          – Jack
          Nov 9 at 16:42




          Something like this? try { BufferedReader read = new BufferedReader(new FileReader("D:\Apps\Esclipse\Assignment\src\application\UserData.txt")); int noOfLines=0; while(read.readLine() == null){ noOfLines++; }
          – Jack
          Nov 9 at 16:42












          I've edited my post with a couple of possible solutions
          – Justin
          Nov 9 at 18:38




          I've edited my post with a couple of possible solutions
          – Justin
          Nov 9 at 18:38












          Okay! fixed Thank you! can I ask another question hope you could help me, after the login I need the rest of the data in text file to load into the User profile GUI. Any idea?
          – Jack
          Nov 10 at 16:55




          Okay! fixed Thank you! can I ask another question hope you could help me, after the login I need the rest of the data in text file to load into the User profile GUI. Any idea?
          – Jack
          Nov 10 at 16:55












          When you find the correct username and password, you could keep reading more lines and store those values in variables and display them where you want.
          – Justin
          Nov 13 at 18:51




          When you find the correct username and password, you could keep reading more lines and store those values in variables and display them where you want.
          – Justin
          Nov 13 at 18:51












          How to store the rest of the lines in text file and call to display in another class?
          – Jack
          Nov 14 at 1:28




          How to store the rest of the lines in text file and call to display in another class?
          – Jack
          Nov 14 at 1:28



          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini