Oppure

Loading
07/01/10 18:59
Sventrip
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 :)

public class Propagate {

	public static void main (String args[]) {	
	String r ="";	
	reverse(r);	
	}
	static  String reverse(String s){
		
		String reverseStr= "";
		for (int i= s.length()-1; i>=0; --i){
			reverseStr += s.charAt(i);
			}
		return reverseStr;
}
}
Ultima modifica effettuata da Sventrip 14/01/10 17:27
aaa
07/01/10 20:20
Devi essere tu a controllare prima di usare la stringa, la sua lunghezza. E se è zero devi generare una exception
12/01/10 18:25
Sventrip
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
aaa
13/01/10 10:42
netarrow
public class Propagate { 
	
    public static void main (String args[]) {     
		String r ="";
		try {     
			reverse(r);     
		} catch(Exception ex) {
			ex.printStackTrace();
		}
    } 
	
    static  String reverse(String s) throws Exception { 
		
        if(s.length() == 0) throw new Exception("Stringa vuota");
		
        String reverseStr= ""; 
        for (int i= s.length()-1; i>=0; --i){ 
            reverseStr += s.charAt(i); 
		} 
        return reverseStr; 
	} 
}


Qui ho usato una generica classe Exception, sarebbe da creare una tua eccezione che estende Exception tipo NullStringException.
aaa
13/01/10 18:23
Sventrip
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 :)
aaa
13/01/10 18:42
netarrow
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"

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.

...
} catch(blablbla) {
blablabla 
} finally {
blablabla
}
...


Ultima modifica effettuata da netarrow 13/01/10 18:45
aaa
14/01/10 17:26
Sventrip
oook diciamo che e' risolto allora ;) :k: sbagliano anche nei libri questo e' da tenere conto...
aaa