Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 141
  • Last Modified:

why is my java program not outputting to a file

Hi there, I'm taking a java class and I'm stuck on my code. Everything with the code works but all thats needed now is to have the outputs print to a file. I put the print writer in but for some reason its not working. Thank you for the help!



import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

public class ch3_PrExecise6 {
      static Scanner console = new Scanner (System.in);
      public static void main (String[] args )
                  throws FileNotFoundException
      {
            //local variables
            String firstName;
            String lastName;
            double FIT=.15;
            double ST=.035;
            double SST=.0575;
            double MT=.0275;
            double PP= .05;
            double HI = 75.00;
            double gA;
            double netPay;
            DecimalFormat df = new DecimalFormat("#.##");
            
            //input
            System.out.println("Enter this information. First Name, Last name, Gross amount.");
            firstName=console.next();
            lastName=console.next();
            gA=console.nextDouble();
            
            PrintWriter outFile = new PrintWriter(firstName+lastName+".txt");
            

            //math
            FIT*=gA;
            ST*=gA;
            SST*=gA;
            MT*=gA;
            PP*=gA;
            netPay=gA-FIT-ST-SST-MT-PP-HI;
            
            //output
            System.out.println(lastName + " " + firstName);
            System.out.println("Gross Amount:           $"+df.format(gA));
            System.out.println("Federal Tax:            $" + df.format(FIT));
            System.out.println("State Tax:              $" + df.format(ST));
            System.out.println("Social Security Tax:    $" + df.format(SST));
            System.out.println("Medicare/Medicaide Tax: $" + df.format(MT));
            System.out.println("Pension Plan:           $" + df.format(PP));
            System.out.println("Health Insurance:       $" + df.format(HI));
            System.out.println("Net Pay:                $" + df.format(netPay));
            
            outFile.close();
      }

}
0
rustypoot
Asked:
rustypoot
1 Solution
 
Paul JacksonCommented:
change :

 //output
             System.out.println(lastName + " " + firstName);
             System.out.println("Gross Amount:           $"+df.format(gA));
             System.out.println("Federal Tax:            $" + df.format(FIT));
             System.out.println("State Tax:              $" + df.format(ST));
             System.out.println("Social Security Tax:    $" + df.format(SST));
             System.out.println("Medicare/Medicaide Tax: $" + df.format(MT));
             System.out.println("Pension Plan:           $" + df.format(PP));
             System.out.println("Health Insurance:       $" + df.format(HI));
             System.out.println("Net Pay:                $" + df.format(netPay));

Open in new window


to:

 //output
             outFile.println(lastName + " " + firstName);
             outFile.println("Gross Amount:           $"+df.format(gA));
             outFile.println("Federal Tax:            $" + df.format(FIT));
             outFile.println("State Tax:              $" + df.format(ST));
             outFile.println("Social Security Tax:    $" + df.format(SST));
             outFile.println("Medicare/Medicaide Tax: $" + df.format(MT));
             outFile.println("Pension Plan:           $" + df.format(PP));
             outFile.println("Health Insurance:       $" + df.format(HI));
             outFile.println("Net Pay:                $" + df.format(netPay));

Open in new window

0
 
rustypootAuthor Commented:
Alright, it worked thank you very much! I was confused because in the first homework code I programmed it printed to a new file just fine.



import java.io.*;
import java.util.*;
public class ch3_PrExercise1 {
      public static void main (String[] args)
            throws FileNotFoundException
      {
            Scanner inFile =
                        new Scanner(new FileReader("test.txt"));
            PrintWriter outFile = new PrintWriter ("out.txt");
            
            //code for data manipulation
            String FirstName;
            String LastName;
            String letter;
            float length;
            float width;
            float radius;
            float balance;
            float rate;
            float area;
            float perimeter;
            float NewBalance;
            float dec;

            int age;

            length=inFile.nextFloat();
            width=inFile.nextFloat();
            radius= inFile.nextFloat();
            FirstName = inFile.next();
            LastName = inFile.next();
            age = inFile.nextInt();
            balance = inFile.nextFloat();
            rate = inFile.nextFloat();
            letter = inFile.next();
            
            area = length*width;
            perimeter =2*(length+width);
            dec=(float) (rate*.01);
            NewBalance=(float)12/(dec*balance);
            System.out.println("Rectangle:"+"\n"+"Length = "+ length + ", width = "+ +width + ", area = "+ area + ", perimeter ="+ String.format(" %.2f",perimeter));
            area = (float)(3.1416*(radius*radius));
            perimeter = 2*(float)3.1416*radius;
            System.out.println("Circle:\n"+"Radius = " + String.format("%.2f", radius) + ", area = " + String.format("%.2f",area) + ", Circumference = " + String.format("%.2f",perimeter));
            System.out.println("Name: "+ FirstName + " " + LastName + ", Age: " + age);
            System.out.println("Beginning Balance = " + "$"+String.format("%.2f",balance) + ", interest rate = " + rate);
            System.out.println("Balance at the end of the month = " + NewBalance);
            System.out.println("\nThe character that comes after " + letter + " in the ASCII set is B");
            inFile.close();
            outFile.close();      
      }

}
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now