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: Code will not count number of links

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Code will not count number of links

     //link count
     
               int linkCount = 0;
               for (int i = 4; i <tweetlength - 2; i++){
                 if (tweet.substring(i-4,i) == "http" && tweet.charAt(i) == ':'  && tweet.charAt(i + 1) == '/' && tweet.charAt(i+2) == '/'){
                   linkCount++;
                 } 
               }
               System.out.println("Number of Links: " + linkCount);
    The rest of my code works fine but this part does not. It will not return a count. Please help.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,143
    My Mood
    Mellow
    Thanks
    187
    Thanked 660 Times in 647 Posts

    Default Re: Code will not count number of links

    Strings are not compared for equality - having the same characters - using the equality operator, '=='. Use the equals() method instead, as in:

    if ( thisString.equals( thatString ) )

    Also, consider simplifying your if statement. An if statement containing many conditions is difficult to troubleshoot.

Similar Threads

  1. Using a Loop and Nested Ifs to Count Number of Vowels in a String
    By Potat in forum Loops & Control Statements
    Replies: 17
    Last Post: February 21st, 2013, 08:35 PM
  2. Count Number of Each Letter in Given Word?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2011, 07:55 PM
  3. Replies: 3
    Last Post: January 28th, 2011, 08:37 AM
  4. Where I'm I wrong? I need to do a count of the number of each element in an array
    By NavagatingJava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2011, 01:50 AM
  5. Java Dos Logic Test count all non increasing number
    By Jhovarie in forum Object Oriented Programming
    Replies: 3
    Last Post: January 13th, 2011, 02:28 PM