Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with errors in code

    I am hoping someone can help me figure out what is wrong with my coding. The assignment is to write a program that reads data from a specified file and stores the ouput in another file. The input has a list of 3 names (last, first) and a salary amount. The output is to read those 3 names (first, last) and have a new salary amount after computing a raise of 5%.
    Here is my code:

    /* Program to read employees names and pay rate from file, calculates raise in pay, and stores output in another file of each employees name and new pay rate.*/
    import java.io.*; //package to use FileReader and PrintWriter
    import java.util.Scanner;

    public class LynchA6
    {
    public static void main(String[] args) throws IOException
    {


    String first_name; //variable to store employees' names
    String last_name;
    double payRate; //variable to show current pay
    double payRaise; //variable to show .05 raise
    double payIncrease; //variable to calculate rate * .05
    double newPay; // variable to show calculated new pay rate

    Scanner inFile = new Scanner(new FileReader ("Assign6Data.txt")); //calls contents of file
    PrintWriter outFile = new PrintWriter("Assign6Out.txt"); //where output is to be saved


    while (inFile.hasNext()) //loop to visit each line
    {
    last_name = inFile.next();
    first_name = inFile.next();
    payRate = inFile.nextDouble();
    newPay = payRate + (payRate * .05);
    }//end while loop

    outFile.printf("%-6s %-6s %7.2f %c %n", first_name, last_name, "$"+newPay); //output with spacing for name and pay

    inFile.close();
    outFile.close();
    }

    }


    The error I get reads:

    ----jGRASP exec: javac -g LynchA6.java

    LynchA6.java:30: variable first_name might not have been initialized
    outFile.printf("%-6s %-6s %7.2f %c %n", first_name, last_name, "$"+newPay); //output with spacing for name and pay
    ^
    LynchA6.java:30: variable last_name might not have been initialized
    outFile.printf("%-6s %-6s %7.2f %c %n", first_name, last_name, "$"+newPay); //output with spacing for name and pay
    ^
    LynchA6.java:30: variable newPay might not have been initialized
    outFile.printf("%-6s %-6s %7.2f %c %n", first_name, last_name, "$"+newPay); //output with spacing for name and pay
    ^
    3 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Wrong section

    Java != Javascript

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,506
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    As DevNull said, wrong forum.

    About the only thing Java and JavaSCRIPT have in common are the first 4 letters of their names.

    But...

    But this is a trivial problem.

    Java is telling you the truth.

    You have a WHILE loop [icode]while (inFile.hasNext()) [/code] and *IN THAT LOOP* you assign values to those variables. But what happens if the while loop NEVER executes? If inFile has no lines at all? Indeed, those variables would *NOT* get any values. So how can you depend on using them in that printf that is *AFTER* the loop?

    HINT: The real problem is that the printf is in the wrong place.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the help. I did post this problem elsewhere on the forum but was afraid I put it in the wrong place. Not to offend anyone, because obviously I have the utmost respect, but I am not computer programming-minded. Just trying to finish this class =D. Thanks again!

  • #5
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    The Java compiler has caught a potential runtime error for you and is asking you to fix it.

    Code:
    while (inFile.hasNext()) //loop to visit each line
    {
    last_name = inFile.next();
    first_name = inFile.next();
    payRate = inFile.nextDouble();
    newPay = payRate + (payRate * .05);
    }//end while loop
    
    outFile.printf("%-6s %-6s %7.2f %c %n", first_name, last_name, "$"+newPay); //output with spacing for name and pay
    If inFile.hasNext() return false on the very first call your while loop won't execute at all. If that happens outFile.printf will execute and try to print first_name, last_name, etc which haven't been initialized. Resulting in a nullpointer exception.

    One possible solution is to set your variables = null when you declare them.
    Last edited by Gox; 05-06-2011 at 01:20 AM. Reason: Huh, somehow I missed that this was already solved...+1 to my post count I guess


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •