Java help

Discussion in 'Mac Programming' started by larswik, Sep 13, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    So I finished my 3rd Java class and I decided to work on the homework at home and not in the lab. I typed out the assignment and I am getting 1 error. The public class CompareInts, the CompareInts is causing a problem

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;

    Code:
    public class [COLOR="Red"]CompareInts[/COLOR] extends Applet implements ActionListener
    		{
    	Label prompt1;
    	TextField input1;
    	Label prompt2;
    	TextField input2;
    	int number1, number2;
    	
    	public void init()
    	{
    	prompt1 = new Label ("Enter an Integer: ");
    		add (prompt1);
    		
    		input1 = new TextField(10);
    		add (input1);
    		
    		prompt2 = new Label ("Enter an Integer and press return");
    		add (prompt2);
    		
    		input2 = new TextField (10);
    		input2.addActionListener( this);
    		add (input2);
    	}
    	
    	public void paint(Graphics g)
    	{
    		g.drawString("The comparison results are: ", 70, 75);
    		
    		if (number1 == number2)
    			g.drawString(number1 + "==" + number2, 100, 90);
    		
    		if (number1 != number2)
    			g.drawString(number1 + "!=" + number2, 100, 105);
    		
    		if (number1 < number2)
    			g.drawString(number1 + "<" + number2, 100, 120);
    		
    		if (number1 > number2)
    			g.drawString(number1 + ">" + number2, 100, 135);
    		
    		if (number1 <= number2)
    			g.drawString(number1 + "<=" + number2, 100, 150);
    		
    		if (number1 >= number2)
    			g.drawString(number1 + ">=" + number2, 100, 165);
    	}
    	
    	public void actionPerfprmed( ActionEvent e)
    	{
    		number1 = Integer.parseInt( input1.getText());
    		number2 = Integer.parseInt( input2.getText());
    		repaint();
    	}
    
    }
    
    
    Now that is just the name of my class so it should not matter what it is, right?
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    I solved part of the problem so far go over the code line by line. I had a typo in the 'public void actionPerformed'. Now it won't let me build it even though I discovered that typo and I get this error. It's my first time using Eclipse, i miss Xcode

    Code:
    load: class Comparison.class not found.
    java.lang.ClassNotFoundException: Comparison.class
    	at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:211)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    	at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:144)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    	at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:662)
    	at sun.applet.AppletPanel.createApplet(AppletPanel.java:807)
    	at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
    	at sun.applet.AppletPanel.run(AppletPanel.java:368)
    	at java.lang.Thread.run(Thread.java:680)
    
     
  4. foidulus macrumors 6502a

    Joined:
    Jan 15, 2007
    #4
    Well you called your class CompareInts and not Comparison, so no wonder the JVM cannot find what you are looking for.
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    It looks like the run configuration in Eclipse is set up to run an applet called Comparison, and not CompareInts.

    Try creating a new run configuration by right-clicking on CompareInts.java in the package explorer, going into Run As, and choosing Java Applet.
     
  6. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #6
    Thanks Jim that worked! I wonder why it is not working. From what I read the public class CompareInts is like creating a new class in object - C and calling it CompareInts. I don't see why it would not run.

    But it is good now, thanks!

    -Lars
     
  7. foidulus macrumors 6502a

    Joined:
    Jan 15, 2007
    #7
    Well, Java is a bit different than C in that regard. In C you essentially compile all the files into one big file, and as long as one of those files(and only one of those files) has a main method then you can compile into an executable and call that executable whatever you want.

    Java does not work like that, you have to specify which class's main you are intending to use, and that name has to match exactly, which is why your code wasn't executing.
     
  8. naples98, Sep 14, 2011
    Last edited by a moderator: Sep 14, 2011

    naples98 macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #8
    Just to be very clear, a *.java file can contain 1 public class and other package level classes (or all package level classes) but the name of the *.java file must match exactly the name of that public class (or one of the package level classes if all the classes are package level). For example

    FooBar.java can contain

    Code:
    public class FooBar {}
    class Foo {}
    class Bar {}
    or

    Code:
    class FooBar {}
    class Foo {}
    class Bar {}
    but it can not contain

    Code:
    class FooBar {}
    class Foo {}
    public class Bar {}
     
  9. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #9
    I see that. The TA in the course said that the Mac version of Java using Eclipse might be different then the Windows version very slightly when it comes to writing code. So I thought there might be a difference with me copying the assignment on paper to Eclipse program.

    Thanks for the explanation.

    -Lars
     
  10. foidulus macrumors 6502a

    Joined:
    Jan 15, 2007
    #10
    On the absolute extreme edges there are some differences, for instance the case of your class file names. I once saw a jar file which, although written in pure Java, was designed only to run on Windows. The programmer ensured that by doing some really spooky things with the .class file names inside the jar. However they are 99.9% the same, esp. at the level you are working at.
     

Share This Page