-2

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();
            }
        }


    }
4
  • what is the error that you get exactly?
    – dammina
    Nov 10, 2016 at 4:39
  • I can't understand why it doesn't work... Posting what exactly goes wrong, or error output will help getting good answers.
    – s.d
    Nov 10, 2016 at 4:40
  • At a glance: if the file doesn’t exists, you create it. Then you read a line from it — an empty line. Then you use Integer.parseInt() to convert that empty line to an integer. That is bound to fail. So I suggest: if the file doesn’t exist, don’t try to read from it. I could be missing something, though.
    – Anonymous
    Nov 10, 2016 at 5:34
  • @OleV.V. It doesn't bound to fail since he is already having a null check if the file doesn't exist he creates one and check for that. if the file doesn’t exist, don’t try to read from it Agreed but if file not exist he is creating it and then try to read from it but according the the OP's logic he will never be able to read from that file because newly created file will always gives him null in br.readLine() method Nov 11, 2016 at 5:35

1 Answer 1

0

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

3
  • Thanks for looking deeper into it than I did. There’s no way I could see myself introducing a dependency on a 3rd party library for this simple purpose, though.
    – Anonymous
    Nov 11, 2016 at 7:02
  • I reworked the code with your suggestions and it works, thanks! Nov 11, 2016 at 8:28
  • @OleV.V. It's not mandatory to use that 3rd party library, we can create our own method to do a (null and empty check) but since it already exists hence gave a suggestion. Nov 11, 2016 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.