2015-09-18 259 views
0
  1. 我应该创建一个带有一些名称和PIN码的文本文件。 例如。彼得1212 约翰1234 玛丽0000 这些都是所谓的名称和针编号。使用扫描仪扫描txt文件

  2. 编写一个java程序,提示用户输入文件路径,名称和密码,并检查它是否有效。

  3. 如果正确的名称和针,打印出“登录成功”,如果失败,“登录失败 ”,如果密码包含非数值字符,“密码 包含非数字字符”。这是我迄今为止;

    import java.util.*; 
    import java.io.*; 
    public class PINCheck { 
        public static void main(String[]args) { 
    
         Scanner s = new Scanner(System.in); 
         System.out.print("Enter file path: "); 
         String filepath = s.nextLine(); 
    
         File passwordFile = new File(filepath); 
    
         System.out.print("Enter name: "); 
         String name = s.nextLine(); 
    
         System.out.print("Enter password: "); 
         String password = s.nextLine(); 
    
         if (password.matches(".*[a-zA-Z]+.*")) { 
          System.out.println("You have entered a non-numerical PIN!"); 
         } else { try { 
           Scanner sc = new Scanner(passwordFile); 
           if (sc.hasNext(name) && sc.hasNext(password)) { 
            System.out.println("You have logged in successfully."); 
           }else { 
            System.out.println("Login Failed."); 
           } 
          } catch (FileNotFoundException e) { 
           e.printStackTrace(); 
          } 
         } 
        } 
    } 
    
  4. 我能够编译我的代码并运行它,但即使是当我在正确的名称和引脚的按键我仍然有登录不成功。任何人都可以告诉我,如果我错误地使用hasNext? (我试了一下没有==真,按预期太不运行。)

回答

0

的代码应该是

import java.util.*; 
import java.io.*; 
public class PINCheck { 
public static void main(String[]args) { 

    Scanner s = new Scanner(System.in); 
    System.out.print("Enter file path: "); 
    String filepath = s.nextLine(); 

    File passwordFile = new File(filepath); 

    System.out.print("Enter name: "); 
    String name = s.nextLine(); 

    System.out.print("Enter password: "); 
    String password = s.nextLine(); 

    if (password.matches(".*[a-zA-Z]+.*")) { 
     System.out.println("You have entered a non-numerical PIN!"); 
    } else { 
     try { 
      Scanner sc = new Scanner(passwordFile); 
      while (sc.hasNext()){ 
       String str = sc.nextLine(); 
       System.out.println("Str="+str); 
       /* 
       StringTokenizer st = new StringTokenizer(str); 
       String uName = st.nextToken(); 
       String uPwd = st.nextToken(); 
       */ 
       String[] values = str.split(" "); 
       System.out.println("uname:upwd:"+values[0]+":"+values[1]); 
       if (name.equals(values[0]) && password.equals(values[1])) { 
        System.out.println("You have logged in successfully."); 
        break; 
       }else { 
        System.out.println("Login Failed."); 
       }    
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
} 
} 

} 

输出:

输入文件路径:数据.TXT

输入名称:拉维

输入密码:1234

uname:upwd:ravi:1234

您已成功登录。

我的data.txt文件的内容:拉维1234

注:==比较和变量的引用不变量内容。

您必须比较名称&来自文件的密码名称&从控制台输入密码输入。它应该是“等于”比较,它不应该检查变量的引用。

+0

试过了,它返回错误布尔值无法解除引用 – Clement

+0

更新了答案。它是next()而不是hasNext() –

+0

这返回了一个新的错误。找不到符号 – Clement

1

扫描器#hasNext(字符串图案)

在此字符串参数,模式 - 一个字符串,指定要扫描的模式,它不匹配的值。

你需要迭代文件内容,将用户名&密码与内容匹配。

Path path = Paths.get(filepath); 
try(Stream<String> lines = Files.lines(path)){ 
    Optional<String> hasUser = lines.filter(s -> s.split(" ")[0].equals(name) && s.split(" ")[1].equals(passowrd)).findFirst(); 
    if(hasUser.isPresent()){ 
     System.out.println("You have logged in successfully."); 
    }else { 
     System.out.println("Login Failed."); 
    } 
} 
+0

我完全不明白你的意思。对不起我是新来的java – Clement

+0

我说过,你需要通读文件并匹配名称和密码 –

+0

好的。它只适用于1.8版本的Java吗? –