Bonjour,
On m'a demandé de faire un petit programme qui permet au client de saisir une phrase , et qu'on retourne chaque mot a part.
pour ça j'ai implémenté ce code qui me parait assez logique mais il affiche seulement le premier mot.
par exemple si le client a saisi "Hello world" on doit afficher :
Hello
world
Par contre mon programme affiche:
Hello.
Merci.
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
 
package fr.orsay.td01;
import java.util.Scanner;
 
public class td1_exo1_2 {
	public static void main(String[] args) {
		Scanner sc	=	new Scanner(System.in);
		System.out.print("Entrez votre phrase SVP");
		String phrase	=	sc.nextLine();
		char []	tab	=	phrase.toCharArray();
		char test	=	' ';
		String mot="";
		String tab1[]	=	new String[phrase.length()];
		int i	=	0;
		int j	=	0;
 
			while(i<phrase.length()) {
				if(tab[i]!=test) {
						mot	=	mot	+	tab[i];
				}
				if (tab[i]==test) {
					tab1[j]	=	mot;
					j++;
					mot	=	"";
				}
 
				i++;
			}
		for(int k=0;k<j;k++) {
			System.out.println(tab1[k]);
		}
 
		}
 
}