Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [JAVA]problema conversione string-int-string

    buonasera a tutti sono terek, nuovo del forum e nuovo di questo linguaggio. ho fatto un piccolo programmino in java sulle coniche e con netbeans vorrei fare anche la parte grafica soltanto che ho un piccolo problema con la conversione del tipo string in int e viceversa: praticamente ho alcuni jtextfield in cui vanno inseriti i coefficienti e il tasto parabola che legge i vari parametri e calcola tutto quanto e lo stampa come testo di un jlabel. Soltanto che non riesco a far leggere il testo che si trova nella textfield, convertirlo in int e poi farlo riconvertire per poterlo inserire come testo nel jlabel... il codice sorgente che ho scritto è questo ( metto solo quello riguardante il punto in questione):


    codice:
    private void jButton1MousePressed(java.awt.event.MouseEvent evt) { 
    java.lang.String lab1, lab2, lab3, lab4;
    int a, b, c; double delta, vx, vy, fy, dir; 
    lab1 = jTextField1.getText(); 
    lab2 = jTextField2.getText(); 
    lab3 = jTextField3.getText(); 
    a = Integer.parseInt(lab1); 
    b = Integer.parseInt(lab2); 
    c = Integer.parseInt(lab3); 
    lab4 = String.valueOf(a); 
    jTextArea1.setText(lab4);
    grazie in anticipo

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: [JAVA]problema conversione string-int-string

    Originariamente inviato da terek
    Soltanto che non riesco a far leggere il testo che si trova nella textfield, convertirlo in int e poi farlo riconvertire per poterlo inserire come testo nel jlabel...
    E quale è il problema o dubbio di preciso? Il codice postato che vedo (anche se fosse parziale) mi pare comunque corretto almeno tecnicamente come sintassi e invocazioni.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    sembra giusto ma mi da numerosi errori riguardo a parse int...

    POCHI errore


    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at desktopapplication1.DesktopApplication1View.jButto n1MousePressed(DesktopApplication1View.java:317)
    at desktopapplication1.DesktopApplication1View.access $800(DesktopApplication1View.java:22)
    at desktopapplication1.DesktopApplication1View$4.mous ePressed(DesktopApplication1View.java:142)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEvent Multicaster.java:280)
    at java.awt.Component.processMouseEvent(Component.jav a:6501)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:626 9)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4860)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 86)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4489)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713 )
    at java.awt.Component.dispatchEvent(Component.java:46 86)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101 )
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 677)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da terek
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    NumberFormatException devi gestirlo tu, la stringa può essere, giustamente, vuota o malformata (con caratteri errati es. "12a4").
    E visto che l'input arriva dall'utente ... segnala l'errore (in modo non "tecnico" ... non dirgli NumberFormatException! ) all'utente!
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.