-2

I am trying make it so every time the program loops it saves into a file. Currently I have this:

import java.io.*;
import java.lang.System;
import java.util.Scanner; 

public class writing {



    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        Scanner input1 = new Scanner(System.in);

        String FileName = input.next();
        String x = input.next();

        for(int i=0; i<'x'; i++){

            System.out.println("Question");
            String MainQ = input1.nextLine();
            System.out.println("Option 1:  ");
            String Op1 = input1.nextLine();
            System.out.println("Option 2:  ");
            String Op2 = input1.nextLine();
            System.out.println("Option 3:  ");
            String Op3 = input1.nextLine();
            System.out.println("Correct Answer (Option Number:)  " );
            String An1 = input1.next();

            System.out.println("Quesiton 1:"+ MainQ);
            System.out.println(""+ Op1);
            System.out.println(""+ Op2);
            System.out.println(""+ Op3);
            String UAn1 = input1.next();
            if (UAn1 == An1){
                System.out.println("Incorrect");    
                System.out.println("Answer is:  " + An1);
            }else{  
                System.out.println("Correct");

            }

            try{

                FileWriter fw = new FileWriter(FileName + ".txt");
                PrintWriter pw = new PrintWriter(fw);

                pw.println(MainQ);
                pw.println(Op1);
                pw.println(Op2);
                pw.println(Op3);
                pw.println(An1);

                pw.close();

            } catch (IOException e){
                System.out.println("Error!");
            }

        }
    }
}

There isn't an error message, sorry for the bad question. I would like for every time the program loops for the text file to come out like this:

(Question 1) Question Example 
(Option 1) Option Example .... 

(Question 2) Question Example 
(Option 1) Option Example

... and so on. But the way it is working now it only records the last input

Coming out like this: (Imagine I use 3 for the third question and option number)

3
3
3
3
  • 3
    What exactly is the problem? Include any exceptions/error messages. – ryanyuyu Feb 03 '15 at 22:22
  • There isnt an error message, sorry for the bad question. I would like for every time the program loops for the text file to come out like this: (Question 1) *Question Example* (Option 1) *Option Example* .... (Question 2) *Question Example* (Option 1) *Option Example* and so on. But the way it is working now it only records the last input. – Deadliestrat Feb 03 '15 at 22:25
  • Well, if `filename` is always the same variable, the same file will always be created. What to do? – kolossus Feb 03 '15 at 22:26
  • So what is the problem? – user207421 Feb 03 '15 at 22:27
  • Format your code for better readability. – h7r Feb 03 '15 at 22:31
  • It's idiomatic for Java to have capitalized CamelCase class names and non capitalized variables. So the class should be `Writing` and `String FileName` should be `fileName`. – h7r Feb 03 '15 at 22:35
  • 2
    [Do **not** compare Strings with `==`.](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – PM 77-1 Feb 03 '15 at 22:38

2 Answers2

2

As you are creating the FileWriter inside the for loop it is always overwriting the content, therefore keeping apparently only the last content. You should move the lines

        FileWriter fw = new FileWriter(FileName + ".txt");
        PrintWriter pw = new PrintWriter(fw);

before the for statement (and try/catch block accoringly).

h7r
  • 4,944
  • 2
  • 28
  • 31
1

Open the FileWriter in append mode, otherwise it will just overwrite your text file instead of appending to it. See the FileWriter constructor documentation.

Also, as others have pointed out, camelCase is the convention for variable names in Java, not PascalCase, and you must not compare strings with == in Java.

Community
  • 1
  • 1
David Conrad
  • 15,432
  • 2
  • 42
  • 54