below is my code about a method regarding arrays.

The purpose of my program is to let the user input the length of the array.
and then insert numbers into the array, sort the numbers,
and let the user search the contents of the array.

my problem is i dont know how to apply them into a main method.
any tips on how to use them? can give me an example?
thanks in advance. I really need help.

public class Exer2
{
int listLength;
int[]list=new int[listLength];

public void insert1(int loc,int item)
{
list[loc]=item;
}

public void insert2(int item)
{
	for(int x=0;x<=list.length;x++)
	{
		{
		if(list[x]==0)
		list[x]=item;
		}
	}
}

public static void selectionSort(int[] list,int listLength)
{
    int index;
    int smallestIndex;
    int minIndex;
    int temp;
    for (index = 0; index < listLength - 1; index++)
    {
        smallestIndex = index;
        for (minIndex = index + 1;
             minIndex < listLength; minIndex++)
            if (list[minIndex] < list[smallestIndex])
                smallestIndex = minIndex;

        temp = list[smallestIndex];
        list[smallestIndex] = list[index];
        list[index] = temp;
    }
}

public static int seqOrderedSearch(int[] list,int listLength, int searchItem)
{
     int loc;
     boolean found = false;
     for (loc = 0; loc < listLength; loc++){
        if (list[loc] >= searchItem)
         {
           found = true;
           break;
         }
 	}
     if (found){
        if (list[loc] == searchItem)
		return loc;
        else
            return -1;
   }
     else
         return -1;
}

}

Recommended Answers

All 2 Replies

I see that you don't have a main method here. Have you tried to write one? If so, what problems have you run into?

There are some common problems that you might see in calling methods out of main(), but based on what I'm seeing here you won't be seeing any of them yet, because you don't have a main().

It's a lot easier to help you out if you have a particular problem or question, for example, if you've tried to do something and it hasn't worked the way you wanted it to.

ok here's my broken main method.
i really dont know how to apply the sub methods into the main method.
but this is my expected format.
I am not sure of my code because when i run it. everything has an error.

I really need help.

public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
int loc,item1,item2;

System.out.println("Enter array length: ");

System.out.println("Insert array item with location: ");
loc=input.nextInt();
item1=input.nextInt();
insert1(loc,item1);

System.out.println("Insert array item only: ");
item2=input.nextInt();
insert2(item2); 

//I am having dificulties on this part
System.out.println("The Sorted Array: ");
selectionSort(int[] list,int listLength);

System.out.println("Enter an item to be search from the array: ");
seqOrderedSearch(int[] list,int listLength, int searchItem);
}

if i would learn how to use the insert1 and insert2 methods.
Maybe ill be able to finish everything. I am confused.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.