[RESOLVIDO] como resolver esse java.lang.NumberFormatException

8 respostas
E

galera, estou com o seguinte problema com esse java.lang.NumberFormatExceptio,
eu preciso que aceite números negativos.

Exception occurred during event dispatching:
java.lang.NumberFormatException: For input string: "-R$ 98,90"
	at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
	at java.lang.Double.parseDouble(Unknown Source)
	at views.JanelaLocacao$12.actionPerformed(JanelaLocacao.java:818)
	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.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.Dialog$1.run(Unknown Source)
	at java.awt.Dialog$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.awt.Dialog.show(Unknown Source)
	at java.awt.Component.show(Unknown Source)
	at java.awt.Component.setVisible(Unknown Source)
	at java.awt.Window.setVisible(Unknown Source)
	at java.awt.Dialog.setVisible(Unknown Source)
	at views.MenuOpcoes$6.actionPerformed(MenuOpcoes.java:265)
	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.AbstractButton.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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.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)

[b]Alguem tem uma idéia, desde já agradeço

8 Respostas

kaique

Você precisa remover tudo que não fizer parte do número (o cifrão e o indicador da moeda) e troca a vírgula por ponto.

[]'s.

S

OLHA VOCÊ DEVIAS POSTAR O CODIGO PARA TE DARMOS UMA AJUDA, MELHOR, MAS DESDE JA A RESPOSTA DO OUTRO MANO É VALIDA, NUM TIPO NUMERICO O CIFRÃO NÃO PODE IR SENÃO DARA ERRO, CRIA UMA CLASSE MOEDA COM OS CAMPOS VALOR E MOEDA(CURRENCE) COMO POSTADO ABAIXO
ficheiro moeda.java

public class moeda
{
     private double quantia;
     private String  currency;
    
    
    public moeda()
    {
    	this.quantia=0;
    	this.currency="não definido";
    }
    
    
    public moeda(double quantia)
    {
    	this.quantia=quantia;
    	this.currency="não definido";
    }
    
    public moeda(String currency)
    {
    	this.quantia=0;
    	this.currency=currency;
    }
    
    public moeda(double quantia, String currency)
    {
    	this.quantia=quantia;
    	this.currency=currency;
    }
    
    
    
   public void setQuantia(double quantia)
    {
        this.quantia=quantia;
    }
   public double getQuantia()
  {
     return this.quantia;
   }
  public void setCurrency(String currency)
  {
       this.currency=currency;
  }
  public String getCurrenyc()
  { 
          return this.currency;

   }
}

ficheiro usarMoeda.java

public class usarMoeda {

   public static void main(String[] args)
   {
   	  moeda bufunfa=new moeda();
   	  moeda larjan=new moeda(100,"reais");
   	  moeda capieque=new moeda(1224);
   	  moeda massa=new moeda("USD");
   	  moeda lequite=new moeda(3344,"USD");
   	  
   	  System.out.println ("quantia: "+lequite.getQuantia ()+"   moeda: "+lequite.getCurrenyc ());
   }
    
    
}

axo que isso pode te ajudar a resolver, mas se não te ajudar vamos esperar a opionião de outros manos
:slight_smile: 8) :idea:

S

OLHA MANO NA VERDADE JAVA É ORIENTADO A OBJECTOS EU DEMOREI MUITO PARA ENTENDER O QUE ISSO SIGNIFICA, MAS ENTENDI BEM A ALGUM TEMPO ATRAS.
SE VOCE QUERES GUARDAR VALORES MONETARIOS NUMA VARIAVEL TIPO: 334 USD, 344 $
AI TEM UMA PARTE QUE É A QUANTIA OUTRA PARTE É A MOEDA, ENTAO SEM PENSAR 2 VEZES DEVES CRIAR UM NOVO TIPO DE DADOS
(CLASSE JAVA) QUE PODERA REPRESENTAR A QUANTIA E A MOEDA.
E APROVEITANDO A OPORTUNIDADE DA CLASSE CRIADA PODES POR ALGUMAS FUNCIONALIDADES DENTRO DA CLASSE ( METODOS).
SE ANALISARMOS COM MAIS CALMA 34444 USD, O VALOR 34444 E A MOEDA USD SÓ TEM SIGNIFICADO SE ESTIVEREM JUNTOS
VALOR MOEDA ( 34444 USD) ENTAO AI NOTAMOS QUE PRECISAMOS TER UMA CLASSE QUE REPRESENTA ISTO,
EU FICO TRISTE POR TAMBEM DESCOBRIR ISSO MUITO TARDE SÓ QUANDO COMEÇEI A ESTUDAR PARA A CERTIFICAÇÃO É QUE COMEÇEI A ENTENDER BEM CERTAS COISA
:slight_smile: AQUELE ABRAÇO

bestlinux

Cara, não precisa usar o CAPS não.rsrs :slight_smile:

Vinny

FAZ assim

seuValor = seuValor.replace("R$","");
seuValor = seuValor.replace(",",".");

ve se da certo depois de fazer isso!

E

obrigado vinny deu certo, agradecimento ao sulito aí tbm, vou seguir seu conselho e dar uma estudada mais sobre isso.

Vinny

Coloca um
[RESOLVIDO] antes do nome do tópico
para se outras pessoas estiverem com o mesmo
problema possam acessar este Tópico e ver que foi resolvido!

Vlw.

E

OK, RESOLVIDO

Criado 3 de dezembro de 2009
Ultima resposta 3 de dez. de 2009
Respostas 8
Participantes 5