Passar Int para String

3 respostas
Eduardo_Silveira

Ola estou fazendo um programa e quero que o sistema identifica o qual o numero que foi colocado no campo de texto, mas nao estou conseguindo fazer. Alguem pode ajudar?

eu tinha feito assim:

int estoquee = Integer.parseInt(Estoque.getText());

if (estoquee <= 0) {
        QuantMin.setForeground(new java.awt.Color(255, 0, 0));
       est.setForeground(new java.awt.Color(255, 0, 0));
       
   } else{
        QuantMin.setForeground(new java.awt.Color(240, 240, 240));
        est.setForeground(new java.awt.Color(240, 240, 240));
    }

mas nao deu certo

3 Respostas

rodriguesabner

pq nao deu certo? qual o erro?

Eduardo_Silveira
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:592)

at java.lang.Integer.parseInt(Integer.java:615)

at Produtos.Estoque.formWindowOpened(Estoque.java:358)

at Produtos.Estoque.access$000(Estoque.java:17)

at Produtos.Estoque$1.windowOpened(Estoque.java:71)

at java.awt.Window.processWindowEvent(Window.java:2051)

at javax.swing.JFrame.processWindowEvent(JFrame.java:305)

at java.awt.Window.processEvent(Window.java:2013)

at java.awt.Component.dispatchEventImpl(Component.java:4889)

at java.awt.Container.dispatchEventImpl(Container.java:2294)

at java.awt.Window.dispatchEventImpl(Window.java:2746)

at java.awt.Component.dispatchEvent(Component.java:4711)

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)
rodriguesabner

Esse erro é pq vc está passando uma String vazia no seu campo. Não existe o número: “”.

Vc precisa passar um número primeiro, não algo vazio… Pode fazer um tratamento pra evitar isso

if (seucampo.getText().equals("")){
    //avisa que precisa digitar algo
}
Criado 28 de janeiro de 2020
Ultima resposta 29 de jan. de 2020
Respostas 3
Participantes 2