Solved

why is my java program not outputting to a file

Posted on 2014-10-09
2
139 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

615 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