首先,我必须道歉,因为有这么多问题,但我不太明白。我正在学习Java教程,但我被困在这里。
如何将用户的输入存储到文本文件中?
这是我的代码。
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class UserInput {
public static void main(String[] args) {
ArrayList<String> fullName = new ArrayList<String>();
ArrayList<String> userId = new ArrayList<String>();
ArrayList<String> qA = new ArrayList<String>();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Please enter your Full Name: ");
fullName.add(sc.nextLine());
System.out.println("Please enter your ID: ");
userId.add(sc.nextLine());
System.out.println("What is your race/ethinicity? A. Hispanic/Latino, B. Asian, C. Caucasian, D. African American: ");
qA.add(sc.nextLine());
System.out.println("What is your preferred language? A. Spanish, B. English, C. Portugues, D. French: ");
qA.add(sc.nextLine());
System.out.println("What is your political affiliation? A. Republican, B. Democrat, C. Green, D. None: ");
qA.add(sc.nextLine());
System.out.println("What is your religious practice? A. Christian, B. Islam, C. Judaism, D. Hinduism: ");
qA.add(sc.nextLine());
System.out.println("What is your sexual orientation? A. Homosexual, B. Heterosexual, C. Bisexual, D. Asexual: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite ice cream? A. Vanilla, B. Chocolate, C. Banana, D. Gum: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite activity? A. Running, B. Walking, C. Sleeping, D. Eating: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite color? A. Burgundy, B. Black, C. Gold, D. Blue: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite clothing item? A. Shoes, B. Hats, C. Shirts, D. Rings: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite pet? A. Dog, B. Cat, C. Tiger, D. Turtle: ");
qA.add(sc.nextLine());
Random random = new Random();
for (int i =0; i < 1; i++) {
int answerScore = random.nextInt(10) + 1;
System.out.println("Test Score " + answerScore + "/10");
}
break;
}
}
}
我想知道如何将用户的姓名,ID和答案存储到单独的.txt文件中。
提前致谢!
答案 0 :(得分:2)
public void saveData(ArrayList<String>fullName){
try {
File newTextFile = new File("D:/textfile.txt");//path of the file
FileWriter fw = new FileWriter(newTextFile);
for(String str: fullName) {
fw.write(str);
}
fw.close();
} catch (IOException io) {
io.printStackTrace();
}
答案 1 :(得分:0)
有关如何将ArrayList<String>
写入文本文件,请参阅https://stackoverflow.com/a/6548204。此外,我认为fullName
和userId
可以是简单Strings
,因为它们只存储一个值?
所以在你的情况下:
//get values ...
java.io.FileWriter writer = new java.io.FileWriter("output.txt");
writer.write(fullName);
writer.write(userId);
for(String str: qA) {
writer.write(str);
}
writer.close();
如果您将fullName
和userId
保持为ArrayList<String>
,请执行以下操作:
//get values ...
java.io.FileWriter writer = new java.io.FileWriter("output.txt");
writer.write(fullName.get(0));
writer.write(userId.get(0));
for(String str: qA) {
writer.write(str);
}
writer.close();
希望对你有所帮助。
答案 2 :(得分:0)
您对无限循环的使用是多余的,因为循环永远不会迭代。 相反,你可以使用以下
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class UserInput
{
String fullName;
String userId;
ArrayList<String> qA = new ArrayList<String>();
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Please enter your Full Name: ");
fullName = sc.nextLine();
System.out.println("Please enter your ID: ");
userId = sc.nextLine();
System.out.println("What is your race/ethinicity? A. Hispanic/Latino, B. Asian, C. Caucasian, D. African American: ");
qA.add(sc.nextLine());
System.out.println("What is your preferred language? A. Spanish, B. English, C. Portugues, D. French: ");
qA.add(sc.nextLine());
System.out.println("What is your political affiliation? A. Republican, B. Democrat, C. Green, D. None: ");
qA.add(sc.nextLine());
System.out.println("What is your religious practice? A. Christian, B. Islam, C. Judaism, D. Hinduism: ");
qA.add(sc.nextLine());
System.out.println("What is your sexual orientation? A. Homosexual, B. Heterosexual, C. Bisexual, D. Asexual: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite ice cream? A. Vanilla, B. Chocolate, C. Banana, D. Gum: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite activity? A. Running, B. Walking, C. Sleeping, D. Eating: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite color? A. Burgundy, B. Black, C. Gold, D. Blue: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite clothing item? A. Shoes, B. Hats, C. Shirts, D. Rings: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite pet? A. Dog, B. Cat, C. Tiger, D. Turtle: ");
qA.add(sc.nextLine());
Random random = new Random();
for (int i =0; i < 1; i++) {
int answerScore = random.nextInt(10) + 1;
System.out.println("Test Score " + answerScore + "/10");
}
boolean youWantToWrite = true;
if (youWantToWrite)
{
writeValuesToFile();
}
}
}
public static void writeValuesToFile()
{
java.io.File f = new java.io.File("FileName.txt");
try (PrintWriter out = new PrintWriter(f);)
{
out.println(fullName);
out.println(userId);
for (String s : qA)
{
out.println(s);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}