5 Replies - 272 Views - Last Post: 03 April 2013 - 06:30 PM Rate Topic: ***-- 1 Votes

#1 bobrown101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-April 13

problem: java program skips for loop (have a feeling its word.length)

Posted 03 April 2013 - 02:35 PM

for(int h=0;h<charArray.length;h++){ // charArray is a charArray of a word
		    if (charArray[h] == x.charAt(0)){ // x is a single letter
		    	System.out.println(x + " is in position" + (h+1) +" of the word"); //this is just to debug
		    }
		    else {
		    	System.out.println("its not in position" + (h+1)); //this is just to debug
		    }
		}



can you guys help me figure out why it is skipping the for loop? I have a feeling it has to do with "h<charArray.length" but i'm not sure. If it helps, I'm making a hangman application and charArray is a string converted into a charArray and x is the letter that the person is guessing.

Thanks in advance,
Brady

Is This A Good Question/Topic? 0
  • +

Replies To: problem: java program skips for loop (have a feeling its word.length)

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: problem: java program skips for loop (have a feeling its word.length)

Posted 03 April 2013 - 04:50 PM

Yes. If charArray has only 1 member, this loop will be skipped.

for(h is 0; loop while h is less than charArray's length; increment h);

If charArray.length is 1 or 0, this loop will run 0 times.
If charArray.length is 2, this loop will run 1 time(because 2 is not less than 2).
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3488
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: problem: java program skips for loop (have a feeling its word.length)

Posted 03 April 2013 - 05:21 PM

View PostFlukeshot, on 03 April 2013 - 11:50 PM, said:

Yes. If charArray has only 1 member, this loop will be skipped.

for(h is 0; loop while h is less than charArray's length; increment h);

If charArray.length is 1 or 0, this loop will run 0 times.
If charArray.length is 2, this loop will run 1 time(because 2 is not less than 2).

Eh? If the length is 1 it will loop once, because 0 is less than 1, and if the length is 2 it will loop twice..
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3488
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: problem: java program skips for loop (have a feeling its word.length)

Posted 03 April 2013 - 05:26 PM

@OP If you don't think the loop is executed at all then just put a simple print statement within the loop; if it doesn't print then it confirms the loop is not executed.

Otherwise, print out the values of the variables (so you can see what is happening) and, preferably, find out how to debug (step through) your code.
Was This Post Helpful? 0
  • +
  • -

#5 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: problem: java program skips for loop (have a feeling its word.length)

Posted 03 April 2013 - 05:46 PM

View Postandrewsw, on 04 April 2013 - 12:21 AM, said:

View PostFlukeshot, on 03 April 2013 - 11:50 PM, said:

Yes. If charArray has only 1 member, this loop will be skipped.

for(h is 0; loop while h is less than charArray's length; increment h);

If charArray.length is 1 or 0, this loop will run 0 times.
If charArray.length is 2, this loop will run 1 time(because 2 is not less than 2).

Eh? If the length is 1 it will loop once, because 0 is less than 1, and if the length is 2 it will loop twice..


Right you are.. No idea what I was thinking just then.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: problem: java program skips for loop (have a feeling its word.length)

Posted 03 April 2013 - 06:30 PM

for(int h=0;h<charArray.length;h++){ // charArray is a charArray of a word


and what is a charArray ?
May be you mean an arry of String ?
for(int h=0;h<foo.length;h++){ // foo is a foo of a word



 if (charArray[h] == x.charAt(0)){ // x is a single letter


yes the only charAt() method that I know comes from the String class and yes it returns a single char... may be a letter
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1