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 4 of 4

Thread: How do I request user value for array object's instance variables with this tester class?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I request user value for array object's instance variables with this tester class?

    I am trying to figure out how to request user input for the values of an array objects instance variables using jOP panes. What would I use to do this?

    Here is my tester class.
    import javax.swing.JOptionPane;
     
    public class StudentTester{
        public static void main( String[] args ){
     
            // create an array of student variables
            Student[] studArray = new Student[4];
     
            // instantiate students using full constructor
            studArray[0] = new Student("Bob", "Barker", "123456", 1600);
            //studArray[1]setSatScore(JOptionPane.showInputDialog("Please enter a valid SAT score. Allowable range is 600 to 2400"));
            studArray[1] = new Student("Carol", "Cowart","234561", 1500);
            studArray[2] = new Student("Dan", "Smith", "345612", 1550);
            studArray[3] = new Student("Carl", "Newell", "456123", 1800);
     
            // instantiates objects and displays all get methods for one student object
            for( int i = 0; i < studArray.length; i++ ){
                System.out.println( "First name " + i + " is: \n" + studArray[0].getFirstName());
                System.out.println( "Last name " + i + " is: \n" + studArray[0].getLastName());
                System.out.println( "ID number " + i + " is: \n" + studArray[0].getIdNumber());
                System.out.println( "SAT Score " + i + " is: \n" + studArray[0].getSatScore() +"\n" );
     
    		// second loop to display toString output
            for( int j = 0; j < studArray.length; i++ ){
                System.out.println( "The student at array index " + i + " is: \n" + studArray[i].toString() +"\n" );
     
    		}
    	}
    }
    }


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

    Default Re: How do I request user value for array object's instance variables with this tester class?

    Are you trying to build a String to show in a JOptionPane method using the contents of some arrays like the posted code does for the println() statements in the for loops?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I request user value for array object's instance variables with this tester class?

    Yes I believe so, and I need the user to be able to set the values for the three string variables (first name, last name, id number) and the lone int variable (SAT score).

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

    Default Re: How do I request user value for array object's instance variables with this tester class?

    I was asking if you wanted to build a String to show as a message to the user.
    Your last post seems to say you want to read data from a user and store it into variables.
    Do you want to use a method of the JOptionPane class to read data from a user and store what the user enters in a variable? The String read for the number will have to be converted from a String to a number using a method of the Integer or Long class depending on how large it is.

    There are lots of examples of the uses of JOptionPane class methods here on the forum. Do a search to find some.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 149
    Last Post: February 19th, 2013, 10:04 PM
  2. Replies: 1
    Last Post: February 14th, 2013, 07:30 PM
  3. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 08:34 PM
  4. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  5. [SOLVED] Instance data member vs Local variables (primitive/object reference)
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 12:42 AM