Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Inputting then displaying through arrays? Urgent...

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    import java.io.*;
    import java.util.Arrays;
    public class Planets
    {
        int a=0;
        int b=0;
        String[] planet = new String[8];
        double[] mass = new double[8];
        public static void main(String[]args)throws Exception
        {
            int choice;
     
     
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            do
            {
            System.out.println("[1] Add Planet/Mass");
            System.out.println("[2] View Records");
            System.out.println("[3] Exit");
            System.out.print("Select your choice: ");
            choice = Integer.parseInt(br.readLine());
            if (choice==1)
                {
                System.out.println("\n\n");
                Planets i = new Planets();
                i.Input();
                }
            if (choice==2)
                {
                System.out.println("\n");
                Planets v = new Planets();
                v.View();
                }
            }
     
            while (choice!=3);
            System.out.println("Thank You!");
        }
     
        public void Input()throws Exception
        {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter planet name: ");
        planet[0] = br.readLine();
     
     
        System.out.println("Enter mass: ");
        mass[0] = Double.parseDouble(br.readLine());
     
        }
     
        void View()
        {
            System.out.println("Planets and their Masses: ");
            System.out.println("" +planet[0] +mass[0]);
     
        }
     
     
    }
    Last edited by noia; May 8th, 2012 at 12:09 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default 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.

Similar Threads

  1. Need urgent help regarding java word wrap function.. URGENT
    By coldice in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 05:43 AM
  2. Not displaying
    By toksiks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:32 AM
  3. Replies: 3
    Last Post: February 21st, 2011, 07:25 PM
  4. need help inputting values into an array
    By pds8475 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 09:47 PM
  5. inputting of text
    By Subhasis Banerjee in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: October 30th, 2009, 12:53 PM

Tags for this Thread