New to Programming .. Passing an array of objects into a method?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1355 Views - Last Post: 20 May 2012 - 12:41 PM Rate Topic: -----

#1 MaccaTractor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-May 12

New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 09:12 PM

Hi, I'm new to programming and I don't really understand what I've done wrong ... I think my code compiles fine, but there are some logical errors, possibly something to do with the way that I'm attempting to pass an array of objects into some of the methods in the same class?

Any help would be greatly appreciated ... I'm open to criticism as i know I'm not very good at programming, but please be constructive, there's nothing worse than someone saying you're shit and not giving you any tips on how to improve

I can post the question i am trying to answer if that would help? Just let me know, thanks!

import java.util.*;

public class Subject {

	private String name;
	private String code;

    public Subject(String name_input, String code_input) {
    	this.name = name_input;
    	this.code = code_input;
    }

    public String getName () {
    	return name;
    }

    public String getCode () {
    	return code;
    }

    public String getDiscipline () {
    	String discipline;
    	return discipline = code.substring (0,2);
    }

    public boolean codeMatches (String input) {
    	boolean match;
    	return match = (input == code);
    }

    public String toString () {
    	String name_code;
    	return name_code = (name + ' ' + code);
    }

    public String [] allDisciplines (Subject [] subjects) {
    	String [] disciplines = new String [subjects.length];
    	for (int i = 0; i < subjects.length; i++) {
    		disciplines [i] = getDiscipline ();
    	}
    	Arrays.sort(disciplines);
    	return disciplines;
    }

    public String [] codesPerDiscipline (Subject [] subjects, String subjectCode) {
    	String [] codes = new String [subjects.length];
    	ArrayList<String> disciplineCodes = new ArrayList<String>();
    	int x = 0;
    	for (int i = 0; i < subjects.length; i++) {
    		codes [i] = getCode ();
    		if (subjectCode == codes [i].substring (3,5));
    		disciplineCodes.add (x,codes [i]);
    		x++;
    		}
    	String [] disCodes = new String [disciplineCodes.size ()];
    	disciplineCodes.toArray(disCodes);
    	return disCodes;
    }

    public boolean isValidCode (String input) {
    	int length = input.length ();

    	if (input == null)
    		return false;

    	for (int i=0; i<3; i++) {
    		if (!Character.isDigit(input.charAt(i)))
			return false;
    		}

    	for (int i=3; i<6; i++) {
    		if (!Character.isLetter(input.charAt(i)))
			return false;
    		}

    	if (length == 6)
    		return true;
    	else return false;
    }

    public boolean codeExists ( Subject [] subjects, String subjectCode) {
    	String [] codes = new String [subjects.length];
    	for (int i = 0; i < subjects.length; i++) {
    		codes [i] = getCode ();
    		if (subjectCode == codes [i])
    		return false;
    	}
    	return true;
    }

    public Subject [] sortDisciplines (Subject [] subjects) {
    	Arrays.sort(subjects);
    	return subjects;
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: New to Programming .. Passing an array of objects into a method?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,471
  • Joined: 27-December 08

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 09:20 PM

What specific problems are you encountering?
Was This Post Helpful? 1
  • +
  • -

#3 MaccaTractor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-May 12

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 09:25 PM

Wow that was quick! ... I wrote a test program (I've deleted it since because it didn't work) ... but basically what the program did was create three Subject objects and then create an array out of those three objects ... i would then try and use the array of Subject objects to test some of the moethods and i kept receiving an error saying something like "non static method cannot be referenced from a static context"
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,471
  • Joined: 27-December 08

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 09:28 PM

Post a sample where you get the error. I'll go through the specific sample with you. This is a compilation error, by the way.
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 09:28 PM

probably coming from your
public static void main(String[] args)
method that you haven't post here


When you have an error please post it
Was This Post Helpful? 1
  • +
  • -

#6 MaccaTractor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-May 12

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 09:42 PM

Oh ok, I'm working on it now .. Sorry, i shouldn't have deleted it, would've been quicker for everyone ... So does this mean that my methods above are correct? or not? ... Thanks heaps by the way!

Ok here's the main method i've been attempting to run

	public static void main () {
		Subject subject1 = new Subject ("Science", "SCI100");
		Subject subject2 = new Subject ("Maths", "MTH100");
		Subject [] subjectArray = {subject1,subject2};
		System.out.println ("The code SCI100 already exists in the array of subjects:" + codeExists (subjectArray, "SCI100"));

	}



And the error is as follows:

Subject.java:17: error: non-static method codeExists(Subject[],String) cannot be referenced from a static context
System.out.println ("The code SCI100 already exists in the array of subjects:" + codeExists (subjectArray, "SCI100"));
^
1 error

Process completed.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,471
  • Joined: 27-December 08

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 09:43 PM

Your problem is that codeExists() is a non-static method, meaning it must be invoked on a Subject object. However, it looks more like a utility method that should be declared as static instead.
Was This Post Helpful? 1
  • +
  • -

#8 MaccaTractor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-May 12

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 09:50 PM

Yeah i've been looking at other forum posts and after reading a few i thought that by changing codeExists() to a static method it might fix my problem ... But i get another error because the codeExists() method references the getCode() method, which is also a non static method .. So i tried changing that to a static method as well, but it references the code variable which is also non static ... And yeah that's as far as i got
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,471
  • Joined: 27-December 08

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 09:51 PM

Rather than changing getCode() to static (as it refers to the invoking Object, so all the calls to getCode() will return the same thing), invoke getCode() on subjects[i].
Was This Post Helpful? 1
  • +
  • -

#10 MaccaTractor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-May 12

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 10:05 PM

Yes i noticed that by changing everything to static that, i was pretty much rendering the program useless :P

Anyway so by invoking the getCode () method on subjects [i] do you mean something like this?

    public static boolean codeExists ( Subject [] subjects, String subjectCode) {
    	String [] codes = new String [subjects.length];
    	for (int i = 0; i < subjects.length; i++) {
    		subjects [i] = getCode ();
    		if (subjectCode == codes [i])
    		return false;
    	}
    	return true;
    }


Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,471
  • Joined: 27-December 08

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 10:06 PM

No. More like:
codes[i] = subjects[i].getCode();



Notice how I am invoking getCode() on the Subject object.
Was This Post Helpful? 1
  • +
  • -

#12 MaccaTractor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-May 12

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 10:19 PM

YES!! That makes complete sense! ... Even when i was writing it i noticed that it was not referencing the subject object!

Ok so following the same logic i changed the allDisciplines() method to
    public static String [] allDisciplines (Subject [] subjects) {
    	String [] disciplines = new String [subjects.length];
    	for (int i = 0; i < subjects.length; i++) {
    		disciplines [i] = subjects[i].getDiscipline ();
    	}
    	Arrays.sort(disciplines);
    	return disciplines;



And clearly i have made an error because, while it compiled and ran fine, my output was:

The code SCI101 already exists in the array of subjects: true
The disciplines are: [Ljava.lang.String;@1e5e2c3

Process completed.
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,471
  • Joined: 27-December 08

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 10:22 PM

The array does not override the Object toString() method. So instead, you will have to loop through it and print out all the elements. The output you got was its memory location.
Was This Post Helpful? 0
  • +
  • -

#14 MaccaTractor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-May 12

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 10:24 PM

i think i can work this one out myself, give me a sec :)

damn you beat me to it
Was This Post Helpful? 0
  • +
  • -

#15 MaccaTractor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-May 12

Re: New to Programming .. Passing an array of objects into a method?

Posted 19 May 2012 - 11:13 PM

You are a life saver!! That was driving me nuts, thank you soo much!!!

If you're still in the mood to help me out... I'm having a little trouble with my if statement...

    public static String [] codesPerDiscipline (Subject [] subjects, String subjectCode) {
    	String [] codes = new String [subjects.length];
    	ArrayList<String> disciplineCodes = new ArrayList<String>();
    	for (int i = 0; i < subjects.length; i++) {
    		codes [i] = subjects[i].getCode ();
    		if (subjectCode == (codes [i].substring (0,3)));
    			disciplineCodes.add (codes [i].substring (3,6));
    		}
    	String [] disCodes = new String [disciplineCodes.size ()];
    	disciplineCodes.toArray(disCodes);
    	return disCodes;



I want it to return true only if the subjectCode matches the first three digits of each of the codes ... At the moments it is always returning true and i dont know why ...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2