Results 1 to 4 of 4
  1. #1
    baltimore is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Syntax error on token "(", ; expected

    Hi, I can't seem to figure out a syntax error. Here is some stuff for background. I am using Eclipse 3.0.1 IDE and I downloaded Java 5 Update 1 JDK from Sun and installed both (note I installed Eclipse first, not sure if it matters). Windows XP SP2.

    Eclipse is showing this error...
    Java Code:
    Syntax error on token "(", ; expected
    Syntax error on token ")", ; expected
    for the following code.(line 21)

    The IDE underlines the () that I put in RED and says the error is there. I don't get it because I don't think I need a ; anywhere. (and this code is copied directly from my prof's website)

    Thanks for any help, really really new to java, I hope it is something simple.

    Java Code:
    package edu.uwec.cs.muellerzabel.conversion;
    import javax.swing.JOptionPane;
    
    public class Convert {
    
    	public static void main(String[] args) {
    		private static double getDouble(string prompt) {
    	        double x = 0;
    	        boolean done = false;
    	        while (!done) {
    	            try {
    	                x = Double.parseDouble(JOptionPane.showInputDialog(prompt));
    	                done = true;
    	            } catch (NumberFormatException e) {
    	                JOptionPane.showMessageDialog(null,
    	                        "That wasn't a valid input.  Please enter a number.");
    	            }
    	        }
    	        return x;
    	    }
    		
    		double dblUserInput = 0;
    		double dblEnglish = 0;
    		double dblFeet = 0;
    		double dblInches = 0;
    		
    		dblUserInput = getDouble("Enter the number of meters you would like to convert to feet or inches.");
    		
    		dblEnglish = dblUserInput * 39.37;
    		dblFeet = dblEnglish / 12;
    		dblInches = dblEnglish;
    		
    		if (dblEnglish > 12){
    			JOptionPane.showMessageDialog(null, dblUserInput + " meters is equal to " + dblFeet + " feet.");
    		}else {
    			JOptionPane.showMessageDialog(null, dblUserInput + " meters is equal to " + dblInches + " inches.");
    			
    		}
    	}
    }
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    You had the getDouble method inside the main method. This confuses the compiler.
    Also, the type (string) in the getDouble argument (string prompt) should be capitalized, viz, "String prompt".
    This compiles okay:
    Java Code:
    package edu.uwec.cs.muellerzabel.conversion;
    import javax.swing.JOptionPane;
    
    public class Convert {
    
        public static void main(String[] args) {
            double dblUserInput = 0;
            double dblEnglish = 0;
            double dblFeet = 0;
            double dblInches = 0;
    
            dblUserInput = getDouble("Enter the number of meters you would " +
                                     "like to convert to feet or inches.");
    
            dblEnglish = dblUserInput * 39.37;
            dblFeet = dblEnglish / 12;
            dblInches = dblEnglish;
    
            if (dblEnglish > 12){
                JOptionPane.showMessageDialog(null, dblUserInput +
                                   " meters is equal to " + dblFeet + " feet.");
            } else {
                JOptionPane.showMessageDialog(null, dblUserInput +
                                   " meters is equal to " + dblInches + " inches.");
            }
        }
    
        private static double getDouble(String prompt) {
            double x = 0;
            boolean done = false;
            while (!done) {
                try {
                    x = Double.parseDouble(JOptionPane.showInputDialog(prompt));
                    done = true;
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null,
                            "That wasn't a valid input.  Please enter a number.");
                }
            }
            return x;
        }
    }

  3. #3
    romilc is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default Syntax error on token "(", ; expected

    Eclipse is showing this error...
    Code:
    Syntax error on token "(", ; expected
    Syntax error on token ")", ; expected
    line 11 and 15..

    the following is my code:
    import javax.jws.WebService;

    @WebService
    public class try2
    {
    String num1,num2;
    public try2(){}
    public String getNmbers()
    {

    final String getNumber1(String num1)
    {
    return num1;
    }
    final String getNumber2(String num2)
    {
    return num2;
    }
    }
    }

    please help me solving the errors in the above code..

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Compiling
    Java Code:
    public class try2
    {
        String num1,num2;
    
        public try2(){}
    
        public String getNmbers()
        {
            final String getNumber1(String num1)
            {
                return num1;
            }
            final String getNumber2(String num2)
            {
                return num2;
            }
        }
    }
    gives
    Java Code:
    C:\jexp>javac Try2.java
    Try2.java:9: ';' expected
            final String getNumber1(String num1)
                                   ^
    Try2.java:9: ';' expected
            final String getNumber1(String num1)
                                               ^
    Try2.java:13: ';' expected
            final String getNumber2(String num2)
                                   ^
    Try2.java:13: ';' expected
            final String getNumber2(String num2)
                                               ^
    4 errors
    Placing methods inside other methods gets the compiler confused.
    Try something like this instead:
    Java Code:
    public class Try2
    {
        String num1,num2;
    
        public Try2(){}
    
        public String getNmbers()
        {
            return num1 + ", " + num2;
        }
    
        final String getNumber1()
        {
            return num1;
        }
    
        final String getNumber2()
        {
            return num2;
        }
    }

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 10:26 AM
  2. How to solve "No compiler error"?
    By iceman in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 04:37 AM
  3. "Cannont find symbol Constructor" error
    By Welsh in forum New To Java
    Replies: 7
    Last Post: 01-25-2008, 01:12 AM
  4. Strange error message "Source not found"
    By ppayal in forum Eclipse
    Replies: 0
    Last Post: 11-25-2007, 07:19 PM
  5. Replies: 1
    Last Post: 07-25-2007, 11:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •