Tout d'abord, je dois m'excuser car il y a tellement de questions comme celles-ci, mais je ne les comprends pas tout à fait. Je prends des tutoriels Java mais je suis coincé ici.

Comment stocker l'entrée de l'utilisateur dans un fichier texte?

Voici mon code.

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;

    }
}

}

Je voudrais savoir comment stocker le nom, l'identifiant et les réponses de l'utilisateur dans un fichier .txt séparé.

Merci d'avance!

0
user7926396 26 avril 2017 à 18:15

3 réponses

Meilleure réponse

Votre utilisation d'une boucle indéfinie est redondante car la boucle n'itère jamais. à la place, vous pouvez utiliser ce qui suit

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
staa99 30 avril 2017 à 08:42
 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();
    }
2
Anne 26 avril 2017 à 18:10

Consultez https://stackoverflow.com/a/6548204 pour savoir comment écrire un ArrayList<String> dans un fichier texte. De plus, je pense que fullName et userId peuvent être simples Strings, puisqu'ils ne stockent qu'une seule valeur?

Donc dans votre cas:

//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();

Si vous conservez fullName et userId en tant que ArrayList<String>, procédez comme suit:

//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();

J'espère que cela vous aide.

0
Community 23 mai 2017 à 12:26