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: SOS

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default SOS

    Hey friends,

    I am stuck in this the program below.
    I am supposed to get the result of the item whether it's in array of not. Kindly guide me through this...where I have done the mistake.
    I am new in Java.

    __________________________________________________ _____________________

    class LinearSearching
    {
    public static void main(String[] args)
    {
    int data[] = {11,23,54,34,44,76};
    int n = data.length;
    int item = 50;
    int loc = 0;
    data[n+1] = item;

    do
    {
    loc = loc + 1;
    }
    while(data[loc]!=item);

    if(loc == n+1)
    {
    loc = 0;
    System.out.println("Unsuccessful");
    }
    else
    System.out.println("Successful");
    }
    }

    __________________________________________________ ______________________


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    60
    My Mood
    Tolerant
    Thanks
    1
    Thanked 17 Times in 16 Posts

    Default Re: SOS

    Hello, I am having trouble understanding your goal. But first, basic arrays are of fixed size. You cannot put an additional item in it like you tried to do in line 9 with n+1, you will run into ArrayIndexOutOfBoundsException when you execute the program.

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

    javapenguin (March 26th, 2011)

  4. #3
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: SOS

    For searching an array, you need to loop through the contents of the array. Loop through each position of the array and check to see if the value of that particular position is equal to what you are searching for.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  5. #4
    Member
    Join Date
    Feb 2011
    Posts
    60
    My Mood
    Tolerant
    Thanks
    1
    Thanked 17 Times in 16 Posts

    Default Re: SOS

    So, just to fully comment the program:
    class LinearSearching
    {
    	public static void main(String[] args)
    	{
    		int data[] = {11,23,54,34,44,76};//array of information you are searching through
    		int n = data.length;//length of the array
    		int item = 50;//item you're searching for
    		int loc = 0;//current location within the array
    		data[n+1] = item;//adding the item to the end of the array, not allowed in Java, arrays are of fixed size.  Using an index out of the range of the original size will crash the program.
    		do// not really a good choice, java arrays start at index 0,  this code will increment to 1 first before checking the condition.
    		{	
    			loc = loc + 1;//increment at least once, 
    		}
    		while(data[loc]!=item);
     
    		if(loc == n+1) // the end has been reached, thus the item is not in the original list
    		{
    			loc = 0; //?, not really required.
    			System.out.println("Unsuccessful");// failure notification
    		}
    		else
    		System.out.println("Successful");  //assuming that the item has been found since the position variable is not at the end of the array.
    	}
    }
    Does this help? You'll need to find a way to make the array with the variable that you're looking for it you want to do it this way.

  6. The Following User Says Thank You to JJeng For This Useful Post:

    OwsumEmam (March 29th, 2011)

  7. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SOS

    WOW...thanks dear for helping me out....actaully the prob existed with your this code but commenting each statement helped me figuring me out the prob....!!!

Tags for this Thread