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