• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Array and GUI

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey there... I am trying to test this program before i start trying to incorporate the GUI stuff. I am new to this stuff and I seem to be getting a type casting error. Can someone take a look and let me know what you think?

If you have not figured it out.. the array is suposed to represent a apartment building with each element of the array representing individual apartments. We are supose to read data into the array and then perform the different operations as seen in my methods... the type cast error is get is in the getAverage method. I've tried to cast it as an int and as a double and neither way will work...
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok forget about the tyoe casting i got that figureed out... but now when i go to compile i get a null pointer exception in the main method somewhere... does anyone know how to remedy that?
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your getAverage method is declared to return an int. However, the math you perform results in a double, which is a floating point number. You want to assign that calculation to avgocc, which is a double. So that's fine. However, you cannot use avgocc as the return value since it is a double and your method is declared to return an int. So you either need to cast it to an int (option 1) and lose precision (2 vs 2.4) before you return it, or your method needs to be declared to return a double rather than an int (option 2).



Since your avgocc variable is a double, and since it makes more sense that an average be a floating number (unless your requirements say otherwise) I think option two is the better one.

You have some other issues looming. I'll comment on them in a another post.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bryan Peach wrote:ok forget about the tyoe casting i got that figureed out... but now when i go to compile i get a null pointer exception in the main method somewhere... does anyone know how to remedy that?



Can you post the entire exception you are getting, along with your new code. I can then teach you how to read an exception stack trace so you can answer your question yourself. A much more valuable outcome.
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got the excepction... it was because i was declaring my Scanner in object in the main... and it was looking for it in my read data method... all i did was move the declaration into the method and it worked.... however in my loop for readData I want it to get the nextInt.. and then prompt me to eneter the value for the next apartment number... but it is not going that... my results are this..


Please enter the number of occupants in apartment #1
1
2
3
4
5
6
7
8
9
Please enter the number of occupants in apartment # 1
The total number of people in the building is: 45
The Average number of people in each apartment is: 4
The number of apartments with above average occupants is: 35
The number of apartments with below average occupants is: 10


there is also something seriously wrong with my above and below average methods that i need to figure out
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so i'm trying to solve my above average method and i wrote it like this:


but it is giving me this error:


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Apartment.getAboveAverage(Apartment.java:51)
at Apartment.main(Apartment.java:78)


but i don't get it because my array has 10 spots so my loop should only check up until 9 right? why is it giving me this error?
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll post my code again since there have been some changes since i orginally did so
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well in the code you posted...


you declare i in your for loop but then you are incrementing index...?
Change index and the line below to i instead.

Also although not important I would write the for loop as, more readable:


Sean
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a classic "gotcha" that has gotten all of us at least once in our coding career:



Your for loop here is not doing what you think it's doing, and this is partly due to indentation that is leading you astray. Because the for loop block is not enclosed in curly braces, it is only looping on the first line, on this statement:


and the loop does not contain this statement:


The solution: always place all loops and if blocks within curly braces. For example:
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the pointers guys.. it seems like even pretty simple programming tasks get me very confused... the readData loop kind of works how i want it now but instead of geting 1,2,3,4 etc... it is giving me


Please enter the number of occupants in apartment #1
1
Please enter the number of occupants in apartment #0
2
Please enter the number of occupants in apartment #2
3
Please enter the number of occupants in apartment #4

4
Please enter the number of occupants in apartment #6
5



And I cannot for the life of me figure out this aboveaverage or below average thing.

i don't know what that gives me but its not the number of apartments with above average occupants... please help...
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured the readData thing figured out but i could use some help on the above and below average methods
 
reply
    Bookmark Topic Watch Topic
  • New Topic