• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Help please, due in 1 hour. Does not compile

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi all,
Kinda short on time. Error message:

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '%'
at java.util.Formatter.checkText(Formatter.java:2547)
at java.util.Formatter.parse(Formatter.java:2533)
at java.util.Formatter.format(Formatter.java:2469)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Sales2.main(Sales2.java:56)
Java Result: 1

Here is the code!

// Exercise 7.20 Solution: Sales2.java
// Program totals sales for salespeople and products.
import java.util.Scanner;

public class Sales2
public static void main( String[] args )
Scanner input = new Scanner( System.in );
// sales array holds data on number of each product sold
// by each salesperson
double[][] sales = new double[ 5 ][ 4 ]; // 5 salespeople, 4 products each person

System.out.print( "Enter salesperson number (-1 to end): " );

int person = input.nextInt(); // the salesperson index

while ( person != -1 )
// To do
// prompt to enter product number and save it as an integer
System.out.print("Please enter the product number: ");
int product = input.nextInt();
// promp to enter sales amont and save it as double
System.out.print("Please enter the sales amount: ");
double salesAmount = input.nextDouble();
// error-check the input number for the array boundary
//error check input
if (person >= 1 && person < 5 && product >=1 && product < 6 &&
salesAmount >= 0)
sales[product -1][person -1] =+ salesAmount;
System.out.println("Invalid Input!");

System.out.print("Enter sales person number(-1 to end):");
person = input.nextInt();
}//end while

//salesperson totals
double salesPersonTotal[] = new double[4];

//display data table
for (int column = 0; column < 4; column ++)
salesPersonTotal[column] = 0;

"Product", "Salesperson 1", "Salesperson 2","Salesperson 3",
"Salesperson 4", "Total");

//Printing a person's sales of a product
for (int row = 0; row < 5; row ++)
double productTotal = 0.0;
System.out.printf("8%d%",(row +1) );

for ( int column = 0; column < 4; column ++)
System.out.printf("%14.2f", sales[row][column]);
productTotal += sales[row][column];
salesPersonTotal[column] += sales[row][column];
} //end for loop

System.out.printf("8%s", "Total");

for (int column = 0; column < 4; column ++)
System.out.printf("14.2%f", salesPersonTotal[column]);


}// end calculations

} // end main

What am I missing??? The table does not want to show.

Edit: Forgot problem spec

7.20 (Total Sales) Use a two-dimensional array to solve the following problem: A company has
four salespeople (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for each type of product sold. Each slip contains the following:
a) The salesperson number
b) The product number
c) The total dollar value of that product sold that day

Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month is available. Write an application that will read all this information for last month’s sales and summarize the total sales by salesperson and by product. All totals should be stored in the two-dimensional array sales. After processing all the information for last month, display the results in tabular format, with each column representing a particular salesperson and each row representing a particular product. Cross-total each row to get the total sales of each product for last month. Cross-total each column to get the total sales by salesperson for last month. Your tabular output should include these cross-totals to the right of the totaled rows and to the bottom of the totaled columns.

See attached program template and output sample.

Posts: 10780
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shawn Lorne wrote:Hi all,
Kinda short on time. Error message:

Well, looks like your time has expired, so there's not much point in replying; but for future reference:
1. UseCodeTags (←click).
2. EaseUp.
3. TellTheDetails.
In fact, a good read of the HowToAskQuestionsOnJavaRanch page would probably be in order.

And next time: Don't leave it so late.

Posts: 22780
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, don't use \n, use %n. That will result in \n on Linux and \r\n on Windows.

Shawn Lorne wrote: System.out.printf("8%d%",(row +1) );
System.out.printf("8%s", "Total");
System.out.printf("14.2%f", salesPersonTotal[column]);

Check out the % signs in these three statements. The second and third ones will work but not as intended (the 8 and 14.2 will be printed as-is, not be used for formatting), the first one is plain wrong. If you need to include a literal % sign you must duplicate it: %%.
it's a teeny, tiny, wafer thin ad:
a bit of art, as a gift, that will fit in a stocking
    Bookmark Topic Watch Topic
  • New Topic