简体   繁体   English

从用户那里获取名称并将其推送到也是一个 .txt 文件的堆栈

[英]get name from the user and push it to a stack that is also a .txt file

Like the title says, I have a.txt file with user names (no white spaces but with all sorts of characters).正如标题所说,我有一个带有用户名的 .txt 文件(没有空格,但有各种字符)。 I want to implement it in a stack so users can manipulate it, add(push) new usernames, and pop(remove) already usernames.我想在堆栈中实现它,以便用户可以操作它、添加(推送)新用户名和弹出(删除)已有用户名。 I'm having problems with the push method, I want to prompt the user for the name and add it to the stack.我在使用 push 方法时遇到问题,我想提示用户输入名称并将其添加到堆栈中。 Should be in a method.应该在一个方法中。

enter code here 

    public static void Add_Username() {

    
    Scanner myScanner = new Scanner(System.in); 

    String line;
    Stack<String> stacks = new Stack<>();
    System.out.println("Please enter the username you'll like to add.");
    line = myScanner.nextLine();
    try {
          File myObj = new File("usernames.txt");
          Scanner myReader = new Scanner(myObj);
          
          while (myReader.hasNextLine()) {
                stacks.push(line);
            }
          myReader.close();
        } catch (FileNotFoundException e) {
          System.out.println("An error occurred.");
          e.printStackTrace();
        }
    
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM