d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Beginner Error > Please Help
Add Reply New Topic New Poll
Member
Posts: 19,377
Joined: Jul 10 2007
Gold: 4,678.60
Sep 23 2013 12:49pm
the errror im getting its
Code
deductions.java:63: error: non-static variable outFile cannot be referenced from a static context
outFile.printf("-31s%1s%8.2f%n", "Name: " + fName + " " + lName + "\n");

im getting this error for every output line, can anyone help me figure out what this means?

Code
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
// William Highsmith
//this program calculates and prints the monthly paycheck for an employee.
// The net pay is calculated after taking the following deductions:
//Federal Income Tax: 15%
//State Tax: 3.5%
//Social Security Tax: 5.75%
//Medicare/ Medicaid Tax: 2.75%
//Pension Plan: 5%
//Health Insurance: $ 75.00

public class deductions
{

static Scanner console = new Scanner(System.in);
PrintWriter outFile = new
PrintWriter("HW3out.txt");
public static void main(String arg[]) throws
FileNotFoundException
{
//Declaring constants
final double FEDERAL = .15;
final double STATE = .035;
final double SSTAX = .0575;
final double MEDICARE = .0275;
final double PENSION_PLAN = .05;
final double INSURANCE = 75.00;
//strings

String fName;
String lName;
String inputStr;
String outputStr;

double gross;

//Gathering Information
fName = JOptionPane.showInputDialog("Enter Your First Name");
lName = JOptionPane.showInputDialog("Enter Your Last Name");
inputStr = JOptionPane.showInputDialog("Please Enter Gross Wages");
gross = Double.parseDouble(inputStr);

//outputting tax values

double TOTALTAXES = ((FEDERAL + STATE + SSTAX + MEDICARE +
PENSION_PLAN) * gross) + INSURANCE;

outputStr = ("Name: " + fName + " " + lName) + "\n" +
("Gross Amount $" + gross) + "\n" +
("Federal Taxes: $" + (FEDERAL * gross) ) + "\n" +
("State Taxes: $" + (STATE * gross) ) + "\n" +
("Social Security Taxes: $" + (SSTAX * gross) ) + "\n" +
("Medicare/ Medicade Taxes: $" + (MEDICARE * gross) ) + "\n" +
("Pension Plan: $" + (PENSION_PLAN * gross) ) + "\n" +
("Insurance: $" + (INSURANCE) ) + "\n" +
("Net Pay: $" + (gross - TOTALTAXES) );

JOptionPane.showMessageDialog(null, outputStr,
"Tax Deductions", JOptionPane.INFORMATION_MESSAGE);

// outputting to file HW3.txt
outFile.printf("-31s%1s%8.2f%n", "Name: " + fName + " " + lName + "\n");
outFile.printf("-31s%1s%8.2f%n", "Gross Amount $" + "-31s%1s%8.2f%n", gross + "\n" );
outFile.printf("-31s%1s%8.2f%n", "Federal Taxes: $" + (FEDERAL * gross) + "\n" );
outFile.printf("-31s%1s%8.2f%n", "State Taxes: $" + (STATE * gross) + "\n" );
outFile.printf("-31s%1s%8.2f%n", "Social Security Taxes: $" + (SSTAX * gross) + "\n" );
outFile.printf("-31s%1s%8.2f%n", "Medicare/ Medicade Taxes: $" + (MEDICARE * gross) + "\n");
outFile.printf("-31s%1s%8.2f%n", "Pension Plan: $" + (PENSION_PLAN * gross) + "\n" );
outFile.printf("-31s%1s%8.2f%n", "Insurance: $" + INSURANCE + "\n");
outFile.printf("-31s%1s%8.2f%n", "Net Pay: $" + (gross - TOTALTAXES) );


outFile.close();
System.exit(0);

}
}


This post was edited by lilwillis121 on Sep 23 2013 12:49pm
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 23 2013 01:14pm
move
Code
PrintWriter outFile = new
PrintWriter("HW3out.txt");

into your main method
Member
Posts: 19,377
Joined: Jul 10 2007
Gold: 4,678.60
Sep 23 2013 01:18pm
Quote (labatymo @ Sep 23 2013 02:14pm)
move
Code
PrintWriter outFile = new
              PrintWriter("HW3out.txt");

into your main method


Thanks!

next issue completly unrelated i believe.

i need to format these outputs into a file and have the decimals line up and be limited to only 2 places however, i keep getting
Code
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2761)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
at java.util.Formatter.format(Formatter.java:2488)
at java.io.PrintWriter.format(PrintWriter.java:905)
at java.io.PrintWriter.printf(PrintWriter.java:804)
at deductions.main(deductions.java:67)



Code

outFile.printf("Name: " + fName + " " + lName + "\n");
outFile.printf("%8.2f", "Gross Amount $" + gross);
outFile.printf("%8.2f", "Federal Taxes: $" + (FEDERAL * gross) + "\n" );
outFile.printf("%8.2f", "State Taxes: $" + (STATE * gross) + "\n" );
outFile.printf("%8.2f", "Social Security Taxes: $" + (SSTAX * gross) + "\n" );
outFile.printf("%8.2f", "Medicare/ Medicade Taxes: $" + (MEDICARE * gross) + "\n");
outFile.printf("%8.2f", "Pension Plan: $" + (PENSION_PLAN * gross) + "\n" );
outFile.printf("%8.2f", "Insurance: $" + INSURANCE + "\n");
outFile.printf("%8.2f", "Net Pay: $" + (gross - TOTALTAXES) );


This post was edited by lilwillis121 on Sep 23 2013 01:30pm
Member
Posts: 2,579
Joined: Jun 1 2012
Gold: 1,524.00
Sep 23 2013 08:35pm
Quote (lilwillis121 @ Sep 23 2013 03:18pm)
Thanks!

next issue completly unrelated i believe.

i need to format these outputs into a file and have the decimals line up and be limited to only 2 places however, i keep getting
Code
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2761)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
at java.util.Formatter.format(Formatter.java:2488)
at java.io.PrintWriter.format(PrintWriter.java:905)
at java.io.PrintWriter.printf(PrintWriter.java:804)
at deductions.main(deductions.java:67)



Code
outFile.printf("Name: " + fName  + " " + lName  + "\n");
        outFile.printf("%8.2f", "Gross Amount $" +  gross);
        outFile.printf("%8.2f", "Federal Taxes: $" + (FEDERAL * gross)  + "\n" );
        outFile.printf("%8.2f", "State Taxes: $" + (STATE * gross)  + "\n" );
        outFile.printf("%8.2f", "Social Security Taxes: $" + (SSTAX * gross)  + "\n" );
        outFile.printf("%8.2f", "Medicare/ Medicade Taxes: $" + (MEDICARE * gross)  + "\n");
        outFile.printf("%8.2f", "Pension Plan: $" + (PENSION_PLAN * gross)  + "\n" );
        outFile.printf("%8.2f", "Insurance: $" + INSURANCE  + "\n");
        outFile.printf("%8.2f", "Net Pay: $" + (gross - TOTALTAXES) );


You are passing an object of type String into the format specifier %8.2f, which is expecting a float.

You could express the line:

Code
outFile.printf("%8.2f", "Gross Amount $" + gross);


As either of these:

Code
outFile.printf("%s%8.2f%s", "Gross Amount $", gross, "\n");

or
Code
outFile.printf("Gross Amount $%8.2f\n", gross);


This post was edited by Schwag on Sep 23 2013 08:35pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll