8 Replies - 1553 Views - Last Post: 19 July 2009 - 09:03 PM Rate Topic: -----

#1 BCurley1973  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 19-July 09

Help with toString() and DecimalFormat

Posted 19 July 2009 - 07:59 PM

Hi everyone,

I am having a little issue with some Java code. I wrote an assignment that takes input from an input text file and then output it to one of two files, depending on the price of the item. The input file is called ProductInformation.txt, and the output file for products >= 50.00 is WishList.txt, and the output file for products < 50.00 is GiftIdeas.txt. The files work correctly, but it only places one decimal place in the output text files. I am pretty sure that I need to use DecimalFormat, but I am unsure exactly how to do so.

Here is my code for the ProductInformation.java file:
public class ProductInformation
{
	// Declare variables that will be used
	private int productNumber;
	private String productName;
	private double productPrice;


	// Method used as a no-argument constructor to call other constructer w/ default values
	public ProductInformation()
	{
		// Calls three argument constructor using default values
		this( 0, "", 0.00 );
	} // Ends no-argument ProductInformation constructor


	// Method used as a constructor to initialize a single record
	public ProductInformation( int number, String name, double price )
	{
		// Initialize productNumber variable
		setProductNumber( number );
		// Initialize productName variable
		setProductName( name );
		// Initialize productPrice variable
		setProductPrice( price );
	} // Ends three-argument ProductInformation constructor


	// Method used to set the product number
	public void setProductNumber( int number )
	{
		productNumber = number;
	} // Ends setProductNumber method


	// Method used to get the product number
	public int getProductNumber()
	{
		// Returns the product number
		return productNumber;
	} // Ends method getProductNumber


	// Method used to set the product name
	public void setProductName( String name )
	{
		productName = name;
	} // Ends method setProductName


	// Method used to get the product name
	public String getProductName()
	{
		// Returns the product name
		return productName;
	} // Ends method getProductName


	// Method used to set the product price
	public void setProductPrice( double price )
	{
		productPrice = price;
	} // Ends method setProductPrice


	// Method used to get the product price
	public double getProductPrice()
	{
		// Returns the product price
		return productPrice;
	} // Ends method getProductPrice

} // Ends class file ProductInformation



This is my code for the SortProductRecords.java file:
// Import statements to get all needed commands
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.IllegalFormatException;
import java.io.File;
import java.io.FileWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.IOException;


public class SortProductRecords
{

	// Declaration of variables

	// Declares an instance of the Scanner object
	private static Scanner inputProductInformation;
	// Declares one instance of the FileWriter object
	private static FileWriter outputGiftIdeas;
	// Declares a second instance of the FileWriter object
	private static FileWriter outputWishList;
	// Declares instance variable of the record that is being read
	private static ProductInformation productInformation;


	// Method used to open text files being used by the application
	public void openFiles()
	{
		// Try statement that opens the proper text files
		try
		{
			// Uses Scanner object to open and read from input ProductInformation.txt file
			inputProductInformation = new Scanner( new File( "ProductInformation.txt" ) );

			// Uses an instance of FileWriter object to open output GiftIdeas.txt file
			outputGiftIdeas = new FileWriter( "GiftIdeas.txt", true );

			// Uses an instance of FileWriter object to open output WishList.txt file
			outputWishList = new FileWriter( "WishList.txt", true );
		} // Ends try statement

		// Catch statement used when a file does not open
		catch( FileNotFoundException fileNotFoundException )
		{
			// Prints error message
			System.err.println( "There was an error opening the file." );
			// Exits the program
			System.exit( 1 );
		} // Ends catch statement

		// Catch statement used when the file cannot be accessed
		catch( SecurityException securityException )
		{
			// Prints error message
			System.err.println( "The file has restricted access." );
			// Exits the program
			System.exit( 1 );
		} // Ends catch statement

		// Catch statement that looks for I/O exceptions
		catch( IOException ioException )
		{
			// Prints error statement
			Logger.getLogger( SortProductRecords.class.getName() ).log( Level.SEVERE, null, ioException );
			// Exits the program
			System.exit( 1 );
		} // Ends catch statement
	} // Ends method openFiles


	// Method used to read records from the input ProductInformation.txt file
	public ProductInformation getProductInformation()
	{
		// Places the record being read in the information object
		ProductInformation information = new ProductInformation();

		// If statement that checks for the next record and reads it if there is one
		if( inputProductInformation.hasNext() )
		{
			// Reads the product number
			information.setProductNumber( inputProductInformation.nextInt() );

			// Reads the product name
			information.setProductName( inputProductInformation.next() );

			// Reads the product price
			information.setProductPrice( inputProductInformation.nextDouble() );
		} // Ends if statement

		// Sets information variable to null if there are no more records to read
		else
		{
			information = null;
		} // Ends else statement

		// Returns the information variable
		return information;

	} // Ends method ProductInformation


	// Method used to determine which output file the product record gets wriiten to based on price
	public void processProductRecords()
	{
		// Reads the product information
		productInformation = getProductInformation();

		// Try statement used to read records, create temporary record, and choose output file
		try
		{
			// While statement that ends when there are no more records to read
			while( productInformation != null )
			{
				// Creates a temporary buffer to hold the information that was read
				StringBuffer tempRecord = new StringBuffer();

				// Appends product number to the temporary record
				tempRecord.append( Integer.toString( productInformation.getProductNumber() ) );

				// Appends a space after the product number
				tempRecord.append( " " );

				// Appends the product name to the temporary record
				tempRecord.append( productInformation.getProductName() );

				// Appends a space after the product name
				tempRecord.append( " " );

				// Appends the product price to the temporary record
				tempRecord.append( Double.toString( productInformation.getProductPrice() ) );

				// Appends an end-of-line marker to the temporary record
				tempRecord.append( System.getProperty( "line.separator" ) );

				// Statements to determine which output file the record gets written into

				// If statement that writes to WishList.txt file if the price is >= 50.00
				if( productInformation.getProductPrice() >= 50.00 )
				{
					outputWishList.write( tempRecord.toString().toCharArray() );
				} // Ends if statement

				// Else statement that writes to GiftIdeas.txt file since price < 50.00
				else
				{
					outputGiftIdeas.write( tempRecord.toString().toCharArray() );
				} // Ends else statement

				// Reads the next product information record
				productInformation = getProductInformation();
			} // Ends while statement
		} // Ends try statement

		// Catch statement for bad format
		catch( IllegalFormatException formatException )
		{
			// Prints error statement
			System.err.println( "There was an error with the output." );
			// Exits the program
			System.exit( 1 );
		} // Ends catch statement

		// Catch statement for I/O error
		catch( IOException ioException )
		{
			// Prints error statement
			System.err.println( "There was an error creating the file." );
			// Exits the program
			System.exit( 1 );
		} // Ends catch statement
	} // Ends method processProductRecords


	// Method used to close all of the txt files
	public void closeFiles()
	{
		// Try statement used to close the files
		try
		{
			// If statement that closes ProductInformation.txt file if it is not empty
			if( inputProductInformation != null )
			{
				inputProductInformation.close();
			} // Ends if statement

			// If statement that closes WishList.txt file if it is not empty
			if( outputWishList != null )
			{
				outputWishList.close();
			} // Ends if statement

			// If statement that closes GiftIdeas.txt file if it is not empty
			if( outputGiftIdeas != null )
			{
				outputGiftIdeas.close();
			} // Ends if statement
		} // Ends try statement

		// Catch statement for I/O exceptions
		catch( IOException ioException )
		{
			// Prints error statement
			System.err.println( "There was an error closing the files." );
			//Exits the program
			System.exit( 1 );
		} // Ends catch statement
	} // Ends method closeFiles

} // Ends class file SortProductRecords



And this is the code for my SortProductRecordsTest.java file:
public class SortProductRecordsTest
{

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args)
	{
		// Creates an object that will run the application
		SortProductRecords application = new SortProductRecords();

		// Calls method used to open files
		application.openFiles();

		// Calls method to read product information records and determines the correct output txt file
		application.processProductRecords();

		// Calls method to close files
		application.closeFiles();
	}// Ends main method

} // Ends main class file SortProductRecordsTest



Is This A Good Question/Topic? 0
  • +

Replies To: Help with toString() and DecimalFormat

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Help with toString() and DecimalFormat

Posted 19 July 2009 - 08:06 PM

Where do you outout, or try to output, your productPrice ?
Was This Post Helpful? 0
  • +
  • -

#3 BCurley1973  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 19-July 09

Re: Help with toString() and DecimalFormat

Posted 19 July 2009 - 08:16 PM

View Postpbl, on 19 Jul, 2009 - 07:06 PM, said:

Where do you outout, or try to output, your productPrice ?



I am using the ProcessRecords method to retrieve the products price. I am calling the getProductPrice method to do this. I am using Double.toString() and appending the price. Since the prices are all even dollar amounts, they all only print one decimal place in the output files. I am almsot positive I need to import DecimalFormat to specify two places.

Here is the snippet of code that appends the product price:
				// Appends the product price to the temporary record
				tempRecord.append( Double.toString( productInformation.getProductPrice() ) );


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Help with toString() and DecimalFormat

Posted 19 July 2009 - 08:25 PM

DecimalFormat dc = new DecimalFormat("0.00");

This has only to be done once
and then

tempRecord.append( dc.format(productInformation.getProductPrice() ));

is 1973 your date of birth ?

This post has been edited by pbl: 19 July 2009 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

#5 BCurley1973  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 19-July 09

Re: Help with toString() and DecimalFormat

Posted 19 July 2009 - 08:34 PM

View Postpbl, on 19 Jul, 2009 - 07:25 PM, said:

DecimalFormat dc = new DecimalFormat("0.00");

This has only to be done once
and then

tempRecord.append( dc.format(productInformation.getProductPrice() ));

is 1973 your date of birth ?



Thank you very much. I just put that in my code and it works perfectly!

And yes, I was born in 1973.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Help with toString() and DecimalFormat

Posted 19 July 2009 - 08:41 PM

View PostBCurley1973, on 19 Jul, 2009 - 07:34 PM, said:

And yes, I was born in 1973.

Cute :) Was the year I had my first programmer job :D
Was This Post Helpful? 0
  • +
  • -

#7 BCurley1973  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 19-July 09

Re: Help with toString() and DecimalFormat

Posted 19 July 2009 - 08:43 PM

View Postpbl, on 19 Jul, 2009 - 07:41 PM, said:

View PostBCurley1973, on 19 Jul, 2009 - 07:34 PM, said:

And yes, I was born in 1973.

Cute :) Was the year I had my first programmer job :D



HAHAHAHA...now I feel like a kid :D
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Help with toString() and DecimalFormat

Posted 19 July 2009 - 08:49 PM

View PostBCurley1973, on 19 Jul, 2009 - 07:43 PM, said:

HAHAHAHA...now I feel like a kid :D

From my perspective, you are :D
Happy to have helped you
Was This Post Helpful? 0
  • +
  • -

#9 BCurley1973  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 19-July 09

Re: Help with toString() and DecimalFormat

Posted 19 July 2009 - 09:03 PM

View Postpbl, on 19 Jul, 2009 - 07:49 PM, said:

View PostBCurley1973, on 19 Jul, 2009 - 07:43 PM, said:

HAHAHAHA...now I feel like a kid :D

From my perspective, you are :D
Happy to have helped you


And helped you did. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1