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 2 of 2

Thread: out of range help.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default out of range help.

    I am having a problem. When i input "na" into the textfield it should output na but instead what its doing is giving me a out of range error of 3. My thinking is that it shouldnt do that because the else statment doesnt happen because add0 = "na". What i need it to do: When i enter na it should write to file as na and when i enter 8 numbers it should run the iName variable. This is my code that i have tried i just need to know what i have done wrong.

     
    String add0 = newn.getText();
    String non = "na";
     
                if(add0=="na"){
                    out.write(non.getBytes());
                }else{
                     String iName = ("Updated Number: " + add0.substring(0, 3) + "-" + add0.substring(3, 5) + "-" + add0.substring(5) + newLine);
                        out.write(iName.getBytes());
                }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,255
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: out of range help.

    Use the equals() method for comparing Strings, not the == operator.

    The code should test the length of the String it is working on before assuming it is long enough for the substring method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Range Calculator
    By javanewb002 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2013, 05:45 PM
  2. String Index Out of range
    By Shikha Jain in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2012, 10:14 PM
  3. String index is out of range
    By etag in forum Exceptions
    Replies: 9
    Last Post: April 4th, 2012, 11:59 PM
  4. if problem with range of numbers
    By deependeroracle in forum Loops & Control Statements
    Replies: 3
    Last Post: February 17th, 2012, 05:33 AM
  5. String index out of range?
    By oksmartypants in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2012, 02:10 PM