2017-04-26 45 views
0

首先,我必須道歉,因爲有這麼多類似的問題,但我不太明白。我正在學習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

嗨,歡迎來到StackOverflow。這不是一個代碼寫入服務,也不是一個教程服務。從你發佈的內容來看,你實際上沒有嘗試過任何東西。 Java教程涵蓋了這個主題 - 如果您遇到麻煩,請發佈您嘗試過的以及發生了什麼。請參閱[問]在這裏提出問題的指導。 – betseyb

回答

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(); 
     } 
    } 
} 
+0

錯誤「out.println(fullName);」 fullName無法解析爲變量。 – user7926396

+0

我將編輯我的代碼,我在那裏犯了一個錯誤。謝謝 – staa99

+0

我編輯過它。 – staa99

0

有關如何將ArrayList<String>寫入文本文件,請參閱https://stackoverflow.com/a/6548204。此外,我認爲fullNameuserId可以很簡單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(); 

如果你保持fullNameuserIdArrayList<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
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(); 
    } 
相關問題