Java Problem defining a constant in java

  • Thread starter Thread starter stonecoldgen
  • Start date Start date
  • Tags Tags
    Constant Java
AI Thread Summary
The discussion revolves around a syntax error encountered in Java code when defining an array of objects. The user initially defines a constant for the number of scenarios and attempts to create an array of "escenario" objects, but encounters a syntax error. It is pointed out that "escenario" should be spelled with an uppercase "E" as "Escenario," aligning with Java naming conventions. After correcting the spelling, the user still faces the same syntax error. The conversation clarifies that if "Escenario" is a class, the correct syntax for creating an array should be "Escenario[] escenarios = new Escenario[NUMERO_ESCENARIOS];" rather than using square brackets incorrectly in the constructor. This highlights the importance of proper syntax and adherence to Java conventions when defining classes and arrays.
stonecoldgen
Messages
108
Reaction score
0
I have:

Code:
public final static int NUMERO_ESCENARIOS=4;

escenarios = new escenario[NUMERO_ESCENARIOS];

having an error in the first line that says: Syntax error on token ";", , expected


but when I have:
Code:
public final static int NUMERO_ESCENARIOS=4 //semi colon deleted

escenarios = new escenario[NUMERO_ESCENARIOS];

I get no error in the definition of the constant but I obviously get a syntax error in the array definition

and when I have:

Code:
public final static int NUMERO_ESCENARIOS=4;

//array definition errased

the constant definition is fine





so what the hell is happening? thanks
 
Technology news on Phys.org
Have you defined "escenario" as a class? (by the way, it is considered good Java practice to spell you classes with an initial upper case letter).
 
Filip Larsen said:
Have you defined "escenario" as a class? (by the way, it is considered good Java practice to spell you classes with an initial upper case letter).

yes, and now that you mention it, I found out that escenario was actually Escenario (so I changed everything in my code to have that uppercase)

however, I still have the initial problem
 
stonecoldgen said:
I have:

Code:
public final static int NUMERO_ESCENARIOS=4;

escenarios = new escenario[NUMERO_ESCENARIOS];

having an error in the first line that says: Syntax error on token ";", , expected

If Escenario is a class, why are you using square brackets (which are used to access an array or List element) to pass NUMERO_ESCENARIOS to the constructor method? Shouldn't you have something like

Code:
public final static int NUMERO_ESCENARIOS=4;

Escenario escenarios = new Escenario(NUMERO_ESCENARIOS);

instead?
 
Or, if you are trying to create an array of Escenario objects, then you would first need to define escenarios to be an Array variable of type Escenario:

Code:
public final static int NUMERO_ESCENARIOS=4;

Escenario[] escenarios = new Escenario[NUMERO_ESCENARIOS];
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top