2015-11-05 91 views
-3

我正在处理类项目的密码登录部分。没有什么花哨。用户或角色将是一个int并且密码是一个String。我现在只是使用简单的加密。我遇到的问题是在读取文件时遇到输入不匹配。过去我做了类似的事情,需要我阅读整数和字符串,并没有任何问题。但我无法弄清楚在这种情况下出了什么问题。任何帮助,为什么我得到这个错误将不胜感激。我正在使用while(inputStream.hasNextLine()),然后阅读int,然后String我试过hasNextInthasNext,并一直得到相同的错误。从txt文件加载int和加密的字符串

public void readFile(){ 
    Scanner inputStream = null; 
    try { 
     inputStream = new Scanner (new FileInputStream("login.txt")); 
    }catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    if(inputStream != null){ 
    while (inputStream.hasNextLine()){ 
     int luser = inputStream.nextInt(); 
     String lpass = inputStream.nextLine(); 
     newFile[count] = new accessNode(luser, lpass); 
     count ++; 
    } 
    inputStream.close(); 
    }  
} 
+0

你需要上传你要得到很好的帮助实际的错误 - I,E,实际的错误消息,说明该线失败和堆栈跟踪。 –

回答

1

尝试阅读它作为一个字符串和字符串转换为一个int

while (inputStream.hasNextLine()) { 

    Integer luser = Integer.parseInt(inputStream.nextLine()); 
    String lpass = inputStream.nextLine(); 
    newFile[count] = new accessNode(luser, lpass); 
    count++; 
} 

但是,你需要确保你的文件有确切格式的数据如下

12342 
password 
1

很难说不知道你得到了什么错误,但我的猜测是,这是因为你没有阅读整个文件。

您的文件可能是这样的:

1\r\n 
password\r\n 

当你调用nextInt(),它读取INT,但不会提前过去的第一个\ r \ n所以,当你调用nextLine()读取到行的末尾,所以你得到的是\ r \ n。您需要阅读第一个\ r \ n,然后阅读密码。

尝试

int luser = inputStream.nextInt(); 
inputStream.nextLine(); 
String lpass = inputStream.nextLine(); 
newFile[count] = new accessNode(luser, lpass);