Welcome to Dream.In.Code
Become a Java Expert!

Join 149,721 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,295 people online right now. Registration is fast and FREE... Join Now!




Printing out an array through a function

 
Reply to this topicStart new topic

Printing out an array through a function

tsukage
12 Feb, 2007 - 11:03 AM
Post #1

New D.I.C Head
*

Joined: 17 Oct, 2006
Posts: 5


My Contributions
I am writing a program for my Java class in which a user will enter up to 10 First and Last Names and the birthdate sorted in different manners, but for right now my main concern is even being able to print out the arrays. Heres my code...

CODE
import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.*;

public class Program_02
{
    public static void main(String[] args)
    {
        //A count for the total number of people the user would like to enter into the program
        int total_names;
        
        do
        {
            String Run_Question = JOptionPane.showInputDialog(null, "How many names would you like to enter" +
                " into the program?(Up to 10 max)", "How Many?", JOptionPane.QUESTION_MESSAGE);
            
            total_names = Integer.parseInt(Run_Question);
            
            //If the user enters more names or less names for example a negative or a number larger than 10
            //This error is displayed.
            if(total_names < 0 || total_names > 10);
            {
                JOptionPane.showMessageDialog(null, "That is not a valid amount of names", "Error!",
                JOptionPane.INFORMATION_MESSAGE);
            }
        }while(total_names < 0 || total_names > 10);
        {
            for(int index = 0;index < total_names;index ++)
            {
                //Establishes strings to hold the data
                String[] First_Name_String = new String[15];
                String[] Last_Name_String = new String[15];
                String[] Birth_Date_String = new String[15];
                
                First_Name_String[index] = JOptionPane.showInputDialog(null, "What is the First Name?", "First Name", JOptionPane.QUESTION_MESSAGE);
                Last_Name_String[index] = JOptionPane.showInputDialog(null, "What is the Last Name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
                 Birth_Date_String[index] = JOptionPane.showInputDialog(null, "What is the Birth Date(MM/DD/YYYY)?", "Birth Date", JOptionPane.QUESTION_MESSAGE);    
                printFirst(First_Name_String, index);
            }
        }
        
    }
    
    public void printFirst(String[] First_Name, int count)
    {
        JOptionPane.showMessageDialog(null, First_Name[count], "Printing results",JOptionPane.INFORMATION_MESSAGE);
    }
}



non-static method printFirst(java.lang.String[],int) cannot be referenced from a static context this is the error i get on line 38
CODE
printFirst(First_Name_String, index);


If I can atleast get it to print or find a more efficient way to get it to print I can move on to sorting and printing.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Printing Out An Array Through A Function
12 Feb, 2007 - 11:09 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,350



Thanked: 51 times
Dream Kudos: 25
My Contributions
It looks like you are trying to call the function (which is outside your static method) from inside the static method. You may have to declare the function inside the static method, or declare an instance of the main class, then call the function using that.
User is online!Profile CardPM
+Quote Post

tsukage
RE: Printing Out An Array Through A Function
12 Feb, 2007 - 08:56 PM
Post #3

New D.I.C Head
*

Joined: 17 Oct, 2006
Posts: 5


My Contributions
QUOTE(Amadeus @ 12 Feb, 2007 - 12:09 PM) *

It looks like you are trying to call the function (which is outside your static method) from inside the static method. You may have to declare the function inside the static method, or declare an instance of the main class, then call the function using that.



Thanks, I got that part now with your help, I had to change it around and I also forgot to put static void i just had public void all i gotta do is hope i can sort these right lol
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:52AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month