[Show all top banners]

Slackdemic
Replies to this thread:

More by Slackdemic
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Java program doesn't compile??
[VIEWED 1917 TIMES]
SAVE! for ease of future access.
Posted on 10-30-06 12:39 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

import java.util.Scanner;
public class Lab09
{
Scanner input = new Scanner( System.in );
double sales = new double[ 5 ][ 4 ];
System.out.print( "Enter sales person number (-1 to end ): ");
int person = input.nextInt();
while ( person != -1 )
{
System.out.print( "Enter product number: ");
int product = input.next();
System.out.print( "Enter sales amount: ");
double amount = input.nextDouble();
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 end): ");
person = input.nextInt();


}

double salesPersonTotal[][] = new double[ 4 ];

for ( int column = 0; column < 4; column++ )
salesPersonTotal[ column ][ row ] = 0;

System.out.printf( "%7s%14s%14s%14s%14s%10s\n", "Product", "Salesperson1","Salesperson2","Salesperson3", "Salesperson 4", "Total" );

for ( int row = 0; row < 5; row++ )
{
double productTotal = 0.0;
System.out.printf( "%7d", (row + 1) );
for ( int coulumn = 0; column < 4; column++ )
{
System.out.printf( " %14.2f", sales[ column ][row] );
productTotal += sales[ column ][ row ];
}
System.out.printf( "%7s", "Total");
for ( int column = 0; column < 4; column++ )
System.out.printf( "%14.2f", salesPersonTotal[ column ] );
System.out.println();

}
}

..................

public class lab09Test
{
public static void main (String args[] )
{
Lab09 application = new Lab09();
application.calculateSales();
}
}
 
Posted on 10-30-06 12:45 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

what is the error u get..
did u make sure your java file name is lab09Test.java?
r u using jb builder or plain java compilation method?
 
Posted on 10-30-06 12:53 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

yes, the name is correct. I am using JCreator.
 
Posted on 10-30-06 12:55 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I use command line...like this way

javac filename.java

and if u r using a simple editor like notepad,editpad make sure that the file extension is .java....while saving from notepad some times it saves as "filename.java.txt"
 
Posted on 10-30-06 1:11 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

slack,
can you please post the error message?
 
Posted on 10-30-06 1:17 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

looks like lots of syntax error..

 
Posted on 10-30-06 3:09 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I corrected several mistakes and now it runs, but still not giving desired output:

import java.util.Scanner;
public class Lab09
{
public void calculateSales()
{
Scanner input = new Scanner( System.in );
/*[][] missing after the variable sales */
double sales[][] = new double[ 5 ][ 4 ];

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

while( person != -1 )
{
System.out.print( "Enter product number: ");

/*Compilation error: it should be input.nextInt(), not input.next()*/
int product = input.nextInt();

System.out.print( "Enter sales amount: ");
double amount = input.nextDouble();

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 end): ");
person = input.nextInt();


}

double salesPersonTotal[][] = new double[ 4 ][ 5 ];
/* for statement missing for row */
for ( int row = 0; row < 5; row++ )
{
for ( int column = 0; column < 4; column++ )
{
salesPersonTotal[ column ][ row ] = 0;
}

}


System.out.printf( "%7s%14s%14s%14s%14s%10s\n", "Product", "Salesperson 1","Salesperson 2","Salesperson 3", "Salesperson 4", "Total" );

for ( int row = 0; row < 5; row++ )
{
double productTotal = 0.0;
System.out.printf( "%7d", (row + 1) );
for ( int column = 0; column < 4; column++ )
{
System.out.printf( " %14.2f", sales[ column ][row] );
productTotal += sales[ column ][ row ];
salesPersonTotal[ column ][ row ] += sales[ column ][row ];
}

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

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

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

System.out.println();
}
}
 
Posted on 10-30-06 3:41 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

This is the output I am getting; and I can see it is not working...any suggestion, anyone?

 
Posted on 10-30-06 3:54 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Slackdemic

I am using java after 3-4 years....So i don't know where your code goes wrong but the error exists here...i think so

double sales[][] = new double[ 5 ][ 4 ];

here you are defining an array of 5 dimesion having 4 dimension each


double salesPersonTotal[][] = new double[ 4 ][ 5 ];

and here you are using an array of 4 dimension having 5 dimension each

That's why this error occurs. you should try either

double sales[][] = new double[ 5 ][ 4 ];

double salesPersonTotal[][] = new double[ 5 ][ 4 ];


or

double sales[][] = new double[ 4 ] [5 ];

double salesPersonTotal[][] = new double[ 4 ][ 5 ];

this might help you
 
Posted on 10-30-06 4:43 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Slackdemic

You are getting array index out of bound exception. You are not handling the indexes correctly in calculate sales methods.

Here you go...
it works but you might have to align the output properly to pretty printer.

import java.util.Scanner;
public class lab09{
public void calculateSales()
{
Scanner input = new Scanner( System.in );
/*[][] missing after the variable sales */
double sales[][] = new double[ 5 ][ 4 ];

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

while( person != -1 )
{
System.out.print( "Enter product number: ");

/*Compilation error: it should be input.nextInt(), not input.next()*/
int product = input.nextInt();

System.out.print( "Enter sales amount: ");
double amount = input.nextDouble();

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 end): ");
person = input.nextInt();


}

double salesPersonTotal[] = new double[ 5];
/* for statement missing for row */
for ( int row = 0; row < 5; row++ )
{

salesPersonTotal[ row] = 0;
}


System.out.printf( "%7s%14s%14s%14s%14s%10s\n", "Product", "Salesperson 1","Salesperson 2","Salesperson 3", "Salesperson 4", "Total" );

for ( int row = 0; row < 5; row++ )
{
double productTotal = 0.0;
System.out.printf( "%7d", (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 ];
}
System.out.printf( "%14.2f", productTotal );
System.out.println();
}
System.out.println();
System.out.printf( "%7s", "Total");

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

System.out.println();
}
}
 
Posted on 10-30-06 4:59 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Thank you guys! But, I had to turn that in with the errors. :(
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 30 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
मन भित्र को पत्रै पत्र!
Guess how many vaccines a one year old baby is given
अमेरिकामा बस्ने प्राय जस्तो नेपालीहरु सबै मध्यम बर्गीय अथवा माथि (higher than middle class)
emergency donation needed
Travelling to Nepal - TPS AP- PASSPORT
Morning dharahara
nrn citizenship
जाडो, बा र म……
1974 AD Pinjadako Suga Remixed
Susta Susta Degree Maile REMIXED version
Elderly parents travelling to US (any suggestions besides Special Assistance)?
कल्लाई मुर्ख भन्या ?
ढ्याउ गर्दा दसैँको खसी गनाउच
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters