File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Can't understand complier error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can Watch "Can New topic

Can't understand complier error

E Pease

Joined: Mar 23, 2004
Posts: 15
I have the following program:

import javax.swing.*;
import javax.swing.JOptionPane;
import java.lang.*;

class Calc
public static void main ( String [] args )
String firstNum, secondNum;
int num1, num2, sum, product, difference, quotent;

// obtain the numbers from the user
firstNum = JOptionPane.showInputDialog( "Enter the first number: ");
secondNum = JOptionPane.showInputDialog( "Enter the second number: ");

// convert form String to int
num1 = Integer.parseInt( firstNum );
num2 = Integer.parseInt( secondNum );

// calculate the sum, product, difference, and quotent
sum = num1 + num2;
product = num1 * num2;
difference = num1 - num2;
quotent = num1 / num2;

// print out the sum, product, difference, quotent
JOptionPane.showMessageDialog( null, "The sum is " + sum, "Result", JOptionPane.Plain_Messge );

//System.out.println( "The sum is " + sum );
//System.out.println( "The product is " + product );
//System.out.println( "The difference is " + difference );
//System.out.println( "The quotent is " + quotent );

} // end main
} // end class Calc

And I get the following error message when I try to compile it:

.\ <identifier> expected
publi static void main (String agrs[])

There are four other errors but this is the first one. I have tried renaming the class and file but I still get this message when I try to compile. I do not understand why it says there is no 'c' on the word public. I also do not know why it says 'String agrs[]' when my program says (String [] args). I do have a tendency to miss the small things but I am really stumped this time.

Thanks in advance.

SCJP 1.5 Beta (in progress, nature willing)
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

When I take away the c in public and compile, I get the same error as you. Are you sure that your JDK is set up correctly? Can you compile other classes?

The only error that I get is on this line...
JOptionPane.showMessageDialog( null, "The sum is " + sum, "Result", JOptionPane.Plain_Messge );
You have spelt Plain_Messge wrong, it should be PLAIN_MESSAGE in caps.
E Pease

Joined: Mar 23, 2004
Posts: 15
I changed the text to PLAIN_MESSAGE and I still get the same message. I am able to complie some programs but not a few. Could it hav something to do with the imports? The program below is one of the ones that run just fine.

// // Java 109 : Networking with Java, by David Reilly
// // MyFirstInternetAddress
// // Displays the IP & hostname of localhost // // */
public class getIP {
public static void main(String args[])
{ InetAddress localaddr = InetAddress.getLocalHost();
System.out.println ("Local IP Address : " + localaddr );
System.out.println ("Local hostname : " + localaddr.getHostName());

catch (UnknownHostException e) {
System.err.println ("Can't detect localhost : " + e); } } }
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Perhaps you've got a funky control character embedded in your source file. This happens sometimes (although it used to happen a lot more in the good old days...

Try selecting and deleting that whole "public static void main" line, and retyping it.

[Jess in Action][AskingGoodQuestions]
E Pease

Joined: Mar 23, 2004
Posts: 15
First let me start by thanking you guys for helping me out.

So, I reemoved the main line and replaced it. I neglected to put in the (String [] args) part before I recomplied it so I got the following error message:

// error message '(' expected
{public static void main{

That made sence so I put in the (String [] args) between main and {. Then I recompiled the program and got the same error message as before:

// error message
.\ ,identifier. expected
E Pease

Joined: Mar 23, 2004
Posts: 15
Sorry, I accidentally posted before I was done.
So, then I created a folder in called test in the folder the original file had been saved in and saved the program in it. Then I recomplied it and it worked fine. I still do not know what the exact problem was but moving to the new folder seemed to fix it. Wierd. Thanks again for all of your help. I hope this thread is useful for others.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Thank you for sharing what you learned.

Note that you can edit your own posts. Just click on the icon that looks a little bit like a piece of paper.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Can't understand complier error
jQuery in Action, 3rd edition