Page 1 of 4 1234 LastLast
Results 1 to 15 of 51

Thread: Aiutino Java

  1. #1
    Lieutenant Brcondor's Avatar
    Join Date
    Mar 2005
    Posts
    4.610

    Default Aiutino Java

    Ciao a tutti.
    Avrei bisogno di un piccolo aiuto.
    C'è un modo semplice per trovare tutte le stringhe uguali a meno di un eventuale carattere finale ?
    Chessò che riconosca scimmia scimmie come uguali, ma anche like e likes?
    Usando String.startsWith becco anche parole che hanno semplicemente lo stesso inizio, e a me non va bene.
    Usando String.Contains mi riconosce vertebrati e invertebrati e anche questo non lo voglio...
    Tipo i wildcards di SQL
    " ...e mai un pensiero non al denaro, non all'amore né al cielo... " Fabrizio de Andrè

  2. #2
    Lieutenant
    Join Date
    Feb 2004
    Location
    Bresso
    Posts
    4.683

    Default

    Dai un occhio alle regular expressions, tipo le classi java.util.regex.Pattern e java.util.regex.Matcher.
    http://docs.oracle.com/javase/7/docs...x/Pattern.html

  3. #3
    Lieutenant Commander Axet's Avatar
    Join Date
    Sep 2003
    Location
    Ginevra
    Posts
    33.807

    Default

    Quote Originally Posted by Kat View Post
    Dai un occhio alle regular expressions, tipo le classi java.util.regex.Pattern e java.util.regex.Matcher.
    http://docs.oracle.com/javase/7/docs...x/Pattern.html
    *

    I'm no hero. Never was. Never will be.
    -----
    Soul of the mind, key to life's ether
    Soul of the lost, withdrawn from its vessel
    May strength be granted so the world might be mended...
    So the world might be mended...

  4. #4
    Lieutenant Brcondor's Avatar
    Join Date
    Mar 2005
    Posts
    4.610

    Default

    Ok, grazie .
    " ...e mai un pensiero non al denaro, non all'amore né al cielo... " Fabrizio de Andrè

  5. #5
    Hador's Avatar
    Join Date
    Mar 2004
    Location
    Milano
    Posts
    31.321

    Default

    Alternativamente estendi string reimplementando l'equals eliminando l'ultimo char, poi cerchi le stringhe "uguali"

  6. #6
    Lieutenant Brcondor's Avatar
    Join Date
    Mar 2005
    Posts
    4.610

    Default

    cazzo hador, questa mi piace di più come soluzione . Mi basta segare di base l'ultimo carattere e fare un check uguali <3
    " ...e mai un pensiero non al denaro, non all'amore né al cielo... " Fabrizio de Andrè

  7. #7
    Hador's Avatar
    Join Date
    Mar 2004
    Location
    Milano
    Posts
    31.321

    Default

    a fare ste zozzerie so un campione

  8. #8
    Lieutenant Commander Axet's Avatar
    Join Date
    Sep 2003
    Location
    Ginevra
    Posts
    33.807

    Default

    Quote Originally Posted by Hador View Post
    Alternativamente estendi string reimplementando l'equals eliminando l'ultimo char, poi cerchi le stringhe "uguali"
    Elegantissimo

    I'm no hero. Never was. Never will be.
    -----
    Soul of the mind, key to life's ether
    Soul of the lost, withdrawn from its vessel
    May strength be granted so the world might be mended...
    So the world might be mended...

  9. #9
    Hador's Avatar
    Join Date
    Mar 2004
    Location
    Milano
    Posts
    31.321

    Default

    Think in OO
    La cosa migliore sarebbe usare un wrapper, ma vabbè non esageriamo

  10. #10
    Hador's Avatar
    Join Date
    Mar 2004
    Location
    Milano
    Posts
    31.321

    Default

    Code:
    public class StringProva {
    
    	String body;
    	String gender;
    	
    	public StringProva(String s) {
    		body = s.substring(0, s.length()-1);
    		gender = s.substring(s.length()-1, s.length()) ;
    	}
    
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + ((body == null) ? 0 : body.hashCode());
    		return result;
    	}
    
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		StringProva other = (StringProva) obj;
    		if (body == null) {
    			if (other.body != null)
    				return false;
    		} else if (!body.equals(other.body))
    			return false;
    		return true;
    	}
    	
    	
    
    }
    LA SOLUZIONEH DEL SOFTWARE ENGINEER
    ma se tieni quell'hashcode e usi un hashset non sei in grado di distinguere tra due istanze con "genere" diverso...

    si, i miei programmi sono tutti così
    Last edited by Hador; 17th December 2013 at 15:43.

  11. #11
    Zaider's Avatar
    Join Date
    Jan 2004
    Location
    Nella pianura della nebbia..
    Posts
    8.709

    Default

    figa hador sei squallidissimo
    gratz.

  12. #12
    Lieutenant Brcondor's Avatar
    Join Date
    Mar 2005
    Posts
    4.610

    Default

    Io ho abbastanza uno stile simile nel cercare le porcate che funzionino.
    Ho risolto così
    if(words[k].equalsIgnoreCase((term[j])) || (words[k].startsWith(term[j]) && words[k].length()<term[j].length()+2 )
    ossia se o sono uguali oppure la prima inizia con la seconda e ha al massimo 2 caratteri in più(messi 2 in più così per sicurezza, in realtà ne basta uno).
    Isipirato da hador, che ringrazio ancora, ho fatto una versione mista
    body = term[j].substring(0, term[j].length()-1);
    if(words[k].equalsIgnoreCase((term[j])) || (words[k].startsWith(body) && words[k].length()<body.length()+3))
    Così becco anche wolf-wolves
    Last edited by Brcondor; 17th December 2013 at 16:37.
    " ...e mai un pensiero non al denaro, non all'amore né al cielo... " Fabrizio de Andrè

  13. #13
    Hador's Avatar
    Join Date
    Mar 2004
    Location
    Milano
    Posts
    31.321

    Default

    oh la mia soluzione è fichissima, pochi cazzi.
    Ancora meglio, scaricherei il dump di wikidictionary, me lo caricherei in una mappa e cercherei i plurali così

  14. #14
    Lieutenant Commander Axet's Avatar
    Join Date
    Sep 2003
    Location
    Ginevra
    Posts
    33.807

    Default

    Quote Originally Posted by Brcondor View Post
    Io ho abbastanza uno stile simile nel cercare le porcate che funzionino.
    Ho risolto così
    if(words[k].equalsIgnoreCase((term[j])) || (words[k].startsWith(term[j]) && words[k].length()<term[j].length()+2 )
    ossia se o sono uguali oppure la prima inizia con la seconda e ha al massimo 2 caratteri in più(messi 2 in più così per sicurezza, in realtà ne basta uno).
    Isipirato da hador, che ringrazio ancora, ho fatto una versione mista
    body = term[j].substring(0, term[j].length()-1);
    if(words[k].equalsIgnoreCase((term[j])) || (words[k].startsWith(body) && words[k].length()<body.length()+3))
    Così becco anche wolf-wolves
    Senza tanti giri di parole: è una merda.
    Le reg exp esistono per un motivo.

    Alternativamente cercati qualcosa su prefix / suffix trees se vuoi fare il sofista.

    I'm no hero. Never was. Never will be.
    -----
    Soul of the mind, key to life's ether
    Soul of the lost, withdrawn from its vessel
    May strength be granted so the world might be mended...
    So the world might be mended...

  15. #15
    Lieutenant
    Join Date
    Feb 2004
    Location
    Bresso
    Posts
    4.683

    Default

    Quote Originally Posted by Hador View Post
    Code:
    public class StringProva {
    
    	String body;
    	String gender;
    	
    	public StringProva(String s) {
    		body = s.substring(0, s.length()-1);
    		gender = s.substring(s.length()-1, s.length()) ;
    	}
    
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + ((body == null) ? 0 : body.hashCode());
    		return result;
    	}
    
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		StringProva other = (StringProva) obj;
    		if (body == null) {
    			if (other.body != null)
    				return false;
    		} else if (!body.equals(other.body))
    			return false;
    		return true;
    	}
    	
    	
    
    }
    LA SOLUZIONEH DEL SOFTWARE ENGINEER
    ma se tieni quell'hashcode e usi un hashset non sei in grado di distinguere tra due istanze con "genere" diverso...

    si, i miei programmi sono tutti così
    Che porcheria immonda lol

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
[Output: 99.32 Kb. compressed to 84.20 Kb. by saving 15.11 Kb. (15.22%)]