Click to See Complete Forum and Search --> : applet help


t2090
06-04-2006, 01:58 PM
hi i'm new here but i just needed some help with my applet.

btw its my first.

heres the code:

import static java.lang.System.out;
import java.util.Scanner;
import java.util.Random;

class guess {

public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);

int numGuesses = 0;
int randomNumber = new Random().nextInt(10) + 1;

out.println("Welcome To the guessing Game");
out.println();

out.print("Enter an int from 1 to 10: ");
int inputNumber = myScanner.nextInt();
numGuesses++;

while (inputNumber != randomNumber) {
out.println();
out.println("Try again...............");
out.println("Enter an int from 1 to 10: ");
inputNumber = myScanner.nextInt();
numGuesses++;
}

out.print("You win after ");
out.print(numGuesses + " guesses.");
}
}


i named it guess.clas and im not sure if i should try it .java but it just wont work. the call-up on my web page is:

<applet code="guess.class" width="500" height="500">
</applet>

whats wrong? :confused:

Khalid Ali
06-05-2006, 02:23 PM
what happens when u try to see the applet in the page?

t2090
06-05-2006, 11:01 PM
just the regular applet gray squarebut there is a red x in the corner instead of a cup of java

Khalid Ali
06-06-2006, 01:16 PM
first thing which I will do is make sure that your browser is configured correctly to display the java applets(download and install most current JRE from java site)

t2090
06-06-2006, 05:05 PM
i have.

Khalid Ali
06-06-2006, 08:54 PM
go to this link (http://java.sun.com/applets/jdk/1.4/demo/applets/ArcTest/example1.html) if your browser shows you the applet that means you have browser correctly configured(then we will move on to next step) otherwise first we need to make sure ur broweser is ok.

t2090
06-07-2006, 01:39 PM
yea it works perfectly.

Khalid Ali
06-07-2006, 05:26 PM
next up will be the path of the file. make sure that file is accessible ..a quick test will be to copy the file name with path(while u are on applet page and paste in the address window. If u get a download box or see the file displayed then that test passes

t2090
06-07-2006, 06:30 PM
i just see text on the page.


EDIT: now i see the download box

ccoder
06-08-2006, 09:40 AM
I'm a bit confused as to what you are doing.

The code you posted is not an applet. It is a java application and will compile and run (once you terminate the line out.print("Enter an int from 1 to 10: ") with a semicolon) from a command prompt.

Regardless of what you named the .java file, the output from the compile is named GuessAgain.class. Renaming GuessAgain.class to guess.class will throw an exception when you run it:

C:\Dev\Java>java guess
Exception in thread "main" java.lang.NoClassDefFoundError: guess (wrong Name: GuessAgain)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

And, yes, trying to load the applet will fail just as you described:

just the regular applet gray squarebut there is a red x in the corner instead of a cup of java

t2090
06-09-2006, 08:18 PM
i just changed the name but it still doesnt work

ccoder
06-09-2006, 08:28 PM
i just changed the name but it still doesnt work
You changed which file to what? And how did you try to run the code?

t2090
06-09-2006, 09:26 PM
i changed it from
class GuessAgain {
to
class guess {

t2090
06-09-2006, 09:28 PM
I'm a bit confused as to what you are doing.

The code you posted is not an applet. It is a java application and will compile and run (once you terminate the line out.print("Enter an int from 1 to 10: ") with a semicolon) from a command prompt.

Regardless of what you named the .java file, the output from the compile is named GuessAgain.class. Renaming GuessAgain.class to guess.class will throw an exception when you run it:

C:\Dev\Java>java guess
Exception in thread "main" java.lang.NoClassDefFoundError: guess (wrong Name: GuessAgain)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

And, yes, trying to load the applet will fail just as you described:



i have no idea wth you mean

ccoder
06-10-2006, 09:09 AM
i have no idea wth you mean
You need to be more explicit. I have no idea what it is you don't understand. Is it the difference between an applet and an application? Why the exception was thrown? Something else?

I just compiled and ran your latest version of the code. Here is the output:

C:\Dev\Java>java guess
Welcom To the guessing Game

Enter an int from 1 to 10: 1

Try again...............
Enter an int from 1 to 10:
2
You win after 2 guesses.

So your code runs as an application. To make it an applet, class guess has to extend java.applet.Applet. You will also have to rewrite you I/O routines