Solved

why is my java program not outputting to a file

Posted on 2014-10-09
2
132 Views
Last Modified: 2014-10-09
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
Comment
Question by:rustypoot
2 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 40372183
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
 

Author Closing Comment

by:rustypoot
ID: 40372188
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now