aspose file tools*
The moose likes Beginning Java and the fly likes <identifier> expected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "<identifier> expected " Watch "<identifier> expected " New topic
Author

<identifier> expected

ryan claunch
Greenhorn

Joined: Jan 31, 2011
Posts: 9
This is my code
ty for the help, i am brand new to java







This is the error the arrors should be under the "=" for some reason the forum isnt letting it

C:\Users\Randy\Desktop\Claunchr_Wk4\Practice.java:21: <identifier> expected
Number= Num;
^
C:\Users\Randy\Desktop\Claunchr_Wk4\Practice.java:22: <identifier> expected
Thing= Ans;
^
2 errors

Tool completed with exit code 1
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

I am assuming you are trying to write a constructor for your class. you need to loose the semicolon on line 20, then wrap the two assignments inside curly braces. right now, you have executable code outside of a method, which isn't legal.

Note: There are a few other issues with your program as well. Once you fix this one, re-compile and try and fix those.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

I find it odd that the compiler didn't mention the missing constructor body as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40051
    
  28
Welcome to the Ranch

Writing useful error messages for a compiler is very difficult. The Sun/Oracle error messages are harder to understand than those from Eclipseâ„¢ for example.
ryan claunch
Greenhorn

Joined: Jan 31, 2011
Posts: 9

is what it is now, however now im getting even more confusing errors

C:\Users\Randy\Desktop\Claunchr_Wk4\Practice.java:18: illegal start of expression
{private int Number;
^

C:\Users\Randy\Desktop\Claunchr_Wk4\Practice.java:21: class, interface, or enum expected
public Practice(int Num, char Ans)
^

C:\Users\Randy\Desktop\Claunchr_Wk4\Practice.java:23: class, interface, or enum expected
Thing= Ans;
^



Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

Why is there a { in front of private int Number; on line 18 of your code?

Fix the indentation of your code: make sure that corresponding { and } are in the same column, so that it is easier to see which closing brace belongs to which opening brace. Also, indent the code that is inside a block, such as lines 14 and 15.

Remove the ; at the end of the declaration of the main() method in line 12.

Note that this program will not be able to run. Java expects the main() method to look like this: public static void main(String[] args). You are declaring it with an array of Practice objects instead of an array of String objects.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
ryan claunch
Greenhorn

Joined: Jan 31, 2011
Posts: 9
was told to put those 2 assignments into those brackets
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

The assignments that you should have put brackets around are in lines 22 and 23, not lines 18 and 19 - those are not assignments, but declarations of member variables.
ryan claunch
Greenhorn

Joined: Jan 31, 2011
Posts: 9
Jesper de Jong wrote:The assignments that you should have put brackets around are in lines 22 and 23, not lines 18 and 19 - those are not assignments, but declarations of member variables.
TY TY TY TY very much, i knew that main was wrong just didnt know how, i think im understanding it a lil better now thanks to you, and all the people who posted above you. works now
Anshul K Jain
Greenhorn

Joined: Jul 24, 2010
Posts: 20

Try with this code basically you makes the methods as abstract. As well code contains many other errors.

public class Practice
{ public static void main(String [] args)
{
Practice object1 = new Practice(6,'$');
System.out.println("Object 1 is:" +object1);

}
private int Number;
private char Thing;
Practice(int Num, char Ans){
Number= Num;
Thing= Ans;
}

}
ryan claunch
Greenhorn

Joined: Jan 31, 2011
Posts: 9
ty but already got it
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40051
    
  28
I can see some other confusion waiting for you, eg the sort of printout you will get because you haven't overridden toString().
ryan claunch
Greenhorn

Joined: Jan 31, 2011
Posts: 9
Campbell Ritchie wrote:I can see some other confusion waiting for you, eg the sort of printout you will get because you haven't overridden toString().
that was the next part of the practice
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

For what it's worth...your code should never have gotten to the state it did in your original post. You should be compiling each time you write a line or two of code. Depending on which you started with, you should have either gotten all the error fixed from your main() method before starting the constructor, or vice-versa.

personally, I do my first compile after writing this much code every time:



My next compilation might be after doing this:



then something like this:



Note that in this last compilation, i've added two new member variables, but haven't done anything with them. I don't try to use them, print them, or even set them. I just want to make sure my declaration is correction. At each stage, I've made minimal changes. if on my new compile I get an error, I can focus in on the one or two lines I've changed, as opposed to slogging through the 20-30 (or god forbid 200).

the less you write before re-compiling, the better off you'll be.
ryan claunch
Greenhorn

Joined: Jan 31, 2011
Posts: 9
im new to java, and we get step by step instructions that are very ambiguous.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40051
    
  28
ryan claunch wrote:im new to java, and we get step by step instructions that are very ambiguous.
Maybe you should put in a query about those instructions.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40051
    
  28
By the way: the names of your variables, Thing and Num, are incorrect style (they should start with small letters) and uninformative; nobody can work out from reading it what "thing" means
ryan claunch
Greenhorn

Joined: Jan 31, 2011
Posts: 9
i dont choose the names
ryan claunch
Greenhorn

Joined: Jan 31, 2011
Posts: 9
im done with the assignment if anyone was wondering what it was gonna end up as
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40051
    
  28
I have corrected yoru indentation, and un-corrected my spelling. Use spaces, not tabs, for indentation. Get a decent text editor (eg NotePad++, NotePad2, jEdit) which supports automatic indentation, bracket matching etc. It makes your job much easier. Set it to convert 1 tab to 4 spaces.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: <identifier> expected