well, first of all, i hope you are not expecting anyone here to do it for you, but what you have to do is basically something like this:
public class Quiz {
public static void main(String[] args) {
string readString = ReadInput();
System.out.println(readString);
StoreToFile(readString);
string readFromFileString = ReadFromFile();
System.out.println(readFromFileString);
}
private string ReadInput()
{ BufferedReader br = null;
String inputString = null;
System.out.print("User input ");
try {
br = new BufferedReader(new InputStreamReader(System.in));
inputString = br.readLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return inputString;
}
private void StoreToFile(string inputString)
{
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("data.dat"));
bw.write(inputString);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private string ReadFromFile()
{
BufferedReader br = null;
String inputString = null;
try {
br = new BufferedReader(new FileReader("data.dat"));
inputString = br.readLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return inputString;
}
}
and as i said before, im not going to do it for you, but this should give you a very clear idea what you need to do, those are just a quick edit of the code that you posted, so im not sure it can even be compiled, and it could be implemented better, but the general idea of what you have to do is:
- a method that read user input from keyboard and return a string
- a method that takes a string as a parameter and write it to a file
- a method that read input from a file and return it
- and finally call the methods from your main
p.s: just wondering, did u learnt about exception before you understand method? i see you are using the try/catch blocks pretty often, u should be able to do this easily..