Welcome Guest [Log In] [Register]
We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Java Code; confused
Topic Started: Aug 30 2011, 07:31 PM (1,702 Views)
gamesir
Member Avatar
Member
[ *  * ]
Okay, so I'm taking this introductory class for computer programming, and for some reason I can't run this file or whatever. Pretty sure I downloaded both JDK and JRE, so I don't know what to do.

Code:
 
/* Lab 2: Basic Computation
Programmer: W. Tong
Date: August 30, 2011
Filename: Computation.java
Purpose: This program displays a simple computation.
*/
import java.io.*;
import java.util.Scanner;

public class Computation {
public static void main(String[] args) {
//create constructop
Scanner input = new Scanner(System.in);

//create variables
int x1;
int x2;
double a, b, c;

x1=10;
x2=5*x1;
System.out.println("Hello, see how my computation.");
System.out.println();
System.out.println("Enter x1 = " + x1);
System.out.println("Enter x2 = " + x2);
System.out.println("x1 + x2 = " + (x1+x2));
System.out.println("x2 - x1 = " + (x2-x1));
System.out.println("x1 * x2 = " + (x1*x2));
System.out.println("x1 / x2 = " + (x1/x2));
System.out.println("x1 mod x2 = " + (x1 % x2));
System.out.println();

System.out.println("Now, we do computation with real numbers.");
System.out.println("And we get the values from keyboard.");
System.out.print("Enter the value for a: ");
a = input.nextDouble();
System.out.print("Enter the value for b: ");
b = input.nextDouble();
System.out.println("Enter a = " + a);
System.out.println("Enter b = " + b);
System.out.println("Compute a + b = " + (a+b));
System.out.println("Compute a - b = " + (a-b));
System.out.println("Compute a x b = " + (a*b));
System.out.println("Compute a / b = " + (a/b));
System.out.println();
}
}
Offline Profile Quote Post Goto Top
 
lightsilver
Member Avatar
this || that
[ *  *  *  *  *  * ]
If you downloaded and installed the JDK, then all you should have to do is point your IDE to the JDK. You know you have it, but your computer doesn't. It's annoying when that happens.

To confirm the problem, open up command prompt/ terminal, navigate to the directory with the java file, and type "javac Computation.java".

I just tested it myself, and it compiles no-problem.

Then to run the program, you just need to enter "java Computation", and it will run. What IDE are you using? I'd recommend either JCreator, BlueJ, or eclipse. Although straight up command line wouldn't hurt (although I'd use Notepad++ (Windows) or TextWrangler or XCode (Mac), or whatever for Linux.

I'm sure, however that the issue is your IDE not knowing about the JDK.
Offline Profile Quote Post Goto Top
 
Karkuta
Member Avatar
Let's get dangerous!
[ *  * ]
Also make sure that the JDK directory is properly set in the system PATH variable.
http://www.java.com/en/download/help/path.xml
Offline Profile Quote Post Goto Top
 
bapezilla
Member Avatar
Member
[ * ]
i've found that working in a simple text editor like jEdit for pc or textwrangler for mac and then compiling and running the programs from the terminal works best when you're only writing simple code like this.
Edited by bapezilla, May 7 2012, 01:34 AM.
Offline Profile Quote Post Goto Top
 
Charlo
Member Avatar
1337er than j00
[ *  *  *  *  *  *  * ]
bapezilla
May 7 2012, 01:34 AM
i've found that working in a simple text editor like jEdit for pc or textwrangler for mac and then compiling and running the programs from the terminal works best when you're only writing simple code like this.
YES! jEdit is my text editor of choice. It works on Macs too. :geek:
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Programming and Scripting Chat · Next Topic »
Add Reply