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

//Chapter 13 (File Input and Output), Java Programming, joyce Farrell, 8th edit

ID: 3681542 • Letter: #

Question

//Chapter 13 (File Input and Output), Java Programming, joyce Farrell, 8th edit

//DebugThirteen3.java

//output, need to fix

// Crestes a file of entrees sold at a restaurant
import java.nio.file.*;
import java.io.*;
import static java.nio.file.AccessMode.*;
import java.util.Scanner;
public class DebugThirteen3
{
   public static void main(String[] args)
   {
      Path file =
         Paths.get("C:\Java\Chapter.13\DebugData4.txt");
      Scanner kb = new Scanner();
      String[] array = new String[2];
      String string = "";
      String delimiter = ",";
      String entree;
      String price;
      final String QUIT = "ZZZ";
      try
      {
         OutputStream output = new BufferedOutputStream(Files.newOutputStream(file));
         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
         System.out.print("Enter first entree or " + QUIT + " to quit >> ");       
         entree = kb.nextLine();
         while(!entree.equals(QUIT))
         {
            System.out.print("Enter price >> ");
            price = kb.nextLine();
            string = entree + delimiter + price +
              System.getProperty("line.separator");
            writer.write(string, 0, string.length());
            System.out.print("Enter next entree or " + QUIT + " to quit >> ");       
            entree = kb.nextLine();
         }
         writer.close();
      }
      catch(Exception e)
      {
        System.out.println("Message: " + e);
      }
   }
}

------------------------------------------------

//DebugData1.txt

Somewhere over the rainbow.

-----------------------------------------------

//DebugData2.txt

There's no place like home.
Are you a good witch or a bad witch?t

---------------------------------------------------

//DebugData3.txt

435-9845
239-9845
981-9883
384-5656
875-3784
874-8120
----------------------------------------------------

//DebugData4.txt

steak,20
lobster,30
macaroni,8
spaghetti,10

Explanation / Answer

1. check folder named Chapter.13 inside Java folder in C drive, if it is not present then create it.

2. old code line : Scanner kb = new Scanner();

     new code line : Scanner kb = new Scanner(System.in);

3. Enter ZZZ in capital letters to exit

Try with following corrected code

//Chapter 13 (File Input and Output), Java Programming, joyce Farrell, 8th edit

//DebugThirteen3.java

//output, need to fix

// Crestes a file of entrees sold at a restaurant
import java.nio.file.*;
import java.io.*;
import static java.nio.file.AccessMode.*;
import java.util.Scanner;
public class DebugThirteen3
{
   public static void main(String[] args) throws Exception
   {
      Path file =
         Paths.get("C:\Java\Chapter.13\DebugData4.txt"); //check folder named Chapter.13 inside Java folder in C drive, if it is not present then create it.
      Scanner kb = new Scanner(System.in); // write System.in as parameter in Scanner Class
      String[] array = new String[2];
      String string = "";
      String delimiter = ",";
      String entree;
      String price;
      final String QUIT = "ZZZ";
      try
      {
         OutputStream output = new BufferedOutputStream(Files.newOutputStream(file));
         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
         System.out.print("Enter first entree or " + QUIT + " to quit >> ");     
         entree = kb.nextLine();
         while(!entree.equals(QUIT))
         {
            System.out.print("Enter price >> ");
            price = kb.nextLine();
            string = entree + delimiter + price +
              System.getProperty("line.separator");
            writer.write(string, 0, string.length());
            System.out.print("Enter next entree or " + QUIT + " to quit >> ");       //Enter ZZZ in capital letters to exit
            entree = kb.nextLine();
         }
         writer.close();
      }
      catch(Exception e)
      {
        System.out.println("Message: " + e);
      }
   }
}