Error checking

what have I missed?

Page 1 of 1

4 Replies - 10624 Views - Last Post: 12 November 2006 - 03:42 PM Rate Topic: -----

#1 haventAclue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-November 06

Error checking

Posted 12 November 2006 - 12:54 PM

Heer is an assignment that just does not seem to work just right. All is well except for the error checking. I don't understand why is outputs the error line when all data entered (and it allows me to enter the data) is valid data. here are the two sets of code.
They both compile without errors.

// Javascript Document
import java.util.Scanner;

public class Sales2
{
	public void calculateSales()
	{
		Scanner input = new Scanner( System. in );
		// sales array hods data on number of each product sold by each sales person
		double sales[][] = new double[ 5 ] [ 4 ];

		System.out.print( " Enter sales person number: ( -1 to quit) ");
		int person = input.nextInt();

		while ( person != -1 )
		{
			System.out.print( "Enter product number: ");
			int product = input.nextInt();
			System.out.print( " Enter sales amount: ");
			double amount = input.nextDouble();

			// Error checking input
			if ( person >= 1 && person < 5 && product >= 1 && product < 6 && amount >= 0 )
			sales[ product -1 ] [ person -1 ] += amount;
		else
			System.out.println( "Invalid input!" );

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

		// calculates total for each sales person
		double salesPersonTotal[] = new double[ 4 ];

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

		System.out.printf( "%8s%14s%14s%14s%14s%10s\n",
						  "Product", "Salesperson 1", "Salesperson 2", "Salesperson 3",
						  "Salesperson 4", "Total" );
		// For each column of each row, print the appropriate vales representing a persons sales of a product
		for ( int row = 0; row < 5; row++ )
		{
		double productTotal = 0.0; // sets product Total amount at 0 dollars and 0 cents (double)
		System.out.printf( "%8d", ( 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 ];
		}  // ends for loop ~ for ( int row = 0; row < 5: row++ )) ~

		System.out.printf( "%10.2f\n:", productTotal );
		// end for loop ~ for ( int column = 0; column < 4; column++ ) ~
		}

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

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


		System.out.println();
	} // end method calculateSales

} // end class Sales2



Second java code needee to run app.
public class sales2Test
{
	public static void main( String args[] )
{
	Sales2 application = new Sales2();
	application.calculateSales();


}// end Main

} // end class



Is This A Good Question/Topic? 0
  • +

Replies To: Error checking

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Error checking

Posted 12 November 2006 - 01:17 PM

Can you specify the input you've given the application?
Was This Post Helpful? 0
  • +
  • -

#3 haventAclue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-November 06

Re: Error checking

Posted 12 November 2006 - 02:04 PM

Sales Person Number - input- 123
product number - input - 456
Sales amount - input 10.00

Then I get the "Invalid input!" string but.. code caries on to allow further input.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Error checking

Posted 12 November 2006 - 02:43 PM

View PosthaventAclue, on 12 Nov, 2006 - 01:04 PM, said:

Sales Person Number - input- 123
product number - input - 456
Sales amount - input 10.00

Then I get the "Invalid input!" string but.. code caries on to allow further input.

The reason you are getting "Invalid input!" message is because of the logic in your IF statement if ( person >= 1 && person < 5 && product >= 1 && product < 6 && amount >= 0 ). The only input allowed for person is a number between 1 and 4 and for product it is between 1 and 5. So in providing input of "123" for Sales Person Number this results in your IF statement to be false, your input is limited to 1, 2, 3, or 4 only. Same thing goes for Product Number.

Try entering input of:

Quote

Sales Person Number - input- 2
product number - input - 4
Sales amount - input 10.00

Was This Post Helpful? 0
  • +
  • -

#5 haventAclue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-November 06

Re: Error checking

Posted 12 November 2006 - 03:42 PM

Thanks. My brain is cooked I've been looking at this code for so long.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1