I am making a simple tutorial program in which the user inputs some data, then that data is written into a textfile, then the textfile is read and the data displayed. However, not all of the data is being written into the textfile. Please view the following:
This is my code:
package chasi.fecalMatter.ExcrementalProgram;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
public class RedundantExcrement {
static String inputName = JOptionPane.showInputDialog("Please input your FULL name:");
static String inputUsername = JOptionPane.showInputDialog("Please input your desired Username:");
static String inputJobTitle = JOptionPane.showInputDialog("Please input your Job title:");
static String inputSalary = JOptionPane.showInputDialog("Please input your monthly salary:");
static int convertedSalary = 0; /*Convert salary from String to int*/
static int benefitDeduction = 0;
static String empFileName = inputUsername+"INFO";
static BufferedWriter bwriter = null;
public static void main (String[] args) throws IOException {
//Catch NumberFormatException.
try {
Integer.parseInt(inputSalary);
} catch (NumberFormatException nfEx) {
JOptionPane.showMessageDialog(null, "Please ensure that " +
"your salary is entered in NUMERIC form.", "ERROR!", 2);
return;
}
//Specify instructions as regard salary -to- Benefit deduction ratio
if (convertedSalary >= 3500) {
benefitDeduction = 300;
} else {
benefitDeduction = 180;
}
try { /*Catches IOException AND NullPointerException*/
FileWriter fwriter = new FileWriter(empFileName);
bwriter = new BufferedWriter(fwriter);
bwriter.write(inputName);
bwriter.newLine();
bwriter.write(inputJobTitle);
bwriter.newLine();
bwriter.write(inputSalary);
bwriter.newLine();
bwriter.write(benefitDeduction);
bwriter.newLine();
bwriter.write("----------");
bwriter.newLine();
} catch (IOException | NullPointerException ionpEx) {
JOptionPane.showMessageDialog(null, "An ERROR has occured", "ERROR!", 2);
return;
} finally { /*CLOSE the writer*/
try{
if (bwriter != null) {
bwriter.close();
}
} catch (IOException ioEx2) {
JOptionPane.showMessageDialog(null, "ERROR!");
return;
}
}
}
}
As you can see, I use the int convertedSalary
to convert the user-inputted inputSalary
from a String into a number. In my method, I use
try {
Integer.parseInt(inputSalary);
} catch (NumberFormatException nfEx) {
JOptionPane.showMessageDialog(null, "Please ensure that " +
"your salary is entered in NUMERIC form.", "ERROR!", 2);
return;
}
In order to covert it. It is later (supposed) to be written by the BufferedWriter.
I also have this code:
try {
Integer.parseInt(inputSalary);
} catch (NumberFormatException nfEx) {
JOptionPane.showMessageDialog(null, "Please ensure that " +
"your salary is entered in NUMERIC form.", "ERROR!", 2);
return;
}
In order to change the value of my benefitDeduction
depending on the value of the user's salary.
HOWEVER, the benefitDeduction
is written into the text file, but instead, I get this:
Thanks if you can help!
Look at the API.
public void write(int c)
throws IOException
Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.
This means that the int is actually a char and will be converted according to Unicode/ASCII table.
To write the integer value use bwriter.writer(String.valueOf(benefitDeduction));