Inputting then displaying through arrays? Urgent...
We were instructed to make a program where the user will input the Planets' name and mass then the data will be shown in a list (optional:the list should be in alphabetical order). We need to make some sort of menu where it displays 3 choices: Add Planet and Mass, View Record, and exit. I got a help from a friend with some parts except the part when displaying the data. it only says "null". it should look like:
Planets and Their Masses:
Earth 1234
Jupiter 12345
Mars 1234
here's the code that needs editing...I've been searching on how to solve this and can't seem to find the solution, and I need to submit this 5 hours from now.
Re: Inputting then displaying through arrays? Urgent...
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting.
Where is the "null" output? What variable has the null value?
Re: Inputting then displaying through arrays? Urgent...
after adding the data, it will return to the menu, when the user chooses the "View Record" it should show a list of what data they had already entered, for example:
Planets and Their Masses:
Earth 1234
Jupiter 12345
Mars 1234
but instead, these shows up:
Planets and their Masses:
null0.0
Re: Inputting then displaying through arrays? Urgent...
That must mean that the program does not assign any values to the variables that are being printed. Look at the print statement for the variables that are being printed and then backtrack in the code to see why they do not have the values you want to see printed.
Re: Inputting then displaying through arrays? Urgent...
The problem is that when you add a planet, it is storing the values locally. When you try to view the data, you're creating a new instance of that object. To solve this, create a Planets object before the while loop, then add your planets to that object if they choose 1. If they choose 2, display the data.