1. Member
Join Date
Feb 2015
Posts
4
Rep Power
0

Hello!

I've compiled all three programs without any problem. However if I test it, that message will show up. I'm using jGrasp as the program. Is there something that I am missing?

Code 1 (Selection sort):

public class SelectionSort{
/** The method for sorting the numbers */
public static void selectionSort(double[] list){
for (int i=0; i<list.length - 1; i++){
//Find the minimum in the list[i..list.length-1]
double currentMin = list[i];
int currentMinIndex=i;

for (int j=i+1; j>list.length; j++){
if (currentMin > list[j]){
currentMin = list[j];
currentMinIndex = j;
}
}

//Swap list[i] with list[currentMinIndex] if necessary;
if (currentMinIndex !=i){
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
}

Code 2 (Linear search):

public class LinearSearch{
/** The method for finding a key in the list */
public static int linearSearch(int[] list, int key){
for (int i=0; i<list.length; i++){
if (key == list[i])
return i;
}

return -1;
}
}

Code 3 (Binary search):

public class BinarySearch{
/** Use binary search to find the key in the list */
public static int binarySearch(int[] list, int key){
int low=0;
int high=list.length-1;

while (high >= low){
int mid=(low + high)/2;
if (key < list[mid])
high=mid -1;
else if (key == list[mid])
return mid;
else
low = mid + 1;
}

return - low - 1; // Now high < low, key not found
}
}

I've been trying to figure this out for the past week. Thank you. :)
Last edited by theefancyone; 02-26-2015 at 09:03 PM.

2. Member
Join Date
Feb 2015
Posts
4
Rep Power
0

You're missing a main method: you need a main method, applet, or MIDIlet.
Here is a simple example.

public static void main(String[] args) {
//code here is run when the program starts up.
}

The main method is called whenever a java program is started. The only exception is in applets, where they're not calling "main" but "init".
Consider combining those classes together into a single class, and having those search/sort methods all contained in one single file, possibly named "DataOperations" or something.
I find it easier to work with one file containing a single class with multiple methods than multiple classes in multiple files for a single method.

3. Member
Join Date
Feb 2015
Posts
4
Rep Power
0

Originally Posted by Psychrom
You're missing a main method: you need a main method, applet, or MIDIlet.
Here is a simple example.

public static void main(String[] args) {
//code here is run when the program starts up.
}

The main method is called whenever a java program is started. The only exception is in applets, where they're not calling "main" but "init".
Consider combining those classes together into a single class, and having those search/sort methods all contained in one single file, possibly named "DataOperations" or something.
I find it easier to work with one file containing a single class with multiple methods than multiple classes in multiple files for a single method.
Alright, so far I was able to compile and test both Linear and Binary searches with no problem, using your method of public static void main(String[] args);

Right now I am doing the same with Selection Sort (1st code posted on my question) and it's not working as such. I compile it and it gives me errors different from the message posted. Thank you.

4. ## Re: "No main methods, applets. or MIDIlets found in file" in these codes. Please help

5. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

Originally Posted by theefancyone
Right now I am doing the same with Selection Sort (1st code posted on my question) and it's not working as such. I compile it and it gives me errors different from the message posted. Thank you.
And you don't think actually posting the errors might help us?

Also, take Darryl's hint and please use code tags when posting code.

6. Member
Join Date
Feb 2015
Posts
4
Rep Power
0

I found some things you might want to take a look at. With no errors given, this is all that I can see that is wrong with your code. There may be more.
Provide your errors, or I will be unable to help any further.

Java Code:
```public class SelectionSort{
/* The method for sorting the numbers */

public static void selectionSort(double[] list){
double currentMin = 0;                                                               // Initialize variables outside of loops
int currentMinIndex = 0;
for (int i=0; i<list.length - 1; i++){                                             //Suggest using enhanced loop, if possible:   for(double i : list)
//Find the minimum in the list[i.list.length-1]
currentMin = list[i];                                                                  //Initialize variables outside of loops. see above for correct placement.
currentMinIndex=i;

for (int j=i+1; j>list.length; j++){
if (currentMin > list[j]){
currentMin = list[j];
currentMinIndex = j;
}
}

//Swap list[i] with list[currentMinIndex] if necessary                       // You dont need ; for comments
if (currentMinIndex !=i){
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
}```
Last edited by Psychrom; 02-27-2015 at 05:59 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•