a problem wtih my loop!! please have a look !!

this is the first node for the program master mind

Actually i need to enter a string, then i need to check whether its of 4 characters and if all letters are in the liste( a,b,c,d,e,f)

until these 2 conditions are nt satisfied , i need to continue to accept strings from keyboard.

so here i did a lot of manipulations, i know tat some are useless, they r just to help me to understand much better

so if u can find a solution; please do help me , coz im really getting mad

thank you!!!

//*************************************************************************

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!MASTER MIND !!!!!!!!!!!!!!!!!!!!!!!!!!!!

//*************************************************************************

int nbEchecs=0,nbpresent=0,j;int nbtrouves =0;int nblettre;int

nIndices=0;int i,c=0,k=0,h=0,p=0;int nbtrouves2=0, nbtrouves1 =0;

boolean bgagne,bAbsent;bgagne=false;

boolean present,pluslong=false;

String cmot1,cLettre;

char [] cDevine =newchar[10];

char [] cLettres =newchar[26];

char [] couleurs ={'A','B','C','D','E','F'};

BufferedReader clavier =new BufferedReader(new

InputStreamReader(System.in));

System.out.println("Entrer la combinaison secrete a deviner avec le code A, B, C, D, E, F");

cmot1=clavier.readLine();

nblettre=cmot1.length();

cmot1=cmot1.toUpperCase();

cDevine = cmot1.toCharArray();

present=true;

//******** to check if it the colors are from a to f n length is 6

do

{

if(cmot1.length()!=4)

pluslong=true;

else

pluslong=false;

for(k=0;k<nblettre;k++)

for(int g=0;g<6;g++)

if(cDevine[k]!=couleurs[g])

c=c+1;

if(c>1)

present=false;

else

present=true;

if(present==false || pluslong==true)

{

System.out.println("Il y a plus que 4 characteres ou Vous avez rentrez une couleur qui nest pas valide!!!");

System.out.println("Recommencer, entrer une combinaison");

cmot1=clavier.readLine();

cmot1=cmot1.toUpperCase();

cDevine = cmot1.toCharArray();

nblettre=cmot1.length();

}

}while(present==true && pluslong ==false);

[4103 byte] By [roseddeeepssa] at [2007-10-2 12:15:57]
# 1

1.) real http://www.mindprod.com/jgloss/unmainnaming.html item 30: Hungarian Notation, please don't use it!

2.) please clean up your code. Newlines are not that expensive! Put each variable declaration into it's own line. It's much more readable ...

3.) and if you find that you've got too many variables for it to look clean (for me that's around 5 different variables in a method) , you might want to extract some functionality into it's own method (for example "boolean isLegalInput(String input)")

4.) You might want to look into regular expressions (hint: look at the Pattern class, try "[a-fA-F]{4}".

JoachimSauera at 2007-7-13 9:01:39 > top of Java-index,Java Essentials,New To Java...
# 2

Have a look at this little method:boolean isCorrect(String input) {

//

// the simple checks go first:

//

if (input == null || input.length() != 4) return false;

//

// check if the chars are all unique in the set A ... F

//

int count= 0;

for (char c= 'A'; c <= 'F'; c++)

if (input.indexOf(c) >= 0)

count++;

//

// return the result:

//

return count == 4;

}

... pay special attention to that for-loop;

kind regards,

Jos

JosAHa at 2007-7-13 9:01:39 > top of Java-index,Java Essentials,New To Java...
# 3

Hey if u dont mind can u explain me how this loop works?

And tanx again, but we didt start to use methods in our programs, tats y i complicated it by writing again n again the same code

but im trying to insert this function in it

tanx a lot

int count= 0;

for (char c= 'A'; c <= 'F'; c++)

if (input.indexOf(c) >= 0)

count++;

roseddeeepssa at 2007-7-13 9:01:39 > top of Java-index,Java Essentials,New To Java...
# 4

> Hey if u dont mind can u explain me how this loop works?

Mind if i do?

; )

int count= 0;

// char's are (in a way) just like int's

// This for-loop starts with A then increases

// to B, C, D, E and the last in the line: F

for (char c = 'A'; c <= 'F'; c++) {

// indexOf(...) is a method from the String class;

// see http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

//

// indexOf(...) return -1 if a char is NOT in the String 'input'

// So if a char from your loop (A,B,C,D,E or F) is indeed in the

// String 'input', then the counter is increased with 1.

if (input.indexOf(c) >= 0) {

// you could also do:

//count = count + 1;

// or:

//coutn += 1;

// It's all the same.

count++;

}

}

prometheuzza at 2007-7-13 9:01:39 > top of Java-index,Java Essentials,New To Java...
# 5
Oh cooollllThank you very much !!!U explained so well tat in my life i wotn ever doubt on this function lol !!!Anyway tanx a lottthave a gud dayAND HAPPY VALENTINES WISHES TO U ALL !!!!!!!
roseddeeepssa at 2007-7-13 9:01:39 > top of Java-index,Java Essentials,New To Java...
# 6

> Oh cooollll

>

> Thank you very much !!!

> U explained so well tat in my life i wotn ever doubt

> on this function lol !!!

You're welcome.

> Anyway tanx a lottt

>

> have a gud day

>

> AND HAPPY VALENTINES WISHES TO U ALL !!!!!!!

Yes, you too... Or, yes, U 2!

; )

prometheuzza at 2007-7-13 9:01:39 > top of Java-index,Java Essentials,New To Java...
# 7
hehei know wat you will think after reading thisbutwhat to do?can u just also tell me how to declare and use this method iscorrect?
roseddeeepssa at 2007-7-13 9:01:39 > top of Java-index,Java Essentials,New To Java...