(if you were looking at my other thread earlier and wondering why I haven't posted on it in a while, I will put that off for awhile. I have it saved in a text document and will PM if I have questions. For now I'm just leaving it off)
I made a pretty small program and I'm wondering how I would save it or find a way to run it as a stand-alone application, outside of the console in Eclipse.
I've tried to run it as an applet (embedded in HTML) and I couldn't figure out how to do that. I tried to run it through command prompt (terminal on Mac with the command 'java') but it just tossed me an error message.
I also tried saving it as a Jar file and a Class file but still nothing.
Do I need to change the code at all?
import java.util.Scanner;
public class Calculator
{
static Scanner typeinput = new Scanner(System.in);
public static void main(String[] args) { decider(); }
public static void decider() {
System.out.println("Would you like to calculate the area for a triangle, rectangle, or circle?");
String message = typeinput.nextLine();
if (message.equals("triangle")) {
System.out.println("Enter the base length of the triangle:");
double base = typeinput.nextDouble();
System.out.print("Enter the height length of the triangle:");
double height = typeinput.nextDouble();
double preCalculation = base * height;
double Area = preCalculation / 2.0D;
System.out.println("The Area of your triangle is: " + Area);
}
if (message.equals("rectangle")) {
System.out.println("Enter one of the side lengths:");
double length1 = typeinput.nextDouble();
System.out.println("Enter the other side length");
double length2 = typeinput.nextDouble();
double Area = length1 * length2;
System.out.println("The area of your rectangle is: " + Area);
}
if (message.equals("circle")) {
System.out.println("Enter the radius of your circle:");
double radius = typeinput.nextDouble();
double Area = 3.14159265358979 * radius * radius;
System.out.println("The area of your circle is: " + Area);
}
else{
return;
}
}
}
Console applications are run by invoking the class in the form java <classname>, without the extension. The invoked class must have a static main() function returning void and accepting a string array:
public static void main(String[] args)
Yours has it. therefore you can run the program by compiling it with javac and running the class file. Eclipse automatically compiles your java source when you save your project, so you can find the class files in your project folder.
An Applet can only run as a class derived from Applet (java.awt.Applet) or the swing applet (I think, javax.swing.JApplet, if I had to guess). Far more complex than a console program since you need to create/manage controls, hook notifications, etc.
but it just tossed me an error message.
Any reason you are keeping that error to yourself?
you have to run it without the extension, otherwise it says it can't find it.
My memory failed me
To compile, use the command javac <name>.java To execute the compiled file, use the command java <mainclassname>
These commands only work if you have the JDK installed. Also, the PATH environment variable must be set to C:\Program Files\Java\jdk<version here>\bin;
I made a pretty small program and I'm wondering how I would save it or find a way to run it as a stand-alone application, outside of the console in Eclipse.
I've tried to run it as an applet (embedded in HTML) and I couldn't figure out how to do that. I tried to run it through command prompt (terminal on Mac with the command 'java') but it just tossed me an error message.
I also tried saving it as a Jar file and a Class file but still nothing.
Do I need to change the code at all?
Yours has it. therefore you can run the program by compiling it with javac and running the class file. Eclipse automatically compiles your java source when you save your project, so you can find the class files in your project folder.
An Applet can only run as a class derived from Applet (java.awt.Applet) or the swing applet (I think, javax.swing.JApplet, if I had to guess). Far more complex than a console program since you need to create/manage controls, hook notifications, etc.
Any reason you are keeping that error to yourself?
If you don't want to do that, should be able to run the class file as java Calculator.class (iirc)
you have to run it without the extension, otherwise it says it can't find it.
My memory failed me
To compile, use the command javac <name>.java
To execute the compiled file, use the command java <mainclassname>
These commands only work if you have the JDK installed. Also, the PATH environment variable must be set to C:\Program Files\Java\jdk<version here>\bin;