Why the IF statement is skip?

Hi,

I'm a novice to Java so I would like some help from you guys out there.

This is the Individu class which I wrote;

public class Individu

{

static final int NB_INDIVIDUS=2;

private Statut[] tab_individus;

private int nbindividus;

public Individu()

{

nbindividus=0;

tab_individus = new Statut[NB_INDIVIDUS];

Scanner Entree = new Scanner(System.in);

Entree.useLocale(Locale.FRENCH);

String prof = "" ;

while (prof != "x")

{

System.out.printf("Nom de profession (x to terminate) : ");

prof = Entree.next();

if (prof != "x") //saisie du nom

{

String sit_f = "";

System.out.printf("Situation Familialle: ") ;

sit_f = Entree.next();

System.out.printf("nombre d'enfants: ") ;

int nb_enfants = Entree.nextInt();

Statut ind = new Statut(prof,sit_f,nb_enfants);

insertion_fin_de_liste(ind);

}

}

}

private void insertion_fin_de_liste(Statut nouveau_statut)

{

if (nbindividus < NB_INDIVIDUS)

tab_individus[nbindividus++] = nouveau_statut;

else affiche();

}

public void affiche()

{

System.out.printf("liste \n");

System.out.printf("prof sit_f nb_enfants\n");

for (int i=0; i<nbindividus; i++)

tab_individus.affiche_info();

System.out.printf("\n");

}

}

Together with two other classes, the program is supposed to show the liste(means end the questions), whenever I type "x", but it seems to skip that procedure and go on and asking me ><<Situation familliale>> and on. What's the fault here?

[1694 byte] By [alankoka] at [2007-11-26 22:51:42]
# 1
I can't read that.When you post code, please use[code] and [/code] tags as described in [url= http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.
jverda at 2007-7-10 12:13:54 > top of Java-index,Java Essentials,Java Programming...
# 2
> if (prof != "x")Classic newbie error. This is not the way to compare String values. All that does is compare the REFERENCES for equality, not the CONTENTS.if (!prof.equals("x"))is the way to do it.
warnerjaa at 2007-7-10 12:13:54 > top of Java-index,Java Essentials,Java Programming...
# 3
This should be in the New to Java forum really ;)
Simeona at 2007-7-10 12:13:54 > top of Java-index,Java Essentials,Java Programming...
# 4

> I can't read that.

>

> When you post code, please use[code] and

> [/code] tags as described in

> [url=http://forum.java.sun.com/help.jspa?sec=formattin

> g]Formatting tips[/url] on the message entry

> page. It makes it much easier to read.

Sorry for the trouble and thanks for the advice. Here they are again.

Class Individu

import java.util.*;

public class Individu

{

static final int NB_INDIVIDUS=2;

private Statut[] tab_individus;

private int nbindividus;

public Individu()

{

nbindividus=0;

tab_individus = new Statut[NB_INDIVIDUS];

Scanner Entree = new Scanner(System.in);

Entree.useLocale(Locale.FRENCH);

String prof = "" ;

while (prof != "x")

{

System.out.printf("Nom de profession (x pour terminer) : ");

prof = Entree.next();

if (prof != "x") //saisie du nom

{

String sit_f = "";

System.out.printf("Situation Familialle: ") ;

sit_f = Entree.next();

System.out.printf("nombre d'enfants: ") ;

int nb_enfants = Entree.nextInt();

Statut ind = new Statut(prof,sit_f,nb_enfants);

insertion_fin_de_liste(ind);

}

}

}

private void insertion_fin_de_liste(Statut nouveau_statut)

{

if (nbindividus < NB_INDIVIDUS)

tab_individus[nbindividus++] = nouveau_statut;

else affiche();

}

public void affiche(){

System.out.printf("liste \n");

System.out.printf("prof sit_f nb_enfants\n");

for (int i=0; i<nbindividus; i++)

tab_individus[i].affiche_info();

System.out.printf("\n");

}

}

I shall add in the other two classes

Class Saisie_Individus

public class Saisie_Individus

{

public static void main(String[] args)

{

Individu societe = new Individu();

societe.affiche();

}

}

and Class Statut

public class Statut

{

public String profession;

public String sit_f ;

public int n_enfants;

//int celibataire = 0;

//int mari?= 1;

// ici commence le constructeur //

public Statut(String prof,String situation_fam,int nb_enfants)

{

profession = prof;

sit_f = situation_fam;

n_enfants = nb_enfants;

}

public void affiche_info() {

System.out.printf("%-15s %-15s %5d\n",profession,sit_f,n_enfants);

}

}

>

alankoka at 2007-7-10 12:13:54 > top of Java-index,Java Essentials,Java Programming...
# 5
*AHEM*Read reply #2.
warnerjaa at 2007-7-10 12:13:54 > top of Java-index,Java Essentials,Java Programming...
# 6
That solves it. Thank you.
alankoka at 2007-7-10 12:13:54 > top of Java-index,Java Essentials,Java Programming...