'Why is my variable output not correct in Java?
I need to create a program that asks the user questions and takes that input and prints it to a file. If the salary is >200k then it needs to ask for the salary again. I am able to get the file creation to work for everything except for empSalary. I cannot figure out why this is writing to the file as a "?". I have tried looking for this and have determined that this is something to do with the scope, but I cannot get it to work. Would someone be able to tell me what I need to change to get the empSalary to print to the file correctly?
A sample of the output:
1,josh,fox,madison,mn,56256,test,?
2,joe,poe,fargo,nd,58102,poeppe,?
'''
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i = 0;
int empSalary;
while (i < 2) {
System.out.print("Enter the employee number .....");
int empNum = scanner.nextInt();
System.out.print("Enter employee's first name ...");
String empFirstName = scanner.next();
System.out.print("Enter employee's last name ....");
String empLastName = scanner.next();
System.out.print("Enter employee's city .........");
String empCity = scanner.next();
System.out.print("Enter employee's State ........");
String empState = scanner.next();
System.out.print("Enter employee's zip code .....");
int empZip = scanner.nextInt();
System.out.print("Enter employee's job title ....");
String empJob = scanner.next();
while(true) {
System.out.print("Enter employee's salary .......");
empSalary = scanner.nextInt();
if (empSalary < 200000)
break;
else
System.out.println("Enter a salary less than $200,000!");
}
try {
FileWriter fw = new FileWriter("testfile.txt", true);
fw.write(empNum + ",");
fw.write(empFirstName + ",");
fw.write(empLastName + ",");
fw.write(empCity + ",");
fw.write(empState + ",");
fw.write(empZip + ",");
fw.write(empJob + ",");
fw.write(empSalary);
fw.write(System.lineSeparator());
fw.close();
} catch (IOException e) {
}
i++;
}
}
}
'''
Solution 1:[1]
@Roberto's answer has correctly diagnosed the problem. I would just like to point out a few other things:
There is a better way produce the output. Use
String.formatto assemble the line to be written.String line = String.format("%d, %s, %s, %s, %d, %s, %d%n", empNum, empFirstName, empLastName, empCity, empState, empZip, empJob, empSalary); fw.write(line);It is cleaner (less verbose) and also more efficient1. A
FileWriteris not buffered, so eachwritecall is likely to perform a syscall.And it avoids the mistake you made with
write(int).You should not EVER silently squash
IOException. At least print an error message.You should use try with resources to ensure that the
FileWriteris always closed. If you don't there is a risk of a resource leak, which may lead to other problems.Repeatedly opening, appending and closing the same file is not the most efficient way1 to do output.
Your code won't deal with cases where the user enters non-integer values where an integer is required. The user will see a stacktrace.
You code will get break if a string field value (for example the city name, or job title) is two or more words; e.g. "New York", or "Deputy Chief Bottle Washer".
US Zip codes are not integers. They can have hyphens embedded in them
The output format doesn't cope with any cases where a field (e.g. a job title?) might contain commas.
1 - Efficiency is typically not the most important issue. However, it is a good thing to be aware of it. Like ... I remember a couple of instances in my first job where someone else wrote some grossly inefficient code out of ignorance and it turned out to be really important.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
