-2

I'm just starting out in java and I'm trying to make a greedy algorithm. The first step is to read the file.txt with the jewel values and bag weight limit and such. unfortunately I am having trouble getting the program to run. I am using eclipse and when I click run I get the following error message "the selection cannot be launched, and there are no recent launches". When I select the java greedy algorithm folder in the file tree and select run i get the following message "selection does not contain a main type". the work file and file.txt are saved in the same folder on my desktop but I wonder if the program isn't finding it. here's my code:

/** open and read a file, and return the lines in the file as a list of strings */
private List<String> readFile(file.txt)
{
    List<String> records = new ArrayList<String>();
    try
    {
        BufferedReader reader = new BufferedReader(new FileReader(file.txt));
        String line;
        while (( line = reader.readLine()) != null)
        {
            records.add(line);
        }
        reader.close():
        return records;
    }
    catch (Exception e)
    {
        System.err.format("Exception occurred trying to read '%s'.", file.txt);
        e.printStackTrace();
        return null;
    }
}

Thanks for the help.

  • 3
    Possible duplicate of [Error: Selection does not contain a main type](https://stackoverflow.com/questions/16225177/error-selection-does-not-contain-a-main-type) – Rann Lifshitz Apr 06 '18 at 19:30

3 Answers3

1

A java class should have a main method then only you can run that.

So, your class will be like this.

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class Test {

public static void main(String... args) {
    //call readFile
    List<String> someList = readFile(<pass filename here>);
    //do something here with someList
}


/** open and read a file, and return the lines in the file as a list of strings */
private static List<String> readFile(String filename)
{
    List<String> records = new ArrayList<>();
    try
    {
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        String line;
        while (( line = reader.readLine()) != null)
        {
            records.add(line);
        }
        reader.close();
        return records;
    }
    catch (Exception e)
    {
        System.err.format("Exception occurred trying to read '%s'.",filename );
        e.printStackTrace();
        return null;
    }
}
}

Note that, I marked readFile method as static which is because I am invoking it from main method without creating an instance of Test class. If you create an instance of Test class, and call readFile method on it, then you can remove static modifier.

humblefoolish
  • 409
  • 3
  • 15
  • I made the changes you suggested but I still get the same error messages. it also says args is not correctly spelled. Is 'args' a placeholder for something? –  Apr 06 '18 at 20:12
  • A.Must15 incorrect spelling must be a warning. Just wondering, how are you running the code? – humblefoolish Apr 06 '18 at 20:21
  • in eclipse. when I select just the individual greedy file and click run it says the selection can't be launched and there are no recent launches. when i select the full file tree and click run as java application it says selection does not contain a main type. –  Apr 06 '18 at 20:47
  • Are there any compilation errors in the code. Or, if you used the above code which clearly has a main method – humblefoolish Apr 06 '18 at 20:55
  • I don't think there are any errors but to be safe I removed all my code and used yours. got the same result. –  Apr 06 '18 at 23:08
  • I am not really sure, what is happening there as it runs properly for me. Just guessing, are imports missing? or if the class name and filename are different? I have added the imports in above code as well. So, all you should change is this `` – humblefoolish Apr 06 '18 at 23:21
  • I tried adding the import stuff but I still get the same result. side note: is there like a website or something with a list of everything that can be imported into java? –  Apr 07 '18 at 03:21
1

You have to add a method named void main(String[] args). This is the method that gets called when you start your program. In this main method you can call your readFile method, like so:

public static void main(String[] args) {
  readFile();
}
byMopo
  • 11
  • 4
0

You are missing the

public static void main(String args[])
{
    ...
}

There you can call your function.

Mr. Wizard
  • 1,093
  • 1
  • 12
  • 19