Bonsoir,
J'aimerais transformer cette méthode main en méthode "normale" qui retourne une String, de manière à pouvoir l'utiliser dans d'autres classes.
Soit avoir un entête du type : public static String stemmer(File f)
J'ai réussi à implémenter de manière à ce qu'elle prenne un File en argument, mais j'ai un soucis pour que la méthode retourne un String
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public static void main(String[] args) { char[] w = new char[501]; Stemmer s = new Stemmer(); for (int i = 0; i < args.length; i++) try { FileInputStream in = new FileInputStream(args[i]); try { while(true) { int ch = in.read(); if (Character.isLetter((char) ch)) { int j = 0; while(true) { ch = Character.toLowerCase((char) ch); w[j] = (char) ch; if (j < 500) j++; ch = in.read(); if (!Character.isLetter((char) ch)) { /* to test add(char ch) */ for (int c = 0; c < j; c++) s.add(w[c]); /* or, to test add(char[] w, int j) */ /* s.add(w, j); */ s.stem(); { String u; /* and now, to test toString() : */ u = s.toString(); /* to test getResultBuffer(), getResultLength() : */ /* u = new String(s.getResultBuffer(), 0, s.getResultLength()); */ System.out.print(u); } break; } } } if (ch < 0) break; System.out.print((char)ch); } } catch (IOException e) { System.out.println("error reading " + args[i]); break; } } catch (FileNotFoundException e) { System.out.println("file " + args[i] + " not found"); break; } } }
En fait, j'ai essayé de créé une nouvelle String textfinal qui est égale aux mots u les uns à la suite des autres (voir code ci-dessous)
Je retourne textfinal à la fin (une fois que toutes les accolades sont fermées)
Mais ça ne fonctionne pas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 { String u; /* and now, to test toString() : */ u = s.toString(); /* to test getResultBuffer(), getResultLength() : */ /* u = new String(s.getResultBuffer(), 0, s.getResultLength()); */ textfinal += u; System.out.print(u); }
J'avoue que je suis complètement perdu au milieu de toutes ces boucles while.
Partager