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++;
}
}
}
'''