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

    [java] convertire stringa in int

    public void listenSocket(String ser,String s){
    //Create socket connection

    try{

    DecimalFormat df = new DecimalFormat();
    Number n = df.parse(s);
    int por=n.intValue();
    socket = new Socket(ser,por);
    ..............

    bash-2.05b$ javac SocketServer.java
    SocketServer.java:65: unreported exception java.text.ParseException; must be caught or declared to be thrown
    Number por = df.parse(s);
    ^

    Io passo stringhe ser e s, e dovrei trasformare in int la stringa s, invece
    ottengo l'errore riportato.

    Helpme!

  2. #2
    Utente di HTML.it L'avatar di Angelo1974
    Registrato dal
    Feb 2003
    Messaggi
    1,107
    Ciao. Mi sa che semplicemente non hai catturato l'eccezione che può generarsi quando parsi la stringa s. Il blocco try/cacth dovresti farlo così:

    try{
    .
    .
    .
    .
    .
    .
    .
    }catch( IOException io ){
    }catch( ParseException pe ){
    }catch( Exception e ){
    }

    Così dovrebbe andare. Ciao
    Se vuoi trovare l'arcobaleno, devi sopportare la pioggia

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    1,798
    Dalle API:
    codice:
    public Number parse(String source)
                 throws ParseException
    
        Parses text from the beginning of the given string to
        produce a number. The method may not use the entire
        text of the given string.
    
        See the parse(String, ParsePosition) method for more
        information on number parsing.
    
        Parameters:
            source - A String whose beginning should be parsed. 
        Returns:
            A Number parsed from the string. 
        Throws:
            ParseException - if the beginning of the specified 
            string cannot be parsed.
    Slack? Smack!

  4. #4
    Ok ho capito perche da l'eccezione, ma come converto la stringa in int?

  5. #5
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    1,798
    codice:
    class String2Int {
            public static void main(String args[]) {
                    try {
                            int intero = Integer.parseInt(args[0]);
                            System.out.println("Valore: " + intero);
                    } catch (NumberFormatException e) {
                            System.out.println("Il contenuto della stringa " + args[0] + " non è un int.");
                    }
            }
    }
    Slack? Smack!

  6. #6
    int a = Integer.valueOf("stinga da convertire").intValue();

    ciao
    ....ma i computer non sono tanto macchine quanto strumenti di ampliamento della mente ed un diverso tipo di mezzo espressivo.
    -- Chi si accontenta non gode si illude --

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 © 2025 vBulletin Solutions, Inc. All rights reserved.