Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Sum values in an array JAVA

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello I have an array of integers that I need to total.
As this is for school, we are not allowed to use array lists or for loops.

I have copied the example as shown by the tutor - but Im getting an error "the type of expression must be an array type but it resolves to an int" (using Eclipse)
Been searching everywhere - but not any luck


Thanks in advance
Shane
 
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

That code looks pretty straightforward. The total variable is declared as a local variable of calcTripFuel method so it must be initialized before you use it, just like you did with the variable i on line 3.

Also, since you've declared total as a local variable, how are you going to communicate the results with the rest of the program? Either return the result from the method (right now you're not since the method is declared as void) or set an instance variable. The former approach is preferable though.

Does Eclipse say what line number the error occurs? Also, there should be a red indicator on the offending line in the editor.
 
Shane Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick response
Il post the entire code
The error is happening on line 88 in eclipse - line 87 here total += legDistance;

[i]Code Deleted in case running the risk of online plagiarism failure when being marked.

Shane

Staff note (Piet Souris):

Note that Junilu's replies are based on the now missing code. Editing posts after these have been replied to is usually a bad thing, but in this case Shane has a good point,

 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Smith wrote:Thanks for the quick response
Il post the entire code
The error is happening on line 88 in eclipse - line 87 here total += legDistance[i]


If you read our wiki page on how to UseCodeTags (←click that, it's a link to the instructions), you'll see the code tags have a firstline option to make line numbers match what you have in your IDE, like this:


The problem is that you've declared legDistance as an int but you're using it on line 88 as though it was declared as an array.
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quickly scanning your code, the only variable I see that's declared as an array is leg.
 
Shane Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Quickly scanning your code, the only variable I see that's declared as an array is leg.



Maybe that's where Ive lost the plot

I have made an array (I think) " int[] legDistance; " on line 49

So I was assuming the distances being entered were being saved into that array ?

This is really confusing to me

Thanks for the help !!
Shane
 
Marshal
Posts: 68899
275
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using legDistance as an int[] in line 87 but you declared it as an int in line 21. That is why you are getting the compiler error.
You have a local variable in lines 49‑53 with the same name. It is usually a bad idea to shadow a name like that; maybe that has caused you confusion. What's more, that array is a local variable with existence only in that method. That is probably a logoic error which the compiler doesn't pick up, but Eclipse might give you a yellow triangle saying the variable is not used locally.
There is a new way to summate an int[]. It would work with only a small change for a long[] or a double[]:-Links:- 1 2. Remember the possibility of an overflow error when adding many integers.

By the way: write “Java” please, not “JAVA” nor “java”.

And welcome to the Ranch (again).
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The legDistance array you declared on line 49 is local to the numLegs() method. That means it only exists in that method and it is created every time that method is called, lives for the duration of the call and then disappears. The next time numLegs() is called, another "incarnation" of the variable is created, used, and then disappears.

The legDistance variable on line 21 is what line 87 (88 in your actual code) is referring to. Since, this variable is declared outside of any methods and it's non-static, it will exist for any instance of the CessnaFuelPlanner class. That is, each CessnaFuelPlanner object you create will have its own copy of the legDistance int variable. This variable is "shadowed" by the local variable legDistance that's declared as an array in the numLegs() method, but only in the numLegs() method. Anywhere else in the class, a reference to a non-local legDistance will resolve to the variable on line 21.
 
Shane Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your assistance is most appreciated.
Yes I think I can see what you are referring to and will make those alterations.
Thanks for the posting pointers, I'll be more careful with future post.
I am enjoying Java and should be around the ranch often.
Regards
 
Campbell Ritchie
Marshal
Posts: 68899
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Smith wrote:. . . most appreciated. . . Regards

Think nothing of it I am sure Junilu would say the same.
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic