exception error at run and while using debug

Customer Order History using TreeMap

Page 1 of 1

1 Replies - 1243 Views - Last Post: 16 September 2009 - 11:20 AM Rate Topic: -----

#1 gmillerlight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 30-April 09

exception error at run and while using debug

Post icon  Posted 16 September 2009 - 09:38 AM

Quote

Develop an application that will read and process customer history order information from an input text file, to determine total spent for each customer. The application will print a report of all customers and the total each spent. The report will be ordered by customer id.

Each record of the text file contains a customer ID, order number, and total of order. An example customer order file:

10051 1234567 150.00
10052 1234568 200.00
10051 1234569 120.00

Customer id should be the first field, order number should be the second field, and the amount of the order in dollars and cents (no commas) should be the third field. White spaces (no commas) should be used as the delimiter between each field and each customer order record should be in a separate line. Your file may be used as input to a future application which is why the format is important.

The file can contain several entries for the same customer ID. Your application should accumulate the total spent for each customer as you read the order information from the file. The application should print total spent for each customer to the screen using natural ordering of the customer id.

Refer to the various Lists, Sets, Maps, and Queues provide by the Sun Java API. Determine which data structure provided by the Java APIwould be the best choice for your application. Your grade will be based on your use of the class selected and your ability to select the most appropriate class to use based on the stated requirements.

Write an application that will satisfy the assignment requirements and test that it works. For your discussion essay, explain in 1-2 paragraphs the class you chose to use and your reasons for your choice.

The output to the screen should be in the following format:

10051 270.00
10052 200.00

@ run & debug this is the error: Exception error occurred exception:java.util.UnknownFormatConversionException: Conversion = '.'
I have tried everything I know and still can not fix it. Please Help!

package customerhistory;


/**
 *
 * @author 
 */
public class CustomerHistoryTest
{

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) 
	{

		// intializes a object for the customer files class
		CustomerFiles files = new CustomerFiles();

		files.openFile();
		files.customerHistory();
		files.closeFiles();

	}

}


package customerhistory;

import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;


/**
 *
 * @author 
 */
   public class CustomerFiles
   {
	   private static Scanner file;
	   private FileWriter customerHistory;
	   private static CustomerRecords records;



	   // constructor initializes CustomerRecords
   public CustomerFiles()
   {
			   //initiate an object
			   records = new CustomerRecords();
   }
   // opens the files
   public void openFile()
   {
		// open input
	   try
	   {
		   file = new Scanner (new File ("CustomerHistory.txt"));
		  
	   }
	   catch (Exception exception)
	   {
		   System.out.println("Error opening the files:" + exception);
		   System.exit(1);
	   }
   }

   private CustomerRecords getCustomerRecords()
   {
		try
		{
			if (file.hasNext())
			{
		records.setCustomerID(file.nextInt());
		records.setOrderNumber(file.nextInt());
		records.setOrderTotal(file.nextDouble());

		return records;
			}
	   }
	   
	   catch ( Exception exception )
	   {
		 System.err.println( "Invalid input from file." + exception);
	   }

	  //if no records present nothing is returned
	  return null;
	}
   

	// method processes the records and prints out
	// order numbers, and order totals in number order
   public void customerHistory()
   {
		// treemap orders and an object key for displaying
	   TreeMap < Integer, Double> orderRecords
			   = new TreeMap< Integer, Double>();
	   Object key;
	   try
	   {
		   // get the first customer record
		   records = getCustomerRecords();
		   // while record not null get the record
		   while (records != null)
		   {
			   if (orderRecords.containsKey(records.getCustomerID()))
			   {
				  orderRecords.put(records.getCustomerID(),
						  orderRecords.get(records.getCustomerID())+ records.getOrderTotal());
		   }
		   else
		   {
				   //key does not exist
				orderRecords.put(records.getCustomerID(),
						   records.getOrderTotal());
		   }
				records = getCustomerRecords();
		   }
		   Set keyset = orderRecords.keySet();
		   for (Iterator itera = keyset.iterator();itera.hasNext();)
		   {
			   key = itera.next();
			   System.out.printf("%s%.sf\n,",key, orderRecords.get(key));
		   }
	   }
	   catch (Exception exception)
	   {
		   System.err.println("Exception error occurred exception:" + exception);
		  
	   }
   }

	public void closeFiles()
	{
		try
		{
	   if (file != null)
	   {
		  file.close();
	   }
	   
	}

		catch (Exception exception)
		{
				 System.err.println("Error while closing a file." + exception);
				 System.exit(1);

		}
	}
   }


package customerhistory;

/**
 *
 * @author 
 */
public class CustomerRecords
{
	private int customerID;
	private int orderNumber;
	private double orderTotal;

	//Null constructor method

	public CustomerRecords()
	{

	}

	// parameter constructor method
	public CustomerRecords (int ID, int number, int total)
	{
		setCustomerID(ID);
		setOrderNumber(number);
		setOrderTotal(total);
	}

	/**
	 * set the customer ID
	 * @param ID
	 */
	  public void setCustomerID(int ID)
	  {
		  customerID = ID;
	  }

	  /**
	   * returns the customer ID
	   * @return customerID
	   */
	  public int getCustomerID()
	  {
		  return customerID;
	  }
	  /**
	   * sets the order number
	   * @param number
	   */
	  public void setOrderNumber(int number)
	  {
		  orderNumber = number;
	  }
	  /** returns the order number
	   * @return orderNumber
	   */
	  public int getOrderNumber()
	  {
		  return orderNumber;
	  }
	  /**
	   * sets the order total
	   * @param total
	   */
	  public void setOrderTotal(double total)
	  {
		  orderTotal = total;
	  }
	  /**
	   * returns the order total
	   * @return orderTotal
	   */
	  public double getOrderTotal()
	  {
		  return orderTotal;
	  }

}



Is This A Good Question/Topic? 0
  • +

Replies To: exception error at run and while using debug

#2 krizjaz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 99
  • Joined: 07-October 07

Re: exception error at run and while using debug

Posted 16 September 2009 - 11:20 AM

I think i found the error. Here it is:
for (Iterator itera = keyset.iterator();itera.hasNext();)
		   {
			   key = itera.next();
			   System.out.printf("%s%.sf\n,",key, orderRecords.get(key));
		   }



You have mistakenly put an 's' in displaying your floating point value. You might want the decimal places to be shorten. If it is the case, a part of the code should be like this:

System.out.printf("%s%.2f\n,",key, orderRecords.get(key));



Replace the 's' character to a number(how many digits you want to display after the decimal point).

Hope i can help. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1