Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-04-2011, 06:14 AM   PM User | #1
Jdiz
New to the CF scene

 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Jdiz is an unknown quantity at this point
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.
Jdiz is offline   Reply With Quote
Old 05-04-2011, 06:56 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
Wrong section

Java != Javascript
devnull69 is offline   Reply With Quote
Old 05-04-2011, 07:25 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 05-04-2011, 10:19 AM   PM User | #4
Jdiz
New to the CF scene

 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Jdiz is an unknown quantity at this point
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!
Jdiz is offline   Reply With Quote
Old 05-06-2011, 01:15 AM   PM User | #5
Gox
Regular Coder

 
Gox's Avatar
 
Join Date: May 2006
Location: Ontario, Canada
Posts: 392
Thanks: 2
Thanked 20 Times in 20 Posts
Gox will become famous soon enough
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
Gox is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:25 PM.


Advertisement
Log in to turn off these ads.