Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java -  esercizio che non riesco a capire [RISOLTO]
Forum - Java - esercizio che non riesco a capire [RISOLTO]

Avatar
Sventrip (Normal User)
Newbie


Messaggi: 19
Iscritto: 20/07/2008

Segnala al moderatore
Postato alle 19:59
Giovedì, 07/01/2010
Sto ripassando le eccezioni e nel libro da dove studio ho provato a fare un esercizio dove chiede di creare una classe con il main(), un metodo reverse() che prende come argomento una stringa e ritorna la stringa al contrario, dice anche che se si passa la stringa al metodo con lunghezza 0 si presenta una eccezione che devo poi arrangiare(questo lo scopo), ma a me non mi da nessuna eccezione passando una stringa di length 0, ce qualcosa che mi sfugge , illuminatemi :)

Codice sorgente - presumibilmente Java

  1. public class Propagate {
  2.  
  3.         public static void main (String args[]) {      
  4.         String r ="";  
  5.         reverse(r);    
  6.         }
  7.         static  String reverse(String s){
  8.                
  9.                 String reverseStr= "";
  10.                 for (int i= s.length()-1; i>=0; --i){
  11.                         reverseStr += s.charAt(i);
  12.                         }
  13.                 return reverseStr;
  14. }
  15. }


Ultima modifica effettuata da Sventrip il 14/01/2010 alle 18:27
PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6402
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 21:20
Giovedì, 07/01/2010
Devi essere tu a controllare prima di usare la stringa, la sua lunghezza. E se è zero devi generare una exception


Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
PM Quote
Avatar
Sventrip (Normal User)
Newbie


Messaggi: 19
Iscritto: 20/07/2008

Segnala al moderatore
Postato alle 19:25
Martedì, 12/01/2010
capisco ma non mi e' chiaro del tutto , perche la dichirazione delle eccezioni non e' ancora stata affrontata nel capitolo, il try e il catch vanno messi nel main o sbaglio? se potresti farmi un esempio sul codice che ho postato ti sarei grato .thanks

PM Quote
Avatar
netarrow (Admin)
Guru^2


Messaggi: 2502
Iscritto: 12/05/2004

Segnala al moderatore
Postato alle 11:42
Mercoledì, 13/01/2010
Codice sorgente - presumibilmente Java

  1. public class Propagate {
  2.        
  3.     public static void main (String args[]) {    
  4.                 String r ="";
  5.                 try {    
  6.                         reverse(r);    
  7.                 } catch(Exception ex) {
  8.                         ex.printStackTrace();
  9.                 }
  10.     }
  11.        
  12.     static  String reverse(String s) throws Exception {
  13.                
  14.         if(s.length() == 0) throw new Exception("Stringa vuota");
  15.                
  16.         String reverseStr= "";
  17.         for (int i= s.length()-1; i>=0; --i){
  18.             reverseStr += s.charAt(i);
  19.                 }
  20.         return reverseStr;
  21.         }
  22. }



Qui ho usato una generica classe Exception, sarebbe da creare una tua eccezione che estende Exception tipo NullStringException.

PM Quote
Avatar
Sventrip (Normal User)
Newbie


Messaggi: 19
Iscritto: 20/07/2008

Segnala al moderatore
Postato alle 19:23
Mercoledì, 13/01/2010
Propagating and Catching an Exception
In this exercise you're going to create two methods that deal with exceptions. One of the methods is the main() method, which will call another method. If an exception is thrown in the other method, main() must deal with it. A finally statement will be included to indicate that the program has completed. The method that main() will call will be named reverse, and it will reverse the order of the characters in a String. If the String contains no characters, reverse will propagate an exception up to the main() method.
-     Create a class called Propagate and a main() method, which will remain
     empty for now.
-     Create a method called reverse. It takes an argument of a String and returns
     a String.
-     In reverse, check if the String has a length of 0 by using the
     String.length() method. If the length is 0, the reverse method will
     throw an exception.
-     Now include the code to reverse the order of the String.
-   Now in the main() method you will attempt to call this method and deal with
any potential exceptions. Additionally, you will include a finally statement
that displays when main() has finished.

Questo esercizio viene proposto prima della dichiarazione ed estensione delle eccezioni, anche io avevo pensato a una soluzione come la tua ma perche sono andato avanti a leggere..avendo solo try catch e finally come si potrebbe fare?? e' questo il mio dilemma :)

PM Quote
Avatar
netarrow (Admin)
Guru^2


Messaggi: 2502
Iscritto: 12/05/2004

Segnala al moderatore
Postato alle 19:42
Mercoledì, 13/01/2010
L'alternativa sarebbe far scaturire l'eccezione in un altro modo, indiretto.
Ad esempio "bucando" la stringa accedendo alla 2 lettera quando invece c'è solo una, e questo genera una ArrayIndexOutOfBoundsException (o un'altra eccezione simile con nome diverso, non ricordo).

Ma sta di fatto che il testo dice di "controllare la dimensione e lanciare un'eccezione"

Testo quotato


In reverse, check if the String has a length of 0 by using the
     String.length() method. If the length is 0, the reverse method will
     throw an exception.



questo può voler dire solo quello che abbiamo fatto.

Poi per il finally basta che lo aggiungi alla fine dopo il catch e con un println dici che è finito il programma.

Codice sorgente - presumibilmente Java

  1. ...
  2. } catch(blablbla) {
  3. blablabla
  4. } finally {
  5. blablabla
  6. }
  7. ...




Ultima modifica effettuata da netarrow il 13/01/2010 alle 19:45
PM Quote
Avatar
Sventrip (Normal User)
Newbie


Messaggi: 19
Iscritto: 20/07/2008

Segnala al moderatore
Postato alle 18:26
Giovedì, 14/01/2010
oook diciamo che e' risolto allora ;) :k: sbagliano anche nei libri questo e' da tenere conto...

PM Quote