File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Array and GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array and GUI" Watch "Array and GUI" New topic
Author

Array and GUI

Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
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

Joined: Sep 08, 2009
Posts: 76
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?
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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

Joined: Dec 17, 2003
Posts: 624

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

Joined: Sep 08, 2009
Posts: 76
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

Joined: Sep 08, 2009
Posts: 76
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

Joined: Sep 08, 2009
Posts: 76
I'll post my code again since there have been some changes since i orginally did so
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

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


I love this place!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Sep 08, 2009
Posts: 76
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

Joined: Sep 08, 2009
Posts: 76
I figured the readData thing figured out but i could use some help on the above and below average methods
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array and GUI
 
Similar Threads
GUI Big trouble
Getting an Average
toString
JButtons.... nothing to do with GUI
Cannot throw exception