/* Asking for a tip on what exactly am I missing here. The debugger shows the va
ID: 3838442 • Letter: #
Question
/*
Asking for a tip on what exactly am I missing here. The debugger shows the variables are holding the right numbers. However, when trying, multiple ways, to print to a txt file its not right. Confused. Any tips would be really appreciated.
*/
package receiptoutput;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
*
*/
public class ReceiptOutput {
static final int MAX_PRODUCTS = 5;
/**
* @param args the command line arguments
*/
//"c:/cs/receipt.txt"
public static void main(String[] args){
String outputFile = " my file where is goes to. it's is writing, just not desired output ";
FileOutputStream outputStream = null;
PrintWriter output = null;
Scanner keyboard = new Scanner( System.in );
int productCount = 0;
ProductInformation[] productArray = new ProductInformation[MAX_PRODUCTS];
try {
/* Instantiate FileOutputStream class and create outputStream object */
outputStream = new FileOutputStream(outputFile);
}
catch ( FileNotFoundException e )
{
System.out.println( "FileNotFoundException: Output File <" + outputFile + "> Does Not Exist" );
System.exit(0);
}
output = new PrintWriter( outputStream );
try {
/* Determine if no productName is entered */
/* If no productName, throw NoEntryException */
while ( productCount < productArray.length ){
productArray[productCount] = new ProductInformation();
System.out.print( "Enter Product Name for Receipt ( <CR> to Quit ): " );
if (keyboard.hasNextLine()) {
productArray[productCount].productName = keyboard.nextLine();
} else {
throw new NoEntryException();
}
System.out.print( "Enter Product Quantity: " );
productArray[productCount].productQuantity = keyboard.nextInt();
System.out.print( "Enter Product Price : " );
productArray[productCount].productPrice = keyboard.nextDouble();
keyboard.nextLine();
productCount++;
}//end while
}//end try
catch( NoEntryException e ){
System.out.println("Try Again");
}
System.out.println( "Number of Products: " + productCount );
for ( int lp=0; lp<productCount; lp++ )
{
/* Add code to output the product Name, Quantity and Price to the output
file */
System.out.println(); ///testing before putting it in file
// output.println(productArray[lp]);
}
output.close();
}//end main()
}//End Receipt_Class
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public class ProductInformation {
String productName;
int productQuantity;
double productPrice;
//
// public ProductInformation() {
// }
//
// public ProductInformation(String productName, int productQuantity, double productPrice) {
// this.productName = productName;
// this.productQuantity = productQuantity;
// this.productPrice = productPrice;
// }
//
// public String getProductName() {
// return productName;
// }
//
// public void setProductName(String productName) {
// this.productName = productName;
// }
//
// public int getProductQuantity() {
// return productQuantity;
// }
//
// public void setProductQuantity(int productQuantity) {
// this.productQuantity = productQuantity;
// }
//
// public double getProductPrice() {
// return productPrice;
// }
//
// public void setProductPrice(double productPrice) {
// this.productPrice = productPrice;
// }
//
//
//
}
Explanation / Answer
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.*;
//Class ReceiptOutput definition
public class ReceiptOutput
{
//Defines constant size of 5
static final int MAX_PRODUCTS = 5;
/**
* @param args the command line arguments
*/
public static void main(String[] ss)throws IOException
{
String outputFile = "Product.txt";
FileOutputStream outputStream = null;
FileReader reader = null;
PrintWriter output = null;
Scanner keyboard = new Scanner( System.in );
int productCount = 0;
//Creates an array of object
ProductInformation[] productArray = new ProductInformation[MAX_PRODUCTS];
//Try block
try
{
/* Instantiate FileOutputStream class and create outputStream object */
outputStream = new FileOutputStream(outputFile);
}//End of try block
//Catch to handle file not found exception
catch ( FileNotFoundException e )
{
System.out.println( "FileNotFoundException: Output File <" + outputFile + "> Does Not Exist" );
System.exit(0);
} //End of catch
//Opens the file for writing
output = new PrintWriter( outputStream );
//Try block
try
{
/* Determine if no productName is entered */
/* If no productName, throw NoEntryException */
//Loops till product count is less than the length specified or cr is entered by the user
while ( productCount < productArray.length )
{
//Initializes the object
productArray[productCount] = new ProductInformation();
String name;
//Accepts product information
System.out.print( "Enter Product Name for Receipt ( <CR> to Quit ): " );
if (keyboard.hasNextLine())
{
//Accepts product name
name = keyboard.nextLine();
//Checks if the product name is cr or CR then come out of the loop
if(name.equalsIgnoreCase("CR"))
break;
//Otherwise store the product name
productArray[productCount].productName = name;
} //End of if
//Throws exception
else
{
throw new Exception();
}//End of else
//Accepts the quantity
System.out.print( "Enter Product Quantity: " );
productArray[productCount].productQuantity = keyboard.nextInt();
//Accepts price
System.out.print( "Enter Product Price : " );
productArray[productCount].productPrice = keyboard.nextDouble();
keyboard.nextLine();
//Increase the product counter
productCount++;
}//end while
}//end try
//To handle exception
catch(Exception e )
{
System.out.println("Try Again");
}//End of catch
//Displays product count
System.out.println( "Number of Products: " + productCount );
//Loops till product count counter
for ( int lp=0; lp<productCount; lp++ )
{
/* Add code to output the product Name, Quantity and Price to the output file */
output.println(productArray[lp].productName);
output.println(productArray[lp].productQuantity);
output.println(productArray[lp].productPrice);
}//End of loop
//Close the file
output.close();
//Loops till product count counter
for(int c = 0; c < productCount; c++)
{
//Displays product information
System.out.println("Product Name: " + productArray[c].productName);
System.out.println("Quantity Purchased: " + productArray[c].productQuantity);
System.out.println("Price of each product: " + productArray[c].productPrice);
}//End of loop
}//end main()
}//End Receipt_Class
//ProductInformation class definition
class ProductInformation
{
//Instance variables
String productName;
int productQuantity;
double productPrice;
//Default constructor
public ProductInformation()
{
productName = "";
productQuantity = 0;
productPrice = 0;
}//End of constructor
//Parameterized constructor
public ProductInformation(String productName, int productQuantity, double productPrice)
{
this.productName = productName;
this.productQuantity = productQuantity;
this.productPrice = productPrice;
}//End of constructor
//Returns product name
public String getProductName()
{
return productName;
}//End of function
//Sets product name
public void setProductName(String productName)
{
this.productName = productName;
}//End of function
//Returns product quantity
public int getProductQuantity()
{
return productQuantity;
}//End of function
//Sets product quantity
public void setProductQuantity(int productQuantity)
{
this.productQuantity = productQuantity;
}//End of function
//Returns product price
public double getProductPrice()
{
return productPrice;
}//End of function
//Sets product price
public void setProductPrice(double productPrice)
{
this.productPrice = productPrice;
}//End of function
}//End of class
Output:
Enter Product Name for Receipt ( <CR> to Quit ): Lux
Enter Product Quantity: 100
Enter Product Price : 22.20
Enter Product Name for Receipt ( <CR> to Quit ): Cinthol
Enter Product Quantity: 150
Enter Product Price : 45.15
Enter Product Name for Receipt ( <CR> to Quit ): Dove
Enter Product Quantity: 180
Enter Product Price : 55.62
Enter Product Name for Receipt ( <CR> to Quit ): cr
Number of Products: 3
Product Name: Lux
Quantity Purchased: 100
Price of each product: 22.2
Product Name: Cinthol
Quantity Purchased: 150
Price of each product: 45.15
Product Name: Dove
Quantity Purchased: 180
Price of each product: 55.62
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.