This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

"The type of the expression must be an array type but it resolved to double" error in Java  RSS feed

 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

The following program is quite simple, there is one single line of car (6 cities i.e 6 stations) and the prices corresponding to the journey. The user enter a departure city and a destination city and the prog should return the total price of the journey.
In the method priceJourney, I get the following error : "The type of the expression must be an array type but it resolved to double". I guess it is linked with the variable scope or smth but can't make it work.
I welcome any suggestions to make the program better, I guess these global variables at the beginning are not the best.
Thanks for your help !


 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37234
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank,
Welcome to CodeRanch!

You have two variables named "prices". One is a static variable. The other is a method parameter:


Since you are in the method, you static version is "hidden" and when you write "prices" it uses the method one. Which is of type double, not double[].

You could solve this in a few ways:
  • Get rid of the prices method parameter and just use the static variable
  • Rename the method parameter "prices" to something else like "currentPrice"
  • Change double prices to double[] prices
  •  
    Frank Poupard
    Ranch Hand
    Posts: 65
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello Jeanne,
    thank your for the welcoming !
    You must be totally right, but none of the ways you have proposed seems to work, this must be something trivial I've overlooked.
    For exemple, when I try to rename the methode parameter "prices" to another (2nd way) I get the following errors in the main (line 22) :

    "- indexDest cannot be resolved to a variable
    - indexDepart cannot be resolved to a variable"


    Do you have an idea? Thanks again.

     
    Jeanne Boyarsky
    author & internet detective
    Sheriff
    Posts: 37234
    519
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Frank Poupard wrote:"- indexDest cannot be resolved to a variable
    - indexDepart cannot be resolved to a variable"

    That's a separate problem. You had it originally too even if the compiler didn't tell you about it. Look at your main method. Where are those two variables defined?
     
    Frank Poupard
    Ranch Hand
    Posts: 65
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeanne Boyarsky wrote:
    Frank Poupard wrote:"- indexDest cannot be resolved to a variable
    - indexDepart cannot be resolved to a variable"

    That's a separate problem. You had it originally too even if the compiler didn't tell you about it. Look at your main method. Where are those two variables defined?


    Indeed, they are not defined in main...I just defined them (in this case they are both defined in main and in methods departCities and destCities) but still got the same errors. There must be something I don't understand !
     
    Jeanne Boyarsky
    author & internet detective
    Sheriff
    Posts: 37234
    519
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Can you post your current code and the current error?
     
    Frank Poupard
    Ranch Hand
    Posts: 65
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sure, here is the code.
    The error message I get is from line 19 :

    "Multiple markers at this line
    - indexDest cannot be resolved to a
    variable
    - indexDepart cannot be resolved to a
    variable"
    (with IDE Eclipse)


     
    Jeanne Boyarsky
    author & internet detective
    Sheriff
    Posts: 37234
    519
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks. As a first thing to note, you aren't using currentPrice in priceJourney() so you might as well remove it.

    As far as your main method, you have this:


    Notice how indexDepart and indexDest are not declared in your main method nor are they static variables.
     
    Frank Poupard
    Ranch Hand
    Posts: 65
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I've followed your advice, removed the variable currentPrice and declared indexDepart and indexDest in the main method.
    This time no complains from the compiler but the prog return the value 0.0 whatever I enter...
     
    Jeanne Boyarsky
    author & internet detective
    Sheriff
    Posts: 37234
    519
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Now that the code compiles, it is time to start debugging. Try adding a println in priceJourney and comparing the actual values to the ones you expect.

    Or better yet, try to walk through your code on paper and see what you expect the variables to be at each step.
     
    Frank Poupard
    Ranch Hand
    Posts: 65
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, this what I'm going to do. Thank you very much for your time and suggestions!
     
    Frank Poupard
    Ranch Hand
    Posts: 65
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello, so I have debugged, and it looks like that in method priceJourney, the parameters int indexDepart int indexDest have the value I declared in main, that is 0. I would like to use the return value of indexDepart and indexDest. Otherwise, I will get 0.0 as output forever.
    How can I do that?

    Once again I put the code for sake of clarity :


     
    Frank Poupard
    Ranch Hand
    Posts: 65
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OK, I've made it work now, finally...
    thanks again ;)
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!