Question

Trying to do this program for my introduction to programming class. I am unable to get it to run, even before I added the printwriter it had no errors and still would not run. Any tips would be appreciated (how to fix it and how to get the printwriter to work.

package pa2;

import java.io.PrintWriter;
import java.util.Scanner;

public class pa2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    {

        Scanner keyboard = new Scanner(System.in);

        PrintWriter prw = new PrintWriter ("pa2output.txt");

        // variables
        double tshirt, chips, coke, tax, sale, subtotal, total, tshirtcost, chipscost, cokecost, deposit, cokewdeposit, discount, change, payment, numshirt, numcoke, numchips;
        String name;
        tshirt = 18.95;
        chips = 1.79;
        coke = 2.99;
        deposit = 1.2;
        tax = .06;
        sale = .15;

        // menu
        System.out.print("What is your name? ");
        prw.print("What is your name? ");
        name = keyboard.nextLine();
        System.out.println("Welcome to Denny's " + name + "! " + "We have the following items for sale:");
        prw.print("Welcome to Denny's " + name + "! " + "We have the following items for sale:");
        System.out.println("T-shirt $" + tshirt + "15% off");
        prw.print("T-shirt $" + tshirt + "15% off");
        System.out.println("Chips $" + chips + "15% off");
        prw.print("Chips $" + chips + "15% off");
        System.out.println("Coke $" + coke);
        prw.print("Coke $" + coke);

        // input
        System.out.println("How many T-shirts do you want?");
        prw.print("How many T-shirts do you want?");
        numshirt = keyboard.nextDouble();
        System.out.println("How many bags of patato chips?");
        prw.print("How many bags of patato chips?");
        numchips = keyboard.nextDouble();
        System.out.println("What about 12-pack Coke?");
        prw.print("What about 12-pack Coke?");
        numcoke = keyboard.nextDouble();
        System.out.println("Please enter your payment: ");
        prw.print("Please enter your payment: ");
        payment = keyboard.nextDouble();
        // variables

        cokewdeposit = (deposit * numcoke);
        tshirtcost = (tshirt * numshirt);
        chipscost = (chips * numchips);
        cokecost = (coke * numcoke);
        subtotal = (tshirtcost + chipscost + cokecost + cokewdeposit);
        discount = (sale * subtotal);
        total = subtotal + (subtotal * tax) - discount;
        change = payment - total;

        // calculation
        System.out.println("Your total is $" + total);
        prw.print("Your total is $" + total);
        System.out.println(name + "here is your receipt");
        prw.print(name + "here is your receipt");
        System.out.println("item," + " " + "unit price," + " " + "how many," + " " + "cost");
        prw.print("item," + " " + "unit price," + " " + "how many," + " " + "cost");
        System.out.println("T-shirt ");
        prw.print("T-shirt ");
        System.out.print(tshirt + " " + numshirt + " " + (tshirt * numshirt));
        prw.print(tshirt + " " + numshirt + " " + (tshirt * numshirt));
        System.out.println("Chips ");
        prw.print("Chips ");
        System.out.print(chips + " " + numchips + " " + (chips * numchips));
        prw.print(chips + " " + numchips + " " + (chips * numchips));
        System.out.println("Coke" + coke);
        prw.print("Coke" + coke);
        System.out.print(coke + " " + numcoke + " " + (coke * numcoke));
        prw.print(coke + " " + numcoke + " " + (coke * numcoke));

        // receipt
        System.out.println("Discount " + subtotal);
        prw.print("Discount " + subtotal);
        System.out.println("Discount " + discount);
        prw.print("Discount " + discount);
        System.out.println("Tax " + tax);
        prw.print("Tax " + tax);
        System.out.println("Total " + total);
        prw.print("Total " + total);
        System.out.println("Payment " + payment);
        prw.print("Payment " + payment);
        System.out.println("Your change is " + change);
        prw.print("Your change is " + change);
        System.out.println("Thank you. Come again!");
        prw.print("Thank you. Come again!");


        keyboard.close();
        prw.close();
    }
}
Was it helpful?

Solution

That big block of code that you have that is not part of main and so it is never called. You must be either 1) put into main, or 2) put into a function that is called from main.

Right now you just have code hanging out in space and you are not calling it.

perhaps toss this around it for starters:

private static void foo(){ 
   // stick block of code in here
}

then call it from main like this:

 public static void main(String [] args){
     foo();
 }

OTHER TIPS

Just a quick addition to the accepted answer.

The code you have written is actually in what is known as an "initialization block" which is completely valid. However it is generally used to "initialize" variables etc.. There are limitations on what you can do with them. (Issues generating threads etc..)

ie:

    int a = 0;
    {
    //Your code
      a=3;
    }

The code inside gets executed at compile time by the compiler, however it is not meant to handle code generation. Only variable initialization. The example below which uses the "static" keyword should let you execute some code, but it is generally not necessary.

int a=0;
static {
//Your code
a= 3;
// Other stuff
}

The better plan is to do it how miss.serena said. Here's a link that explains how initialization blocks work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top