0

First off, I must apologize because there is so many questions like these but I don't quite get them. I'm taking Java tutorials but I'm stuck here.

How do I store the user's input into a text file?

Here is my 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;

    }
}

}

I would want to know how to store the user's name, ID, and answers to a separate .txt file.

Thanks in advance!

1
  • Hi, and welcome to StackOverflow. This is not a code-writing service, nor a tutorial service. From what you posted, you haven't actually tried anything. The Java Tutorials cover this subject - if you have trouble, post what you tried and what's going on. See How to Ask for guidance on asking questions here. Commented Apr 26, 2017 at 15:38

3 Answers 3

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Your usage of an indefinite loop is redundant because the loop never iterates. instead you could use the following

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

1 Comment

Error in "out.println(fullName);" fullName cannot be resolved to a variable.
0

See https://stackoverflow.com/a/6548204 for how to write an ArrayList<String> into a text file. Furthermore, I think fullName and userId can be simple Strings, since they only store one value?

So in your case:

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

If you keep fullName and userId as ArrayList<String>, do:

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

Hope that helps you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.