【问题标题】:How to assign new users different text files如何为新用户分配不同的文本文件
【发布时间】:2013-04-12 17:25:02
【问题描述】:

我正在开发的程序会询问用户他们是否是新用户,如果用户是新用户,您必须为他们的个性化音乐数据库分配他们自己的 txt 文件。 txt 文件应标有用户名。我尝试使用int 变量来标记txt 文件,因为用户数未知。这是我到目前为止所做的:

System.out.println ("Are you a new user?(y/n)");

response = (stdin.readLine ());

if (response.equals ("Y"))
{
    System.out.println ("What is your name?");
    name [0] = (stdin.readLine ());

    BufferedWriter writer = new BufferedWriter (new FileWriter ("" + name + ".txt"));

    System.out.println ("How many songs would you like to enter?");
    number = Integer.parseInt (stdin.readLine ());

    for (int i = 0 ; i < number ; i++)
    {

        System.out.println ("What is the artist of the song you would like to add?");
        artist [i] = (stdin.readLine ());
        System.out.println ("What is the genre of the song you would like to add?");
        genre [i] = (stdin.readLine ());
        System.out.println ("What is the name of the song you would like to add?");
        songs [i] = (stdin.readLine ());
        count++;
    }


    for (int i = 0 ; i < number + count ; i++)
    {
        writer.write ("" + artist [i] + "");
        writer.newLine ();
        writer.write ("" + genre [i] + "");
        writer.newLine ();
        writer.write ("" + songs [i] + "");
        writer.newLine ();
    }
    writer.close ();

}
else if (response.equals ("y"))
{
    System.out.println ("What is your name?");
    name [0] = (stdin.readLine ());

    BufferedWriter writer = new BufferedWriter (new FileWriter ("" + name + ".txt"));

    System.out.println ("How many songs would you like to enter?");
    number = Integer.parseInt (stdin.readLine ());

    for (int i = 0 ; i < number ; i++)
    {

        System.out.println ("What is the artist of the song you would like to add?");
        artist [i] = (stdin.readLine ());
        System.out.println ("What is the genre of the song you would like to add?");
        genre [i] = (stdin.readLine ());
        System.out.println ("What is the name of the song you would like to add?");
        songs [i] = (stdin.readLine ());
        count++;
    }


    for (int i = 0 ; i < number + count ; i++)
    {
        writer.write ("" + artist [i] + "");
        writer.newLine ();
        writer.write ("" + genre [i] + "");
        writer.newLine ();
        writer.write ("" + songs [i] + "");
        writer.newLine ();
    }
    writer.close ();
}

【问题讨论】:

  • 所有用户名都是唯一的吗?
  • 问题是什么?
  • 第二个@EvanKnowles。此外,使用 try-catch 读取用户输入以优雅地处理异常。阅读它here
  • 这里有什么问题?
  • 复制粘贴是怎么回事?

标签: java arrays syntax bufferedreader filereader


【解决方案1】:

如果您想为新用户创建一个新文件,请像这样使用

 File file = new File(name+ ".txt");

    if(! file.exists())
    {
        file.createNewFile();
    }
    BufferedWriter writer = new BufferedWriter (new FileWriter (file));

也可以使用

response.equalsIgnoreCase("Y")

避免重复代码

【讨论】:

    猜你喜欢
    • 2021-07-15
    • 1970-01-01
    • 2017-04-21
    • 2014-09-26
    • 1970-01-01
    • 2021-10-24
    • 2012-05-12
    • 2016-11-06
    • 2015-08-22
    相关资源
    最近更新 更多