Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Weird String .length() method

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Weird String .length() method

    public class BinaryToDecimalRecursive {
     
        public static void main(String[] args) {
     
            int decimal = toDecimal("10");
        }
     
        public static int toDecimal(String str) {
     
            System.out.println(str.length()); // this was printing 3 before!
     
            if (str.length() == 1) {
     
                return Integer.parseInt(str);
            }
            else {
     
                return 0;
            }
        }
    }

    as i was playing with my BinaryToDecimal recursive program, i bumped into something that makes me confuse, the code above is something i did to reproduce the error i encountered a while ago, but i was unable to produce it again, as you can see im passing a String argument with ofcourse(obviously) has a length of 2, but the weird thing i saw a while ago, when i tried to print its length, it gave me a length of 3, how come? ichecked and checked it, create another program where the length method call returns a valid string length, i close the code tab, ran some other programs, go back to this one again, then boom! it gave me a length of 2. Im sorry but i cant reproduce it again, im just curious why did it happen.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Weird String .length() method

    Well, like you said, this code does not produce that result, so it's going to be pretty hard to diagnose. I would have tried printing out the String immediately before printing out its length.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Weird String .length() method

    i just did every possible things in terms of code(printing out) just to monitor the behavior of the program, i wasn't actually expecting a very definitive response, i was just hoping that someone could have experience a very very weird thing just like this or may an idea to where i can start to narrow down my search, something like about the JVM? or is there a virus that could affect the runtime of a program in JVM? well i got a loads of viruses in my unit as of now. this will be an added risk-to-be-aware in case i do some things again and avoid the oh-my-god-whats-going-on thing just like what happened a while ago.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Weird String .length() method

    I would highly, highly suspect that the problem has nothing to do with the JVM (other than you need the JVM to run it) or any viruses you might have. A problem like this is almost always (99.9% of the time) due to the code not running how you think it is- not because the code is wrong, but because you misunderstood what the code would do.

    I'm not trying to sound mean, but it's important to know that this is almost definitely your fault as a programmer, not the fault of some bug or other issue outside of your control- you wrote code that resulted in unexpected behavior. It's your job to debug it and figure out where your expectations differ from reality.

    You say you printed out the String. What was its value? How did it get that value? Where did that value come from?

    Again, I'm not trying to sound mean or harsh, but I think it's a good practice to start solving a problem by asking "what did I do wrong or misunderstand?" instead of assuming the blame belongs elsewhere.

    Recommended reading: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    chronoz13 (July 11th, 2012)

  6. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Weird String .length() method

    yes and thank you, i might just have over looking on something , and i actually found that there is no way a virus can breach the JVM or any bytecode that it loads.

Similar Threads

  1. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  2. Maximum length of a string
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 6th, 2012, 09:47 AM
  3. [SOLVED] Problem using the length of a string to bound the number of iterations of a for loop
    By dtitt3 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 3rd, 2011, 01:44 PM
  4. text.length method problem?
    By computercoder in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 3rd, 2010, 10:40 AM
  5. Comparing Strings only using the .length() method - possible?
    By Ryker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2010, 05:52 PM