Hello all. This is my first post in this forum. I am in the second semester of a college Java class, so I'm pretty new. I just finished my first project on I/O. Unfortunately, the professor in this class has a habit of making assignments without first presenting the material, so on a new topic I sometimes get a bit lost trying to scour the internet for information that is relevant to my level of experience. We don't have a regular textbook that we follow either, so I cannot look to that for help.
My issue now is that I just had a lab due (which Ive already submitted, so this is not cheating) that asked us to do the following:
Have a file containing a few lines of questions, one on each line, that can be answered in one word. Like this:
How old are you?
What is your name?
What is the weight of a cat on Veda?
Our program was supposed to display each line one by one and let the user type in an answer, saving the answer on the same line as the question.
How old are you? 5
What is your name? Merriwether
What is the weight of a cat on Veda? 83_vlerps
At startup, the program was supposed to offer options to either open and print an existing file with the questions already answered, or answer the questions in a file. The question and answer were supposed to be on the same line so they could be split apart at the question mark and printed on separate lines if the user chose option 1.
How old are you?
5
What is your name?
Merriwether
What is the weight of a cat on Veda?
83_vlerps
So I pieced together a program with what I read online, but I'm really a bit lost. It's a really messy program, and there were some things I was not able to do. One thing is that I was not able to save the data in the same file that I was reading, because the PrintWriter object deletes everything that is in the destination file when it is opened (I think). So I saved it to an output file, and then put in a convoluted renaming/deleting scheme that doesn't even work.
Also, the split() method that was meant to break the question and answer apart to be printed on separate lines doesn't seem to work either.
Can anyone offer some guidance on how this program should be written, while keeping at the same basic level?
Thanks to everyone for sharing their knowledge.
import java.util.*; import java.io.*; public class Lab5 { public static void main(String[] args) throws IOException, FileNotFoundException { int choice; String location; Scanner input = new Scanner(System.in); System.out.println("Let's answer some questions."); do { System.out.println("Would you like to open answers from a previously saved file? Enter 1 for yes and 2 for no: "); choice = input.nextInt(); }while (choice != 1 && choice != 2); if (choice == 1) { System.out.println("Please enter the file and path: "); location = input.next(); File answers = new File(location); if (!answers.exists()) { System.out.println("File not found."); choice = 2; } printAnswers(answers); }//end if else { System.out.println("That's okay, you can answer my questions."); location = "c:/BlueJ/mine/questions.txt"; File questions = new File(location); if (!questions.exists()) { System.out.println("File not found."); System.exit(0); } answerQuestions(questions); } }//end main public static void printAnswers(File answers) throws IOException { Scanner fileReader = new Scanner(answers); while (fileReader.hasNext()) { String line = fileReader.nextLine(); String[] ln = line.split("?"); System.out.println(ln[0] + "?"); System.out.println(ln[1]); }//end while }//end method public static void answerQuestions(File questions) throws IOException, FileNotFoundException { String answer; Scanner input = new Scanner(System.in); Scanner fileReader = new Scanner(questions); File outputFile = new File("c:\\BlueJ\\mine\\outputFile.txt"); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile))); while (fileReader.hasNext()) { String line = fileReader.nextLine(); System.out.println(line); answer = input.next(); line = line.concat(" " + answer); writer.println(line); } writer.flush(); writer.close(); boolean rn1 = questions.renameTo(new File("c:\\BlueJ\\mine\\answers.txt")); boolean rn2 = outputFile.renameTo(new File("c:\\BlueJ\\mine\\questions.txt")); if (rn1 && rn2) { File file = new File("c:\\BlueJ\\mine\\answers.txt"); boolean deleted = file.delete(); if (deleted) System.out.println("Questions file altered."); else System.out.println("Original file could not be altered. A new file, answers.txt, was created."); } else System.out.println("Original file could not be altered. A new file, outputFile.txt, was created."); } }