1

I have this loop that won't stop when it should and it's only happen on my production server but not on my dev server. So I'm going crazy.

//objectName can be any value from [a-}], 
char objectName = 'a'; //objectName is initialized with value 'a'

//before objectName hit the line below, it was increment objectName++; until the value of 
//objectName = '}'
for( char c = objectName; c <= 'z'; c++ ){
    //do something
}

objectName can be any character but in my case I know that my objectName will be "}" character, which is technically more than 'z'. So what I don't understand is why is my loop being executed still.

The funny thing is we have test server which uses JAVA 1.6.0_30, and we have try testing there and it's working just fine.

My Dev Server uses : java 1.6.0_45 My Prod Server uses : java 1.6.0_30

If you see anything wrong with my logic please let me know, any circumstance that will make my loop be active even when it shouldn't feel free to let me know.

  • 1
    How are you deploying to the production server? Anything that may change character encodings? What happens if `objectName` was `'}'` as opposed to `"}"`? – nanofarad May 19 '14 at 22:33
  • @hexafraction I think that `"}"` is a typo, as if it really were a `String` I don't think that fragment would compile – awksp May 19 '14 at 22:34
  • I don't think it would compile with "}" to be honest. – user184994 May 19 '14 at 22:34
  • What is the type of objectName? Initializing it with "}" suggests String, but then you use it to initialize a char. – Patricia Shanahan May 19 '14 at 22:34
  • Post you actual code. – Braj May 19 '14 at 22:38
  • objectName is a variable of type char – user3570611 May 19 '14 at 22:41
  • I modified the code in the question a bit, hopefully it's more helpful. And yes I used single quote, I just mistype to begin with, sorry :( – user3570611 May 19 '14 at 22:46
  • @hexafraction : We use ANT to build the project then create an ear file then deploy that ear. We are using Weblogic server – user3570611 May 19 '14 at 22:51
  • I would recommend that you put some log statements with if else condition just before you hit the for loop. Also add a log statement just inside the for loop again in a if else condition. Lost of times when we think our code is working certain way it's not. I hope it helps :) – vkg May 20 '14 at 00:10
  • Run this code on your server and check the value of 'z' & '}' `System.out.println("z:" + (int)('z')); System.out.println("}:" + (int)('}'));` I test it under my JRE 1.5.0_06 with your codes and the loop is working fine. If the printout shows that } is 125 and z is 122 I don't think there's any problem with the loop, more like it could be some other issue with some other part of the codes. – Sky May 20 '14 at 01:49
  • 1
    There are chances you might change the objectName to something else, how about trying to hardcode your for loop `char c = '}'` and see if it works? If it is not looping then it might not be your loop problem. But the variable objectName might be accidentally assigned somewhere else. – Sky May 20 '14 at 02:21
  • @Sky Your comment seems to point me in the right direction the most. My code was right all along. It's the input value that I expected is wrong. The piece of code before hitting my loop which I never suspect because I did not write that piece of code so I never suspect it. Thank you. If you put your comment as an answer I will vote select your respond as the answer of this question :) ... again Thank you. – user3570611 May 21 '14 at 16:09

2 Answers2

0

In your question you say "I have this loop that won't stop when it should..." when should it stop? Currently the loop is exiting when the condition turns false. Basically when 10 >= 35.

First off, how is '}' "technically" more than 'z'?

'}' is equivalent to -1 and 'z' is equivalent to positive 35.

Using the code that you haven't commented you will loop 26 times, as expected (using char 'a' & char 'z').

'a' is equivalent to 10 and 'z' is equivalent to 35.

You can check these values by using Character.getNumericValue()

char objectName = 'a'; 
for( char c = objectName; c <= 'z'; c++ ){
    //System.out.println("test");
}

Yields the same result as:

for( int i = 10; i <= 35; i++ ){
    //System.out.println("test");
}

Unless I have missed something, this seems like an overly complicated way to achieve a really simple result.

I would have asked for clarification on something but I can't comment.

ChristianF
  • 1,735
  • 4
  • 28
  • 56
  • '}' is more than 'z' based on [ASCII](http://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html) table. Or you can cast it into an integer. Although Java uses [unicode](http://stackoverflow.com/questions/15610247/can-we-switch-between-ascii-and-unicode), I'm not sure why it uses ASCII value for for loop. Interesting. for( int i = 1; i <= 'z'; i++ ){ System.out.println(i); } Notice it prints all the way based on the ASCII value in this case under my JRE. Not sure how looping through character works. I'm guessing it's converted to ASCII value – Sky May 20 '14 at 02:04
  • After googling more, I realised UTF-8 & 16 has the same value as ASCII table. That means Character.getNumericValue() is some other special methods. So we shouldn't use this to method to convert as it doesn't includes symbol and special commands. – Sky May 20 '14 at 02:14
  • My intention is to group array of String into alphabetical group based on the string first letter. Prior to hitting this problem loop, I have gone through looping the array of string. In my case I know that my last string start with a 'z'. Therefore, when I finish grouping I increment the objectName++; one more time; which is why I know that the objectName before hitting this loop is equals to '{' or as I mistype '}', but all and all it is more than 'z' for sure. The loop in question is there just in case that the last item doesn't start with 'z' then I would not have all 26 groups. – user3570611 May 20 '14 at 13:25
0

I'm no expert so I could be wrong here but using characters to iterate through a for loop looks alien to me and probably not the best thing to be doing.

I would suggest using an array/(array)List of characters and iterating through the list that way or as @Chris said by getting the numeric value of the characters

for(int i = Character.getNumericValue(objectName); i <= Character.getNumericValue('z'); i++)
{
    //System.out.println("test");
}

Like I said I'm no expert but using numbers to iterate through for loops seems like a better practice or at least a more stable one.

Schonge
  • 332
  • 2
  • 6
  • 17