Hey guys, I've created a java applet which calculates PPI. my problem occurs when I add it to a webpage on dreamweaver. I get the following error message:

load: class calculator.java not found.
java.lang.ClassNotFoundException: calculator.java
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: H:\nnoooo\PPI website\calculator\java.class (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 7 more
Exception: java.lang.ClassNotFoundException: calculator.java

This is the HTML code:

<applet code="calculator.java" width="250" height="250">
      </applet>

Heres my java applet code:

import java.util.Scanner;
import java.applet.*;

public class calculator extends Applet{
	
Scanner nuggz = new Scanner(System.in);
Double payment,length,answer;
int months = 12;

{ 
	System.out.println("Enter your montly payment: ");
	payment=nuggz.nextDouble();
	System.out.println("Enter the length of the loan: ");
	length=nuggz.nextDouble();
	answer= (payment/100*20)*months*length;
	System.out.println("Your estimated claim is: " +answer);
	
}
}

Any help would be greatly appreciated.

Cheers.

Do you know what the javac compiler does? It compiles .java files creating .class files.
The .class files are what you execute. The .java files are source files.

Change your code= to have the filename of the .class file:

code=calculator

Also the Applet will not be able to read from a console. You'll have to write some GUI to get input from the user.

Ok, that makes sense i guess! thanks for the reply!

NormR1 is making a sense.
May I ask you why you use Scanner in Applet?
As far as I understand the Scanner is used for DOS window input. If it is a web application, you may have to create a Textfield instance to receive data a client inputs.

Edited 6 Years Ago by tong1: n/a

Hey, I've played around with a GUI, and have managed to make a GUI that produces what i want within Dialog boxes. What I want to do is instead of dialog boxes have the applet appear like a form, so the user will enter their monthly repayment in the first box, the length of their loan in the second box, and then hit calculate, which would produce the result of the calculation.

I'm rather new at Java however and am struggling, can anyone point me in the right direction, starting to pull my hair out haha.

Cheers.

import java.util.Scanner;
import java.applet.*;
import javax.swing.JOptionPane;


public class Estimator extends Applet {

	
	
	String fn = JOptionPane.showInputDialog("Enter your monthly repayment");
	String sn = JOptionPane.showInputDialog("Enter the length of the loan");
	
	int months = 12;
	int Payment = Integer.parseInt(fn);
	int Length = Integer.parseInt(sn);
	int Sum = (Payment/100*20)*months*Length;
	
	JOptionPane.showMessageDialog(null, "The answer is " +Sum, "Your Estimated claim", JOptionPane.PLAIN_MESSAGE);
}

I have modified a program from a text book by H.M.Deitel, P.J. Deitel (Java How to Program). It helps you to have a solution.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Factorial extends JApplet implements ActionListener {
   JLabel numberLabel, resultLabel;
   JTextField numberField, resultField;

   // set up applet GUI
   public void init() {
      // obtain content pane and set its layout to FlowLayout
      Container container = getContentPane();
      container.setLayout( new FlowLayout() );

      // create numberLabel and attach it to content pane
      numberLabel = new JLabel( "Enter an positive integer(less than 21) and press Enter" );
      container.add( numberLabel );

      // create numberField and attach it to content pane
      numberField = new JTextField( 10 );
      container.add( numberField );

      // register this applet as numberField ActionListener
      numberField.addActionListener( this );

      // create resultLabel and attach it to content pane
      resultLabel = new JLabel( "Fibonacci value is" );
      container.add( resultLabel );

      // create numberField, make it uneditable
      // and attach it to content pane
      resultField = new JTextField( 15 );
      resultField.setEditable( false );
      container.add( resultField );

   } // end method init

   // obtain user input and call method fibonacci
   public void actionPerformed( ActionEvent event )
   {  
      long number, facValue;

      // obtain user input and convert to long
      number = Long.parseLong( numberField.getText() );

      showStatus( "Calculating ..." ); 

      // calculate fibonacci value for number user input
      facValue = fac( number );

      // indicate processing complete and display result
      showStatus( "Done." );   
      resultField.setText( Long.toString( facValue ) );

   } // end method actionPerformed
  
   // recursive declaration of method fibonacci
   public long fac( long n )
   {
      // base case
      if ( n == 0 || n == 1 )  
         return n;

      // recursive step
      else
         return n*fac(n-1);

   } // end method fac

} // end class Factorial

The width for the applet should be large enough to have label and TextField box in one row:

<html>
<applet code = "Factorial.class" width = 500 height = 100>
</applet>
</html>

Edited 6 Years Ago by tong1: n/a

This article has been dead for over six months. Start a new discussion instead.