所以我创建了一个用户名密码数据库,将其写入文件中:
import java.util.*;
import java.io.*;
import java.lang.*;
public class createfile {
private Formatter x;
public void openFile(){
try{
x = new Formatter("okey.fil");
}
catch(Exception e){
System.out.println("error get it right");
}
}
public void addRecords(){
Scanner input = new Scanner(System.in);
System.out.println("Enter number of records");
int num = input.nextInt();
for(int counter = 1; counter<=num; counter++){
String username = input.nextLine();
String email = input.nextLine();
String userpass = input.nextLine();
String emailpass = input.nextLine();
x.format("%s%s%s%s%s%s%s%s", username,"\t", email,"\t", userpass,"\t", emailpass, "\n");
}
}
public void closeFile(){
x.close();
}
}
public class Main {
public static void main(String[] args) {
createfile f = new createfile();
f.openFile();
f.addRecords();
f.closeFile();
}
}
问题是,它以错误的格式输出错误: 我将使用2作为记录数(num = 2) 并且字符串将与它所要求的相同(username = username,emailpass = emailpass ...) 这是输出:
username email userpass
emailpass username2 email2 userpass2