• 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

Objects inside if statements

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having a very frustrating issue at the moment and perhaps the answer lies here?

I'm currently having an issue with if statements. I want my core.java class to contain an if statement which closes the entire program if my variable counter reaches 2.


I implemented that using a seperate method addCounter() which goes as


The real issue is described here:

I can't seem to come up with a fitting if statement which checks if the method getCounter has reached after addCounter();has been invoked several times

My first idea was to use something such as

if(changeState.getCounter().equals(2)){
System.exit(0);
}
//I also tried using:

if(changeState.getCounter() == 2)

//however, that didn't work either
both lines give me numerous errors which I can't wrap my head around:

.java:476: error: illegal start of type: if(True.getCounter().equals(2)){

.java:476: error: <identifier> expected: if(True.getCounter().equals(2)){

.java:476: error: ';' expected: if(True.getCounter().equals(2)){

.java:476: error: illegal start of type: if(True.getCounter().equals(2)){

.java:476: error: illegal start of type: if(True.getCounter().equals(2)){

.java:476: error: ';' expected: if(True.getCounter().equals(2)){

Could anyone elaborate on what is going wrong and what should be done to overcome this issue? Thank you in advance!

C.C.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Caglar Gul wrote:

That is the correct way to do it.

Your code snippet indicates you have an object reference named 'changeState', yet your error message says you have a class named 'True'. Something is amiss. We need to see more of the code.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Caglar, welcome to the Ranch!

Those problems, actually, they don't have anything to do with what you're trying to do there. What's going on is this: somehow your braces (those { and } characters which delimit code blocks) have got out of order, and you're trying to put lines of code outside of the class. So you have too many closing braces (the } character) before them.

So what you need to do is go through your code and make sure it's properly indented and the opening and closing braces match up. Get rid of the extras. If you're using an IDE (Eclipse, Netbeans, etc) which can do that for you, then definitely ask it to do that. If not, well, you've got nearly 500 lines of code to go through and clean up. That's really too much code for one class but if you're a beginner then you probably aren't being taught good programming practices yet.
 
Caglar Gul
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Caglar Gul wrote:

That is the correct way to do it.

Your code snippet indicates you have an object reference named 'changeState', yet your error message says you have a class named 'True'. Something is amiss. We need to see more of the code.


Hiya! I would like to edit my post but I can't seem to find a edit post button(?)
And thanks for pointing that out! True was a horrible name for instantiating an object, I changed it to changeState so that it could avoid confusion!
 
Caglar Gul
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Hi Caglar, welcome to the Ranch!

Those problems, actually, they don't have anything to do with what you're trying to do there. What's going on is this: somehow your braces (those { and } characters which delimit code blocks) have got out of order, and you're trying to put lines of code outside of the class. So you have too many closing braces (the } character) before them.

So what you need to do is go through your code and make sure it's properly indented and the opening and closing braces match up. Get rid of the extras. If you're using an IDE (Eclipse, Netbeans, etc) which can do that for you, then definitely ask it to do that. If not, well, you've got nearly 500 lines of code to go through and clean up. That's really too much code for one class but if you're a beginner then you probably aren't being taught good programming practices yet.



I must admit that I did see an indentation/bracket error. What didn't happen though is that it made the problem go away :/
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the first 100 lines of code, including any package and import statements. Be sure to use Code tags so that we get line numbers.
 
Caglar Gul
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Can you post the first 100 lines of code, including any package and import statements. Be sure to use Code tags so that we get line numbers.



I'm not sure anyone will benefit from this... my first 100 lines are full of items being added to the JFrame..



This is the code after all the items
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Caglar Gul wrote:

Carey Brown wrote:Can you post the first 100 lines of code, including any package and import statements. Be sure to use Code tags so that we get line numbers.



I'm not sure anyone will benefit from this... my first 100 lines are full of items being added to the JFrame..

Where's the class declaration? Where's the imports? Where's the line 34 mentioned in your error message?
 
Caglar Gul
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Caglar Gul wrote:

Carey Brown wrote:Can you post the first 100 lines of code, including any package and import statements. Be sure to use Code tags so that we get line numbers.



I'm not sure anyone will benefit from this... my first 100 lines are full of items being added to the JFrame..

Where's the class declaration? Where's the imports? Where's the line 34 mentioned in your error message?



My brain is regressing as we speak, it's 3:21 am over here and my brain is returning to an infantile state. I will update the post after a sleep.

Thank you for bearing with me
 
Caglar Gul
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Caglar Gul wrote:

Carey Brown wrote:Can you post the first 100 lines of code, including any package and import statements. Be sure to use Code tags so that we get line numbers.



I'm not sure anyone will benefit from this... my first 100 lines are full of items being added to the JFrame..

Where's the class declaration? Where's the imports? Where's the line 34 mentioned in your error message?


Line 34 was a test of why it wouldn't work so it's not there anymore but here's the code you're requesting:



I hope that this is sufficient?
Thanks!
 
Caglar Gul
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a piece of unused code at line 22, ignore that since it has no use and has already been deleted
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Caglar Gul wrote:. . . here's the code you're requesting: . . .

That appears to be different from what you first posted, and doesn't appear to contain the code producing the compile‑time error you showed yesterday. We can only help if you post the real code that produces the errors.
I can, I am afraid, see lots more about your current code.
  • Don't extend classes like JComponent. You may need to extend JPanel for displaying the game, but it will probably only need its paintComponent() method overriding.
  • Give paintComponent() protected access, and use the following as its first line:- super.paintComponent(g); That will remove the previous image from the panel.
  • Why are those int fields public?
  • I don't like the notion of adding this to a frame. I think you should be adding Panels, as mentioned above.
  • There is something not right about methods to draw a player object. That method belongs in the Player class. The same applies to other sorts of Item.
  • The checkMove method is confusing; there must be a simpler way to do that. Start getting suspicious whenever you see instanceof.
  • I think the individual objects in the game should take care of their own code, which doesn't appear to be happening at the moment.
     
    Caglar Gul
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Caglar Gul wrote:. . . here's the code you're requesting: . . .

    That appears to be different from what you first posted, and doesn't appear to contain the code producing the compile‑time error you showed yesterday. We can only help if you post the real code that produces the errors.
    I can, I am afraid, see lots more about your current code.
  • Don't extend classes like JComponent. You may need to extend JPanel for displaying the game, but it will probably only need its paintComponent() method overriding.
  • Give paintComponent() protected access, and use the following as its first line:- super.paintComponent(g); That will remove the previous image from the panel.
  • Why are those int fields public?
  • I don't like the notion of adding this to a frame. I think you should be adding Panels, as mentioned above.
  • There is something not right about methods to draw a player object. That method belongs in the Player class. The same applies to other sorts of Item.
  • The checkMove method is confusing; there must be a simpler way to do that. Start getting suspicious whenever you see instanceof.
  • I think the individual objects in the game should take care of their own code, which doesn't appear to be happening at the moment.




    Dear Sherif, you're 100% right. You could call this my Magnum Opus made possible with all my current understanding of Java.
    I'm also positive that this could've been different, without making use of instanceof and casting some things. But I couldn't figure that out. I know that this is a clear sign of bad coding and I will be working on getting a grip on that.

    As for the int fields: I need those variables across classes. You're currently seeing my core class but there are  7 more classes which make use of those for generating an interface.

    Yes, I made a mistake by not using panels hence this entire project was a massive headache.

    And you're about right on the extension as well, it did make getting some things easier though the case of the project however allowed us to do something like this. We didn't need to override anything graphically-wise

    Even while writing this, I keep editing the names of variables, classes and methods to make it sound more reasonable, that's why some stuff might not add up like how changeState has been turned in to

    I would like to say that my issue has been fixed as well! So Java doesn't like objects being called without the use of methods and that was exactly what was going on here..
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic