Question:
Could someone help me debug this java program?
2008-10-02 09:55:03 UTC
I have begun writing this java program and for some reason or another it is not working. I think that what is causeing the problem are the if statements and having multiple conditions. so i go to put braces around them and they are errors. I leave them off and the program goes through both loops. Can someone help me out? here is the code that i have so far:

/* SoftwareSales.java
Eric Tovar October1, 2008
This program is meant to facilitate sales based on a price while giving
discounts where applicable on a package that sells for $99
*/

import java.util.Scanner;
public class SoftwareSales {

public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);

double packages;
double total;
int unit_price=99;
System.out.printf("Software Sales");
System.out.printf("Please enter the number of packages that are being purchased.");
packages=keyboard.nextDouble();
if
(packages>1 && packages<10);
total = packages * unit_price;
System.out.printf("Your total for "+packages+"$"+total);



if

(packages>=10 && packages>20);
total = packages * (unit_price*0.02);
System.out.printf("Your total for " + packages+"$"+total);



}
}
any and all help is very much appreciated
Four answers:
Neeraj Yadav♄
2008-10-02 10:17:44 UTC
corrected code



import java.util.Scanner;



public class SoftwareSales {



public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);



double packages;

double total;

int unit_price = 99;

System.out.printf("Software Sales");

System.out

.printf("Please enter the number of packages that are being purchased.");

packages = keyboard.nextDouble();

if (packages > 1 && packages < 10) {

total = packages * unit_price;

System.out.printf("Your total for " + packages + "$" + total);



}



if (packages >= 10 && packages > 20) {

total = packages * (unit_price * 0.02);

System.out.printf("Your total for " + packages + "$" + total);



}



}

}





Man..you haven't used if statements correctly

in case where you use

if statements

it will just take the next line as statement.



You were using ; after the condition which means there's no statement for it ; means simple blank statement



correct your basics dear...



You need to read more text books before you really start doing codes.







Hope this helps

Cheers:)
deonejuan
2008-10-02 11:55:33 UTC
First, you can totally eliminate the second logic condition. Just say if (packages > 9) give the discount.



Second, I like keeping everything String until you need the math. Here, I banged this out. Please use it to study with. I could have put even more methods() into it to keep the logic clean and distinct from the math.



public class SSales {



static final int UNIT_PRICE = 99;



public SSales() {

intro();

}

public static void main(String[] args ) {

new SSales();

}

private void intro() {

Scanner sc = new Scanner(System.in);

double discountFactor = 0.0;



String[] liner = {

"---------",

"SOFTWARE SALES"};

System.out.printf(

"%-10s %14s %10s%n%n",

liner[0],liner[1],liner[0]);

System.out.printf(

"Please enter the number of packages"

+" that are being purchased.");

String qty = sc.nextLine();

if( validateInt( qty)) {

int numPurch = Integer.valueOf(qty);

if( numPurch > 10) {

System.out.printf("%s%n","Big Discount");

discountFactor = .02;

}

if( numPurch > 1 && numPurch <= 9) {

System.out.printf("s%n", "Good Ole Boy discount");

discountFactor = .01;

}

if( numPurch == 1) {

System.out.printf("%s%n","Remind them about the volume discounts");

discountFactor = .00;

}

System.out.printf( "%-23s%4d%n",

"Quantity Purchased: ", numPurch);

System.out.printf("%-21s$%2d.00%n", "Each at:",UNIT_PRICE);



double subTot = (UNIT_PRICE * numPurch);

double disc = (UNIT_PRICE * numPurch) * discountFactor;



System.out.printf("%-17s$%9.2f%n", "Sub-total",subTot);



if( discountFactor > 0.00) {

System.out.printf( "%-23s%.2f%% %n","Discount",discountFactor);



System.out.printf("%-16s-$%9.2f%n", "Less Discount:",disc );

}

System.out.printf("%-17s$%9.2f%n","Total Amt. Due:", (subTot - disc ));

System.out.printf("%s%s%s%n%n",liner[0],liner[0],liner[0]);

}

}

private boolean validateInt( String quantity ) {



try {

Integer.parseInt(quantity);

return true;

} catch (Exception e) {

return false;

}



}

}
kelsith
2008-10-02 10:02:39 UTC
if (packages>1 && packages<10) {

total = packages * unit_price;

System.out.printf("Your total for "+packages+"$"+total);

} else if (packages>=10 && packages>20) {

total = packages * (unit_price*0.02);

System.out.printf("Your total for " + packages+"$"+total);

}
?
2016-05-29 09:56:53 UTC
As the error message told you, it cant find you file ("a" file). You may put the full path of your file, example: File testFile = new File ("/home/test/filename.txt"); // in linux or File testFile = new File("c:\somepath\filename.txt"); // in windows Note: Same case for both infile and outfile. :D


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...