#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Exclamation Most likely a simple error; printf ignoring variable


    I'm only trying to print out the elements of this array out into a table. I'm going to do more formatting on it in a moment, but I can't seem to get a temporary variable to work in the printf.

    for (int count1 = 0; count1 < 4; count1++)
    {
    for (int count2 = 0; count2 < 4; count2++);
    {
    System.out.printf( "%10.2f ",
    sales[count2][count1]);
    }
    }

    The printf statement is telling me that count2 needs to be declared before it can be used.

    Any help with the probably stupid question would be awesome.
    Code:
    public class EmpSales 
    {
    	
    public void SalesData()
    {
    	
    	double data[][] = new double[][] 
    	{ 
    		{101,1,152.34},
    		{101,2,23.45},
    		{101,3,12.45},
    		{101,4,76.34},
    		{101,5,12.45},
    		{102,1,65.23},
    		{102,2,12.34},
    		{102,3,87.23},
    		{102,4,123.45},
    		{102,5,65.34},
    		{103,1,150.5},
    		{103,2,200},
    		{103,3,127},
    		{103,4,32.45},
    		{103,5,195.86},
    		{104,1,72.85},
    		{104,2,41.18},
    		{104,3,12.34},
    		{104,4,43.56},
    		{104,5,77.77},
    		{101,1,234.34},
    		{101,2,286},
    		{101,3,154},
    		{101,4,350.23},
    		{102,1,244},
    		{102,3,247},
    		{102,5,311},
    		{103,2,86.12},
    		{103,3,238},
    		{103,4,278},
    		{103,5,165.65},
    		{104,1,85},
    		{104,2,148},
    		{101,5,199.12},
    		{102,1,277},
    		{102,2,302.76},
    		{102,3,119},
    		{102,4,122},
    		{102,5,171.77},
    		{103,5,77.65},
    		{101,1,56},
    		{101,2,273},
    		{101,3,69.12},
    		{101,4,70},
    		{101,5,102},
    		{102,3,200},
    		{102,4,112.12},
    		{102,5,219},
    		{102,2,207},
    		{103,1,112.89},
    		{103,3,339} 
    	};
    	int row = 0, column;
    	double saleAmount;
    	
    	double sales[][] =  new double [5][4];
    	{
    	
    	
    	for (int i = 0; i<51; i++)
    	{
    		row = (int) data[i][1]-1;
    		column = (int) (data[i][0]-101);
    		saleAmount = data[i][2];
    		
    		sales[row][column] += saleAmount;
    		
    		
    
    
    	}
    
    		
    	}
    	for (int count1 = 0; count1 < 4; count1++)
    	{
    		for (int count2 = 0; count2 < 4; count2++);
    		{
    		System.out.printf( "%10.2f ",
    			sales[count2][count1]);
    		}
    	}
    }
    	
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    The error says the code inside the {}s is not part of the preceding if statement.
    The ; at the end of the if statement terminates the if statement.
    count2 is only defined inside the if statement.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    I feel...incredibly dumb that I missed that. Figured it out right before I saw your post.

    Thanks for the help. It's probably fairly obvious that I'm new at this. I'm sure my code is hideous.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    One thing you should change is in the for statement's terminating condition. You should use the .length attribute of the array, not a hardcoded number: 51
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Yep, I went back an changed that when I was finishing up. I had it set to that to test changes in the input data, to see the error codes. Going to post my final product now, just for fun.

    The formatting probably could have been done much easier, but it at least gives me the right outputs that I'm looking for. I just wish I knew how to make an actual table with lines.

    Code:
    /*
     * This class populates the sales array with any data entered
     * into the objects array. It then displays this data summed for each
     * employee into an easy to read table.
     * 
     * James Childs; March 2013
     */
    
    public class EmpSales 
    {
    	
    public void SalesData() //constructor for the test class
    {
    //populate the data array	
    	double data[][] = new double[][] 
    	{ 
    		{101,1,152.34},
    		{101,2,23.45},
    		{101,3,12.45},
    		{101,4,76.34},
    		{101,5,12.45},
    		{102,1,65.23},
    		{102,2,12.34},
    		{102,3,87.23},
    		{102,4,123.45},
    		{102,5,65.34},
    		{103,1,150.5},
    		{103,2,200},
    		{103,3,127},
    		{103,4,32.45},
    		{103,5,195.86},
    		{104,1,72.85},
    		{104,2,41.18},
    		{104,3,12.34},
    		{104,4,43.56},
    		{104,5,77.77},
    		{101,1,234.34},
    		{101,2,286},
    		{101,3,154},
    		{101,4,350.23},
    		{102,1,244},
    		{102,3,247},
    		{102,5,311},
    		{103,2,86.12},
    		{103,3,238},
    		{103,4,278},
    		{103,5,165.65},
    		{104,1,85},
    		{104,2,148},
    		{101,5,199.12},
    		{102,1,277},
    		{102,2,302.76},
    		{102,3,119},
    		{102,4,122},
    		{102,5,171.77},
    		{103,5,77.65},
    		{101,1,56},
    		{101,2,273},
    		{101,3,69.12},
    		{101,4,70},
    		{101,5,102},
    		{102,3,200},
    		{102,4,112.12},
    		{102,5,219},
    		{102,2,207},
    		{103,1,112.89},
    		{103,3,339} 
    	};
    	int row = 0, column;
    	double saleAmount;
    	
    	double sales[][] =  new double [5][4];
    	{
    	
    //populate the sales array	
    	for (int i = 0; i<data.length; i++)
    	{
    		row = (int) data[i][1]-1;
    		column = (int) (data[i][0]-101);
    		saleAmount = data[i][2];
    		
    		sales[row][column] += saleAmount;
    		
    		
    
    
    	}
    
    		
    	}
    //set up the table titles
    	System.out.printf("%10s %-44s %10s",
    			"Products", "       Employee", "       Product Sales Totals");
    	System.out.println("");
    	System.out.printf("%s %10s %10s %10s %10s",
    			"          ", "101", "102", "103", "104" );
    	System.out.println("");
    	
    //start populating the table
    	
    	for (int count1 = 0; count1 < 5; count1++)
    	{
    		System.out.printf("    %10d",
    				count1+1);
    		
    		double psalesTotal = 0;
    		
    		for (int count2 = 0; count2 < 4; count2++)
    		{
    			
    		System.out.printf( "%10.2f ",
    			 sales[count1][count2]);
    		
    		
    		psalesTotal = psalesTotal + sales[count1][count2]; //adds the product sales totals to the loop
    		
    		
    		}
    		
    		System.out.printf("  %10.2f",
    				psalesTotal);
    		
    				
    		System.out.println("");
    	}
    	
    	
    	System.out.printf("%s",
    				"Employee Totals");
    	
    	
    	for (int count1 = 0; count1 < 4; count1++)
    	{
    		double esalesTotal = 0;
    		
    
    			for (int count2 = 0; count2 < 5; count2++)
    			{
    				esalesTotal = esalesTotal + sales[count2][count1];
    				
    			}
    			
    			System.out.printf("%10.2f ",
    						esalesTotal);
    	}
    	
    	
    	
    	
    }
    	
    }

IMN logo majestic logo threadwatch logo seochat tools logo