-3

I am giving int dmcdw the value 'd' and want to catch the error via an Exception, but it isnt working.

here is a part of my code:

private int dmcdw = d;
private String cdw = "w";

private void cdwPlausi()
{
    try
    {

        if (dmcdw > 0 ^ cdw.substring(0).equalsIgnoreCase("w"))
        {
            //
        }
        else
        {
            //
        }
    }
    catch (NumberFormatException ex)
    {
        //
    }
}

I used NumberFormatException, but it is not working for me, what am I doing wrong?

here is the error message that shows up in my console:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 

d cannot be resolved to a variable

at importiert.Importiert_Tarif.<init>(Importiert_Tarif.java:19)
at frame.Frame_Main$2.actionPerformed(Frame_Main.java:228)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Jens
  • 67,715
  • 15
  • 98
  • 113
Angry Red Panda
  • 419
  • 1
  • 5
  • 28

1 Answers1

0

Your code will throw a compile time error. You are trying to assign a character 'd' to an int.

to get the number format exception, try

int dmcdw;
try{
dmcdw = Integer.parseInt("d");
}catch(NumberFormatException ex){
// do something with the error
}
Erik Nyström
  • 537
  • 4
  • 9
Jitin Kodian
  • 471
  • 4
  • 14
  • still throws out this Error : "Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "d"" – Angry Red Panda Jan 13 '16 at 09:53
  • Hmm, its still not working. Isnt somehow possible to just throw out a error message instead of trying to convert the String into an int? – Angry Red Panda Jan 13 '16 at 10:02
  • you should read about try catch block. In the above code, only when we try to convert the string to int will the Numberformatexception be thrown. You can write your own error message in the catch block. And since it is an error, use System.err.println(); – Jitin Kodian Jan 13 '16 at 10:24