Results 1 to 19 of 19
Thread: <identifier> expected, I'm stuck
- 12-11-2008, 08:40 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
<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.
- 12-11-2008, 08:55 AM #2
public static double selScale(int chosenScale, double convertScale)
you should write like this
I've spent approx. 2 1/2 hours searching the webLast edited by DevzAbhi; 12-11-2008 at 08:58 AM.
DevZ;)
- 12-11-2008, 09:12 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
- 12-11-2008, 09:19 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 12-11-2008, 09:20 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 12-11-2008, 09:20 AM #6
oops my mistake .. you should keep them as integer ..
code by eranga is what u should use .....DevZ;)
- 12-11-2008, 09:25 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
I really like that if our thread starter can keep eye on post #4. :)
- 12-11-2008, 09:38 AM #8
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
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)
Unfortunately, I'm extremely tired (3:33am), so this little adventure isn't so much fun anymore as it was Wednesday night. *laughs*
- 12-11-2008, 09:50 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
All local variables must initialize before use. It's best practice all variables, including instance variable, initialize before use.
- 12-11-2008, 09:55 AM #10
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
- 12-11-2008, 10:02 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Ok, if you are working on an IDE just debug and see what values take by all variables before use.
- 12-11-2008, 10:04 AM #12
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
- 12-11-2008, 10:15 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
On what you are coding on Java? Notepad + Command prompt or any Java IDE?
- 12-11-2008, 10:17 AM #14
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
- 12-11-2008, 10:22 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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.
- 12-11-2008, 03:46 PM #16
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); ...
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-12-2008, 08:15 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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.
- 12-12-2008, 05:56 PM #18
- 12-13-2008, 05:14 AM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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
-
error"<identifier> expected" trough the use of interface
By parme in forum New To JavaReplies: 3Last Post: 12-05-2008, 08:34 PM -
getting identifier expected error . help me !
By victorkeath in forum New To JavaReplies: 3Last Post: 11-07-2008, 05:49 PM -
Identifier Naming Rule
By udayadas in forum Advanced JavaReplies: 1Last Post: 08-21-2008, 05:56 PM -
Identifier expected error
By vasu18 in forum New To JavaReplies: 1Last Post: 01-01-2008, 05:49 PM -
Error: <identifier> expected
By barney in forum AWT / SwingReplies: 2Last Post: 07-31-2007, 07:38 AM
Bookmarks