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

Thread: If statement not returning anything

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question If statement not returning anything

    import java.net.*;
    import java.io.*;
     
    public class Foo{
    	public static void main (String[] args)
    	{
    		try {
    				InetAddress thisIp = InetAddress.getLocalHost();	
    				String[] result = thisIp.getHostAddress().split("[.]");
    				String IPAddr = (result[0] + "." + result[1] + "." + result[2]);
     
     
     
    					for (int i = 1; i < 256; i ++)
    					{
    					//For loop
    						String IPAddress = (IPAddr + "." + i);
    						System.out.println(IPAddress);
    						if (IPAddress == (result[0] + "." + result[1] + "." + result[2] + "." + result[3]))
    						{
    						System.out.println("IP FOUND");
    						}
     
    					}
    			} 
    				catch (Exception e)
    			{
    				e.printStackTrace();
    			}
    	}
    }


    I'm not sure why this isn't working, the IF statement doesn't return anything to the console.

    Any ideas?

    Thanks a bunch!


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: If statement not returning anything

    1.) It only prints, not returns anything.

    2.) Even if it did, main is void so it can't return anything.

    3.) For a String, use the .equals method.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    Jonathan_C (November 15th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: If statement not returning anything

    try

    if (IPAddress.equals(......))

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

    Jonathan_C (November 15th, 2010)

  6. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile Re: If statement not returning anything

    Thanks
    That worked perfectly!
    Last edited by Jonathan_C; November 15th, 2010 at 04:02 PM.

Similar Threads

  1. Method returning boolean
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 06:45 PM
  2. Returning reversed string? Help please.
    By mindlessn00b in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 18th, 2010, 04:23 PM
  3. Returning Null
    By Woody619 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 22nd, 2010, 12:53 PM
  4. returning a 2D array
    By straw in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 04:30 AM
  5. throwing and returning
    By chronoz13 in forum Exceptions
    Replies: 6
    Last Post: October 25th, 2009, 12:00 AM