0

This is the source code:

package feetToInches2;

public class Feet2Inches {
    private double feetToInches_(double feet) {
        return 12* feet;    
    } 

    private int max(int val1 int val2) {
        if (val1 > val2) {
        } else {
            return (val2);
        }
        return 0;
    }
}

When I try to run it, it says "Selection does not contain main type". I have asked my professor and he says sometimes eclipse does that and all I need to do is restart my computer. Is that true?

Edd
  • 3,724
  • 3
  • 26
  • 33
qax
  • 109
  • 1
  • 14
  • What is the entry point of a java program? – TheLostMind Oct 07 '14 at 08:33
  • You're missing a "public static void main(String ... args)" method. – Jan Vladimir Mostert Oct 07 '14 at 08:34
  • As the message say you miss the method names main `public static void main(String[] args) {` – Jens Oct 07 '14 at 08:34
  • Where's the ```Selection``` you speak of? – NeplatnyUdaj Oct 07 '14 at 08:34
  • You had not define the main() inside of the class. When you run a java application, JVM is seeking main() to run the application.Define the main() and run the application. Also you had not define the body of the if. – Dhanushka Gayashan Oct 07 '14 at 08:35
  • If you think about it a little, what do you expect to happen? Run `max()` or `feetToInches()`? With what parameters? Java needs an entry point, where your program can start, and for that you need a static `main()` method. – Balázs Édes Oct 07 '14 at 08:41
  • Either your professor is trying to make a joke of you, or you did not post all of your code. Btw. if you are using Eclipse, open your class, and pres `Ctrl`+`Shift`+`F`. It will reformat your code to a form, that java programmers like it ;) – T.G Oct 07 '14 at 08:48
  • There is no `public` method in this class. – Mohan Oct 07 '14 at 08:53
  • if I add public static void main(String[] args) { after public class Feet2Inches { it gives me an error .. – qax Oct 07 '14 at 09:23

3 Answers3

1

What you need is a main method. It is an entry point

public static void main(String[] args) {
        //Your code here
}

in your case it can look like:

package feetToInches2;


public class Feet2Inches {
    public double feetToInches_ (double feet) {
        return 12* feet;    
    } 

    public int max(int val1, int val2) {
        if (val1 > val2) {
            return (val1);
        } 
        else {
            return (val2);
        }
    }
    public static void main(String[] args) {
        Feet2Inches converter=new Feet2Inches();
        int max = converter.max(100,50);
        double inches = converter.feetToInches_(10.5);
        System.out.println("Max value = " + max);
        System.out.println("Inches = " + inches);
    }

}

i've changed your method modifiers so it would be more usefull. Class with no public method is unneeded class ;) I also fixed some of errors in your code.

EDIT:

If you want to allow people to youse your code to their own calculation you have two choises. You can take parameters from args array which holds parameters that was program runned with. or you can prompt for a data at the runtime.

1 Using parameters

public static void main(String[] args) {
    Feet2Inches converter = new Feet2Inches();
    if (args.length != 3) {
        System.err.println("Missing arguments! give me three numbers");
        System.exit(1);//error exit
    }
    int val1 = Integer.valueOf(args[0]);
    int val2 = Integer.valueOf(args[1]);
    double val3 = Double.valueOf(args[1]);
    int max = converter.max(val1, val2);
    double inches = converter.feetToInches_(val3);
    System.out.println("Max value from (" + val1 + "," + val2 + ")= " + max);
    System.out.println(val3 + "Feet = " + inches + " inches");
}

now you have to call your program with arguments Here you can see how to do it from cmd and Here is how to do it in Eclipse

2 ask user for parameters at runtime

public static void main(String[] args) {
    Feet2Inches converter = new Feet2Inches();
    Scanner input = new Scanner(System.in);
    System.out.print("Argument 1:");
    int val1 = input.nextInt();
    System.out.print("Argument 2:");
    int val2 = input.nextInt();
    System.out.print("Argument 3:");
    double val3 = input.nextDouble();
    int max = converter.max(val1, val2);
    double inches = converter.feetToInches_(val3);
    System.out.println("Max value from (" + val1 + "," + val2 + ")= " + max);
    System.out.println(val3 + "Feet = " + inches + " inches");
}
Community
  • 1
  • 1
T.G
  • 1,913
  • 1
  • 16
  • 29
  • So I copied and tried to run the code you have written but it says for max and inches "The value of the local variable max is not used" – qax Oct 07 '14 at 09:05
  • yes, it is correct. i assigned an result of your functions to a local variables, but as you see, i dont do much with them. it is up to you what you want to do next. you can for example wtite those results on screen. i'll update my answer so you can see how it works. "The value of the local variable max is not used" is not an error, its just warning that says hat something is created for no reason. If you have to call a method, that returns some value, and you dont need it (you just need what is happening inside method), then just ignore return value, and dont assign it to any variable. – T.G Oct 07 '14 at 09:09
  • thanks a lot. How for example I would want to let someone enter his own numbers and it would convert it for him. Just like a field where he can put the value of inches and it would be converted to feet. When I ran your code this is what have been written in the console: Max value = 100 Inches = 126.0 but there is no way I can edit those fields. I am sorry im just a begginer in Java. Sorry for any stupid quesitons. – qax Oct 07 '14 at 09:15
0

This is wrong

private int max(int val1 int val2)

it should be

private int max(int val1, int val2)

on top of that you a public static void main(String[] args) { from which you can call your methods.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

In order to run a Java Application you always need to have public static void main(String[] args) method defined. This method should contain whatever the code you need to execute.

maheeka
  • 1,983
  • 1
  • 17
  • 25
  • where exactly should I put it in my code. I have tried after public class Feet2Inches and it doesnt work. It always gives an error. – qax Oct 07 '14 at 09:34
  • It should be inside Feet2Inches class or even could be in a completely different class. – maheeka Oct 07 '14 at 09:42