NoahMoonWhisper NoahMoonWhisper - 22 days ago 3
Java Question

[Java]How do you create a text file to store a single number and constantly update that number?

This is the code I came up with to try to store a single number that will be constantly changed, but I am getting errors, I am doing this in Java GUI. I can't understand why it doesn't work. First time posting here on stackOverFlow.

public void addPoints(int points, String fileName) throws IOException
{
String name = (fileName + ".dat");
File file = new File(name);
if (!file.exists())
{
file.createNewFile();

}
try (BufferedReader inStream = new BufferedReader(new FileReader(file)))
{
String inString;
int rndInt;

while((inString = inStream.readLine()) != null)
{

rndInt = Integer.parseInt(inString);
points += rndInt;
clearFile(fileName);
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(String.valueOf(points));
bw.close();

}
inStream.close();
}


}


Error I am getting

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.parseInt(Integer.java:615)
at ComputerScienceIAGUI.addPoints(ComputerScienceIAGUI.java:1080)
at ComputerScienceIAGUI.jButton12ActionPerformed(ComputerScienceIAGUI.java:856)
at ComputerScienceIAGUI.access$1300(ComputerScienceIAGUI.java:25)
at ComputerScienceIAGUI$14.actionPerformed(ComputerScienceIAGUI.java:576)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)


Also the code for the Jbutton which is the only other reason I can think of.

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
try
{

addPoints(Integer.parseInt(jTextField15.getText()), "Ruby");
addPoints(Integer.parseInt(jTextField16.getText()), "Amber");
addPoints(Integer.parseInt(jTextField17.getText()), "Pearl");
addPoints(Integer.parseInt(jTextField18.getText()), "Sapphire");


}
catch(IOException ioe)
{}
}


EDIT: It works!! But the code may not be the most efficient, thanks for all the help though. Here's the code that manages to create a single file storing 1 number that can be increased or decreased.

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
String rP,aP, sP, pP;
rP = jTextField15.getText();
aP = jTextField16.getText();
pP = jTextField17.getText();
sP = jTextField18.getText();
addPoints(rP, "Ruby");
addPoints(aP, "Amber");
addPoints(pP, "Pearl");
addPoints(sP, "Sapphire");
infoBox("Points have been saved!", "Message");


}
catch(IOException ioe)
{}
}


And the other part

public void addPoints(String points, String fileName) throws IOException
{
String name = (fileName + ".dat");
File file = new File(name);
if (!file.exists())
{
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write("0");
bw.close();

}
try (BufferedReader inStream = new BufferedReader(new FileReader(file)))
{
String inString;
int rndInt;

if((inString = inStream.readLine()) != null)
{

rndInt = Integer.parseInt(inString);


int number = Integer.parseInt(points) + rndInt;
clearFile(fileName);
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(String.valueOf(number));
bw.close();
inStream.close();
}
else
{
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write("0");
bw.close();
}
}


}

Answer

java.lang.NumberFormatException: For input string: ""

As per Java Documentation and I quote it here :

NumberFormatException is thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

Now your file creation and reading part is fine since you are already doing a null check, so it will not fail there.

I think that empty string is coming from any of the JTextField, so you need to check for a null or empty String before parsing it into Integer.

You can use a Guava Library which has a nice utility method #Strings.isNullOrEmpty

Note : If you can post the line number with the code snippet it can help us identify the issue better, till then try with this

Comments