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: Object array with constructor

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Object array with constructor

    I have a class project that requires a Symbol array. The parameters are:

    Symbol class with constructor and methods equals and toString and a TestDriver class that declares the Symbol array and main. Here's what I have:
    public class Symbol {
    private String inputSymbol; //Instance variable
     
        public Symbol(String sym){
            inputSymbol = sym;
        }
     
        public boolean equals(Symbol sym){
            return false;
        }
     
        public String toString(){
        }

    public class TestSymbols {
     
    public static void main(String[] args) throws IOException {
    Symbol[] sym = new Symbol[100];
     
    sym[0] = new Symbol("Katie");
    String tempText = sym[0].toString();
    System.out.println(sym[0]);
    System.out.println(tempText);
     
    System.exit(0); //end program
     
    }
     
    }

    The print statements are always null. What am I doing wrong that I cannot populate/access sym? How do I add and read an element at sym [i]?

    Thanks for your halp.
    Last edited by helloworld922; September 8th, 2010 at 03:17 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,403
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Object array with constructor

    Does the code you've posted compile? The toString() method doesn't appear to return anything.

    Please use code tags when posting your code: Java Forums - BB Code List

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Object array with constructor

    Thank you for the quick reply. Resolved!!! toString needed to return the instance variable (I had it returning null). To summorize, type String, for example, is a class and when you type String. and get all the available options, those are the methods. By creating class Symbol, the methods in that class are associated with Symbol.
    Last edited by thedolphin13; September 8th, 2010 at 10:56 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,403
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Object array with constructor

    (I had it returning null).
    So why do you post code that is NOT the code with the problem?????

Similar Threads

  1. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM
  2. Array object and constructors
    By TarunN in forum Collections and Generics
    Replies: 14
    Last Post: May 6th, 2010, 04:04 PM
  3. How do you set an object in array to null value?
    By Arius in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 02:50 AM
  4. Private Constructor
    By Ganezan in forum Object Oriented Programming
    Replies: 4
    Last Post: November 7th, 2009, 03:02 PM
  5. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM