I just started taking the Stanford CS106a course on iTunes, but I'm running in to problems with Eclipse. Here's my code:
/*
* File: Add2Integers.java
* -----------------------
* A simple ConsoleProgram to add two integers
* and display their total.
*/
import acm.program.*;
public class Add2Integers extends ConsoleProgram {
public void run() {
/* So all y'all in the back can see! */
setFont("DejaVuSerif-BOLD-24");
println("This program adds two numbers.");
int n1 = readInt("Enter n1: ");
int n2 = readInt("Enter n2: ");
int total = n1 + n2;
println("The total is " + total + ".");
}
}
When I try to run it, I get the message that the section does not contain an applet. I think it has to do with import acm.program.
I downloaded the acm toolkit and tried adding the program.java file to my root folder, building the path, doing the same for the entire acm folder, nothing works.
I just need help getting this simple program up and running so that I can start learning.
I'm running OSX 10.8.
To run a Java application, you need a main method:
public static void main(String[] args) {
Add2Integers add2Integers = new Add2Integers();
add2Integers.run();
}
You need to start your ConsoleProgram from a main method:
public static void main(String[] args) {
new Add2Integers().start(args);
}
See: Introduction to the JTF Packages
When I try to run, I get the message that the section does not contain an applet.
That's because it isn't an applet. It's an ordinary Java application.
The examples which do graphics, are applets. But this is text-only - it extends ConsoleProgram - so it is not an applet.
I know its too late, but you dont need the main(String[] args), all you need is press the right click in your project, go to propieties, then java build path, libraries, add external Jars, and search the acm.jar file in your pc.
Related
I am using a Mac osx 10.10.5 Yosemite, and I am trying to run my short Java program using Terminal. I first entered: "emacs Salary.java". Then: "javac /Users/username/Documents/Salary.java". And finally: "java Salary". It created a .class file for my java program, yet it says "
Error: Could not find or load main class Salary"
after I try running it. Am I doing something wrong? I checked the directories and it's in the correct place. The code is provided below, thank you in advance:
public class Salary {
public static void main (String [] args) {
int hourlyWage = 20;
System.out.print("Annual salary is: ");
System.out.println(hourlyWage * 40 * 50);
System.out.print("Monthly salary is: ");
System.out.print((hourlyWage * 40 * 50) / 12);
return;
}
}
I am a begineer in java programming.
I was making a program to display odd numbers from 1 to 100 using loops. But that program when executed displays the odd number from 7 to 100 please help me to rectify that program.
The program.....
public class oddnumbers1to100
{
public static void main(String args[])
{
for(int a=1;a<=100;a+=2) {
System.out.println(a);
}
}
}
As expected the issue is the console you're using, in fact the console integrated in BlueJ, your IDE.
You can do two things to get the full output:
List numbers horizontally:
for (int a = 1; a <= 100; a+=2)
System.out.print(a + " ");
Or change the buffer setting of your console:
Click on "View" and then "Show Terminal" in the main menu (shortcut Ctrl + T)
Then click on "Options" and select "Unlimited buffering"
Uninstalling the compiler or the IDE (as suggested in the comments) won't help you here, since it is not a problem of your program or your compiler.
I want to do something like this:
Python Code:
nums = [1,2,3]
Java Code:
nums_Java[] = nums //from python
System.out.println(nums_Java[0])
Output:
1
I have been looking over jython but I just can't seem to find the answer. It seems like it should be very simple but I'm lost. Thanks!
If I understand the question correctly, you'd like to run some embedded python code from a java program, and get the value of a python variable.
Based on http://www.jython.org/archive/21/docs/embedding.html , I wrote a small program that might help:
import org.python.util.PythonInterpreter;
import org.python.core.*;
public class SimpleEmbedded {
public static void main(String[] args) throws PyException {
PythonInterpreter interp = new PythonInterpreter();
interp.exec("nums = [1,2,3]");
PyObject nums = interp.get("nums");
System.out.println("nums: " + nums);
System.out.println("nums is of type: " + nums.getClass());
}
}
Unfortunately, I don't have jython installed at the moment, so the above code is untested. Also I'm not sure what type you will get back from the interpreter, and how to convert it to a java array or access its items. But the program should get you started and give you some more information.
While studying from the pdf presentation of the lesson, i usually try and compile a lot of the examples given, often i rewrite all of it by myself, so it's also a memorization exercise.
However in this case, i don't seem able to compile something that i copy-pasted; then i proceeded to rewrite it all by myself and it worked. I don't know if it's a bug or something that i'm missing.
i'll leave the code here and the terminal error. btw i'm on osx lion and i'm using textwrangler as an editor and the terminal for compiling.
import java.lang.Math;
public class Radice
{
public static void main(String[] args)
{
double r = Math.sqrt(2);
double x = r * r;
if (x==2)
System.out.println("OK");
else
System.out.println("Non ci credevi?");
}
}

import java.lang.Math;
public class Radice2
{
public static void main(String[] args)
{
double r = Math.sqrt(2);
double x = r * r;
if (x==2)
System.out.println("OK");
else
System.out.println("Non ci credevi?");
}
}
only Radice2 work. here's the log
iMac-di-alessio:~ alessiobogesso$ cd Desktop/
iMac-di-alessio:Desktop alessiobogesso$ java Radice2
Non ci credevi?
iMac-di-alessio:Desktop alessiobogesso$ javac Radice.java
Radice.java:16: error: illegal character: \65532
^
Radice.java:16: error: reached end of file while parsing
^
2 errors
iMac-di-alessio:Desktop alessiobogesso$
thanks for your help
The compiler told you that there is a illegal character in your source code. The Unicode 65532 is not printable so it is not displayed and you are unable to see it but it is there.
I have an odd problem. So I am writing a program that uses Python for a simple user scripting interface. But to keep my question simple...I am trying to use PythonInterpreter.set to set a variable in the Python interpreter to a String value. But when I set it I get this exception:
LookupError: no codec search functions registered: can't find encoding
The following is my code:
package demo;
import org.python.util.PythonInterpreter;
public class Application {
public static void main(String[] args) {
PythonInterpreter pi = new PythonInterpreter();
String greeting = "Jesus Christ";
Integer times = 6;
pi.exec("actor = 'Lucy'");
pi.set("greeting", greeting);
pi.set("times", times);
pi.exec("print '%s %s' % (greeting, actor)");
pi.exec("print \"%s %s \\n\" % (greeting, actor) * times");
System.out.println("RESULT: " + pi.eval("actor == 'Lucy'"));
System.out.println("ACTOR: " + pi.get("actor"));
}
}
If you need to see the pom file for my project, I can include it, but really I just have the Jython 2.5.0 library installed. I am wondering if I needed to install something else on my system other than having maven install this library for me. I do have Python installed on this computer, and PYTHON_HOME setup in the environment variables. What am I doing wrong?
EDIT: The line:
pi.set("times", times);
...works just fine.
But...
pi.set("greeting", greeting);
does not. I imagine it has something to with times being a primitive data type and greeting being a String.