Solved

why is my java program not outputting to a file

Posted on 2014-10-09
2
130 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 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

20 Experts available now in Live!

Get 1:1 Help Now