Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a JAVA program that asks the user to answer some questions. First, you sho

ID: 3804622 • Letter: W

Question

Write a JAVA program that asks the user to answer some questions. First, you should ask the user if they want to load answers from a previously saved file. If they respond yes you should look for the file name of your choosing in the path of your choosing and try to load that. If no file was previously saved or they answered no, the user must answer 8 questions of your choosing. They can be simple (i.e. What is your favorite color?) or they can be difficult (i.e. When the zombie apocalypse comes would you sacrifice your friend for your own survival if need be?).

In your file you should save the questions and the answers on the same line so it will look like this:

What is your favorite color? Blue

How old are you? 25

By doing this we can read in an entire line and simply separate the question based on the location of the question mark, anything before it and including it is the question and anything after it is the answer.

If the user wants to load previous questions and the file exists you should print out the question on one line and the answer on another.

Explanation / Answer

import java.io.*;

public class Main{

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Load from file? (Y/N)");
String ch = br.readLine().trim();
File f = new File("P:/qns.txt");
String data = "";
if (ch.toLowerCase().startsWith("y") && f.exists()) {
BufferedReader bf = new BufferedReader(new FileReader(f));
String q ;
for(int j=0;j<8;j++){
q = bf.readLine().trim();
int ind = 0;
for(int i=0;i<q.length();i++){
if(q.charAt(i)=='?'){
ind = i;
break;
}
}   
String s = q.substring(0,ind);
System.out.println(s);
String ans = br.readLine().trim();
data = data + s + "? " + ans + " ";
}
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write(data);
bw.flush();
bw.close();
}
else{
String qs[] = {"What is your favourite color","What is your favourite sport","Do you like game of thrones",
"What is your favourite subject","Are you a programmer","When the zombie apocalypse comes would you sacrifice your friend for your own survival if need be",
"What is your favourite movie","Who is your favourite cartoon character"};
data = "";
for(int i=0;i<qs.length;i++){
System.out.println(qs[i]+"?");
String ans = br.readLine().trim();
data = data + qs[i]+"? "+ans+" ";
}
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write(data);
bw.flush();
bw.close();
  
}

}
}

OUTPUT 1 :

Load from file? (Y/N)
N
What is your favourite color?
Blue
What is your favourite sport?
Cricket
Do you like game of thrones?
I love it
What is your favourite subject?
java
Are you a programmer?
ofcourse
When the zombie apocalypse comes would you sacrifice your friend for your own survival if need be?
qn too big
What is your favourite movie?
pulp fiction
Who is your favourite cartoon character?
kick buttowski

OUTPUT 2 :

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote