Olá galera!!
Tenho um campo chamado desconto do tipo string em que pode ser armazenado números da forma: 2 ou 2,4, por exemplo. Uso o código abaixo para eliminar a vírgula e transformá-la em ponto.
String str = tfDescontoAdicional.getText().replaceAll("\.", "");
str = str.substring(0, str.lastIndexOf(",")) + "." + str.substring(str.lastIndexOf(",")+1, str.length());
O problema ocorre quando não há vírgula no número, ocorre o seguinte erro.
[quote]Exception in thread “AWT-EventQueue-0” java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at br.com.sstintas.view.VendaView$Foco3Listener.focusLost(VendaView.java:1270)
at java.awt.AWTEventMulticaster.focusLost(Unknown Source)
at java.awt.Component.processFocusEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(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.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)
[/quote]
Gostaria de saber como faço para verificar se o número possui vírgula, pois caso não haja não uso a transformação.
Alguma sugestão :?:
Obrigada