Solved

why is my java program not outputting to a file

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 54
how to install java on RHEL image on EC2 4 57
JAVA API design with micro service cloud in mind 1 82
Java Eclipse Loop 3 31
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

726 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