Results 1 to 19 of 19
  1. #1
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Unhappy <identifier> expected, I'm stuck

    Hello guys,

    First time poster, and new to Java. I'm trying to make a temperature converter for my Java Programming class, which would convert amongst the three scales (Cel, Fah, Kelvin). I was on a role but then ran into a problem.

    Here's the code (Red is where the errors occured):


    Java Code:
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    
    public class Project
    {
    	public static void main(String[] args)
    		{
    			System.out.println("\tThe Celrenvin Temperature Converter");
    
    			//declare class variables
    			int chosenScale, convertScale;
    			double userValue, convert;
    
    			//call methods
    			chosenScale = getScale();
    			userValue = getValue();
    			selScale(chosenScale, convertScale);
    		}
    
    
    	//The getScale method asks the user to input a temperature scale to convert FROM.
    	public static int getScale()
    	{
    		//declare method variables
    		int scale = 0;
    		boolean done = false;
    
    		//loop while not done
    		while(!done)
    		{
    			try
    			{
    				String message = JOptionPane.showInputDialog(null,"Enter the number corresponding with the temperature scale you are converting from: \n1) Celsius \n2) Fahrenheit \n3) Kelvin\n\n");
    
    				scale = Integer.parseInt(message);
    
    
    				//test for valid scales 1, 2, or 3
    				if (scale<1 || scale>3) throw new NumberFormatException();
    				else done = true;
    			}
    			catch(NumberFormatException e)
    			{
    				JOptionPane.showMessageDialog(null,"Please enter a 1, 2, or 3.","Error",JOptionPane.INFORMATION_MESSAGE);
    			}
    		}
    		return scale;
    	}
    
    	//The getValue() method asks the user to input a temperature value and validates it.
    	public static double getValue()
    	{
    		//declare method variables
    		double value = 0.0;
    		boolean done = false;
    
    		//loop while not done
    		while(!done)
    		{
    			try
    			{
    				String answer = JOptionPane.showInputDialog(null,"Enter a temperature value:");
    
    				value = Double.parseDouble(answer);
    
    				//test for valid value
    				if (value <= 0) throw new NumberFormatException();
    				else done = true;
    			}
    			catch(NumberFormatException e)
    			{
    					JOptionPane.showMessageDialog(null,"Your entry was not in the proper format.",	"Error",JOptionPane.INFORMATION_MESSAGE);
    			}
    		}
    		return value;
    	}
    
    	//The selScale() method accepts the scale offers conversions for the chosen scale.
    	[COLOR="Red"][B]public static double selScale(int chosenScale, convertScale)[/B][/COLOR]
    	{
    			int chosen = 0;
    
    			switch(chosenScale)
    			{
    				case 1:
    					chosen = convertScale;
    					break;
    
    				case 2:
    					chosen = convertScale;
    					break;
    
    				case 3:
    					chosen = convertScale;
    					break;
    			}
    		return chosen;
    	}
    [COLOR="Red"][B]}[/B][/COLOR]





    I'm prompted with 2 errors when I try to compile:
    (The blue is where the indictator was pointing)



    Java Code:
    [COLOR="red"]<identifier> expected 
    public static double selScale(int chosenScale, convertScale[COLOR="Blue"])[/COLOR]
                     
    ')' expected [COLOR="blue"]}[/COLOR][/COLOR]


    I'm not done coding the program yet. I've spent approx. 2 1/2 hours searching the web, brainstorming, trying to figure out what the problem is specifically. Any help would be greatly appreciated. I have to turn this by Thursday night at 12am, so I'm short on time I'm afraid.

  2. #2
    DevzAbhi's Avatar
    DevzAbhi is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    public static double selScale(int chosenScale, double convertScale)


    you should write like this

    I've spent approx. 2 1/2 hours searching the web
    its a syntax error that is why you are not able to search it on web
    Last edited by DevzAbhi; 12-11-2008 at 09:58 AM.
    DevZ;)

  3. #3
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by DevzAbhi View Post
    public static double selScale(int chosenScale, double convertScale)


    you should write like this

    Is there a way I could keep them both as integers? Since I have them both as 'int' in the class variables?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by UnderpaidWorker View Post
    Is there a way I could keep them both as integers? Since I have them both as 'int' in the class variables?
    Java Code:
    public static double selScale(int chosenScale, [COLOR="Red"]int[/COLOR] convertScale)

  6. #6
    DevzAbhi's Avatar
    DevzAbhi is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    oops my mistake .. you should keep them as integer ..

    code by eranga is what u should use .....
    DevZ;)

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    I really like that if our thread starter can keep eye on post #4. :)
    Thank you for the link, it's very informative.

    I actually tried doing that about an hour and a half ago, but gave up because I received a:

    Java Code:
    variable convertScale might not have been initialized
    public static double selScale(int chosenScale, int convertScale)
    I just got the same error just now.

    Unfortunately, I'm extremely tired (3:33am), so this little adventure isn't so much fun anymore as it was Wednesday night. *laughs*

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    All local variables must initialize before use. It's best practice all variables, including instance variable, initialize before use.

  10. #10
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    All local variables must initialize before use. It's best practice all variables, including instance variable, initialize before use.
    Thank you, I actually woke up a little now after getting over the original problem.

    I appreciate it guys, I'll post the final code once it's complete.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, if you are working on an IDE just debug and see what values take by all variables before use.

  12. #12
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Ok, if you are working on an IDE just debug and see what values take by all variables before use.
    Could you repeat that?

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  14. #14
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    On what you are coding on Java? Notepad + Command prompt or any Java IDE?
    Java IDE, specifically Text Pad.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, if you are working on the Text Pad, just forget about debugging and stuff. First of all read the link I've post. Then read more about variable declarations and stuff. It'll helpful you to solve the problem. Then think about other stuff in later.

  16. #16
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default println

    Suggestion... a simple way to debug a program is to put temporary println commands in the parts of your code that you think are not working so that you can see the flow of data and variable values.
    For example:
    Java Code:
    ...
    int anInt = goGetSomething();
    System.out.println("goGetSomething returned: " + anInt);
    ...
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all I think our thread starter must have a clear idea about the debugging. Seems to me he's mess on that my few last replays.

  18. #18
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Quote Originally Posted by CJSLMAN View Post
    Suggestion... a simple way to debug a program is to put temporary println commands in the parts of your code that you think are not working so that you can see the flow of data and variable values.
    For example:
    Java Code:
    ...
    int anInt = goGetSomething();
    System.out.println("goGetSomething returned: " + anInt);
    ...
    Luck,
    CJSL

    This technique is a life saver. I only use the debugger in extremely rare cases. Pretty much everything can be figured out using a print statement if you know what your looking for in the first place.

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by xcallmejudasx View Post
    This technique is a life saver. I only use the debugger in extremely rare cases. Pretty much everything can be figured out using a print statement if you know what your looking for in the first place.
    All the time I use a debugger. Few reasons are cause for that. Major thing is typing extra lines in the code can be mess later. :cool: Other thing is, in this way we can print and see only simple variables, I mean if I want to see the values in an array, need to print values in a loop. That means when the debug information become more complex additional lines take into. Watch list the simplest one I can use in such cases. :)

Similar Threads

  1. Replies: 3
    Last Post: 12-05-2008, 09:34 PM
  2. getting identifier expected error . help me !
    By victorkeath in forum New To Java
    Replies: 3
    Last Post: 11-07-2008, 06:49 PM
  3. Identifier Naming Rule
    By udayadas in forum Advanced Java
    Replies: 1
    Last Post: 08-21-2008, 06:56 PM
  4. Identifier expected error
    By vasu18 in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 06:49 PM
  5. Error: <identifier> expected
    By barney in forum AWT / Swing
    Replies: 2
    Last Post: 07-31-2007, 08:38 AM

Posting Permissions

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