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

So, basically I need to get a file if it exists..Print it so that one line has t

ID: 3538809 • Letter: S

Question




So, basically I need to get a file if it exists..Print it so that one line has the question and the next has the answers. (Do you like Dogs? NextLn has Yes....etc).If file doesn't exist ask the questions save the answers and the questions in same line (Do you like Dogs? Yes -all on same line)so entire line can be read at once and can be seperated by a ? Mark.


Here is what I have so far...Any help appreciated.


import java.io.*;

import java.util.*;

import java.util.Scanner;


public class tst

{

public static void main(Strin[]args)throws IOException

{

java.io.File file =new java.io.File("test.txt");

if(file.exists())

{System.out.println("File already exists");

System.out.println("Absolute path is " +file.getAbsolutePath());

int i =scanner.nextInt();

System.out.println(i);


}

else

{

java.io.File file= new java.io.File("testing.txt");

  

  

Scanner input=new Scanner(file);

  

While input hasNext())

{

System.out.println("Would you like to load answers from a previously saved file?");

answer=input.nextln

  

if answer==yes;


FileOutputStream outFileSTream=new FileOutputStream(outFile);

PrintWriter outStream =new PrintWriter(outFileStream);

outStream.println("What is your favorite color?");

outStream.println("What is your second favorite color");

outStream.println("What is your third favorite color?");

outStream.close();

}}


Am I close at all??

Explanation / Answer

I have just modified code according description mentioned in the question, I have tested it, It got worked perfectly. Please see the code below.


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


public class tst{
    public static void main(String args[])throws IOException{
         File file =new File("test.txt");
         //if file exists content will be displayed with question and answers.
         if(file.exists()){
            Scanner input=new Scanner(file);
            System.out.println("File already exists");
            System.out.println("Absolute path is " +file.getAbsolutePath());
            while(input.hasNext()){
                System.out.println(input.nextLine());
            }
        }
         //If file not exists ask the questions and answer and load in single line.
         else{
            Scanner scanner = new Scanner(System.in);
            FileOutputStream outFileSTream=new FileOutputStream(file);       
            PrintWriter outStream =new PrintWriter(outFileSTream);
            System.out.println("What is your favorite color?");
            String answer = scanner.nextLine();
            outStream.println("What is your favorite color?"+answer);
            System.out.println("What is your second favorite color");
            answer = scanner.nextLine();
            outStream.println("What is your second favorite color?"+answer);
            System.out.println("What is your third favorite color?");
            answer = scanner.nextLine();
            outStream.println("What is your third favorite color?"+answer);
            outStream.close();
   
        }
    }
}

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