英文:
How to store multiple usernames in one textfile
问题
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class Userfile {
private static String name;
private static Scanner scanage;
private static Scanner scan;
public static void main(String[] args) throws IOException {
Random r = new Random();
int age;
String username;
char randomc = 2;
System.out.println("Please enter your name");
scan = new Scanner(System.in);
name = scan.nextLine();
System.out.println("Please enter your age");
scanage = new Scanner(System.in);
age = scanage.nextInt();
username = name.toLowerCase() + age;
String alphabet = "xy!&69£zHDErnABCabcFfGghIiJjKkLlMmNPp";
for (int i = 0; i < 1; i++) {
randomc = alphabet.charAt(r.nextInt(alphabet.length()));
String userId = username + randomc;
System.out.println("Your Username is " + userId);
}
FileWriter userid = new FileWriter("file path");
String userId = username + randomc;
userid.write("\n" + "Username: " + userId);
userid.close();
}
}
英文:
My program asks the user for their name and age. The username is their name and age plus a random character at the end of it. I want to store that username into a new text file but whenever I run the code it only writes one username at a time it doesn't include the one that I added before. Here is my code Please help me I am new.
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class Userfile {
private static String name;
private static Scanner scanage;
private static Scanner scan;
public static void main(String[] args) throws IOException {
Random r = new Random();
int age;
String username;
char randomc = 2;
System.out.println("Please enter your name");
scan = new Scanner(System.in);
name = scan.nextLine();
System.out.println("Please enter your age");
scanage = new Scanner(System.in);
age = scanage.nextInt();
username = name.toLowerCase()+age;
String alphabet = "xy!&69£zHDErnABCabcFfGghIiJjKkLlMmNPp";
for (int i = 0; i < 1; i++) {
randomc = alphabet.charAt(r.nextInt(alphabet.length()));
String userId = username + randomc;
System.out.println("Your Username is " +userId);
}
FileWriter userid = new FileWriter("file path");
String userId = username + randomc;
userid.write("\n" + "Username: " + userId);
userid.close();
}
}
答案1
得分: 0
如果您想要向一个之前已经写入过文本的文件中写入文本,您需要使用FileWriter(File file, boolean append)构造函数:
FileWriter userid = new FileWriter("文件路径", true); //假设"文件路径"是实际文件的路径
此外,您的程序仅会询问一次输入,所以如果您想要添加多个用户名,就需要多次运行它。或者您可以将您已完成的部分放入循环中,以多次询问输入。说到循环,您目前使用的循环没有实际意义,因为其中的语句只会执行一次,就像没有循环包裹它们时一样。
英文:
If you want to write text to a file to which you've already written text before, you need to use the FileWriter(File file,boolean append) constructor:
FileWriter userid = new FileWriter("file path", true); //assuming "file path" is the actual path to the file
Besides that, your program only asks for input once, so you'll need to run it multiple times if you want to add multiple usernames. Or you could wrap what you've done in a loop to ask for input multiple times. And speaking of loops, the one loop you do have serves no real purpose as the statements it executes will run once, just like they would without a loop wrapping them.
答案2
得分: 0
因为你每次都在覆盖文件。
将
FileWriter userid = new FileWriter("文件路径");
替换为
FileWriter userid = new FileWriter("文件路径", true);
英文:
It's because you are overriding the file everytime.
Replace
FileWriter userid = new FileWriter("file path");
with
FileWriter userid = new FileWriter("file path", true);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论