• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

<identifier> expected

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it odd that the compiler didn't mention the missing constructor body as well.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;
^



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ryan claunch
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
was told to put those 2 assignments into those brackets
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 20
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ty but already got it
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im new to java, and we get step by step instructions that are very ambiguous.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dont choose the names
 
ryan claunch
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im done with the assignment if anyone was wondering what it was gonna end up as
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Acetylsalicylic acid is aspirin. This could be handy too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic