Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java - Arraylist/passing Parameter/return With Tostring

string array

  • Please log in to reply
12 replies to this topic

#1 klueless

klueless

    CC Lurker

  • New Member
  • Pip
  • 7 posts
  • Programming Language:C
  • Learning:C

Posted 15 May 2012 - 08:54 PM

Ive looked on google on how to do this, none that ive tried work. The case study: I have two classes, one is a test class and one is the actual programming class. in the actual programming class I am supposed to:
- receive a passed arrayList, sort it, and return it with only the sorted 3 first letters oh the string that ive put in the arrayList.

Here is what Ive done: (the test class, user input class)
System.out.println("Please enter the subject code: ");
String mySubjectCode = JOptionPane.showInputDialog("Enter subject code", "ITC100");
System.out.println("Please enter the subject name: ");
String mySubjectName = JOptionPane.showInputDialog("Enter subject name", "Java Programming");
System.out.println("");

System.out.println("Entered subject: " + objectSubject.toString(mySubjectCode, mySubjectName));
System.out.println("The Dicipline is: " + objectSubject.getDicipline(mySubjectCode));
System.out.println("Matching Code: " + objectSubject.codeMatches(mySubjectCode));
System.out.println("");
					  
myList.add(new Subject(mySubjectCode, mySubjectName));

whis is my other class, with the programming stuff

public class Subject {
	private String subjectName;
	private String subjectCode;
	private ArrayList myTempList = new ArrayList();
	String myMatcher = "[A-Z]{3}[0-9]{3}";
	Subject(String mySN, String mySC)
	{
		mySN = subjectName;
		mySC = subjectCode;
	}
	Subject()
	{
	}
	public String getSubjectName()
	{
		return subjectName;
	}
	public String getSubjectCode()
	{
		return subjectCode;
	}
   public String getDicipline(String a)
   {
	   return a.substring(0,3);
   }
   public boolean codeMatches(String a)
   {
	   return a.substring(0,6).matches(myMatcher);
   }
   public String toString(String a, String <img src='http://forum.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' />
   {
	   return a + " " + b;
   }
   public String allDiciplines(ArrayList <String> allSubject)
   {
	   Collections.sort(allSubject);
	   for(int i = 0; i < myTempList.size(); i++)
			{
				return System.out.println(myTempList.get(i).toString(subjectCode, subjectName));
			}
   }

I was wondering if anyone could help me with the passing arrayList and point me whats wrong with my syntax? Thank you!
  • 0

#2 klueless

klueless

    CC Lurker

  • New Member
  • Pip
  • 7 posts
  • Programming Language:C
  • Learning:C

Posted 16 May 2012 - 01:47 AM

i forgot to mention the case study:

I was supposed to make 2 different class, one is a subject class and another is TestSubject.
in subject class, i am suppose to do all the programming side where test subject is user input.

User input: 2 strings, subject code and subject name, and stored in an arrayList.

the subject class has a function of:
- getSubjectName
- getSubjectCode
- getDicipline
- codeMatch
- toString
- all dicipline

where subjectCode has to be like(ABC000) first three are letters and three numbers. where the subject name can be anything, more than one words is also acceptable.
The part that I am stuck is the storing the user inputs into the arraylist, then using the arraylist to be passed into a function in the subject class that does sorting, and returning only the first 3 letters of whatever stored in the arraylist such as:
ABC
BBA
ITC
ZTH

while the substring part and the storing part is not too hard, i am stuck in the printing of the arraylist part (last post, very very bottom). Does anyone know anything wrong with my syntax?any help is appreciated.
  • 0

#3 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 16 May 2012 - 02:40 AM

You shouldn't be returning System.out.println() there.
  • 0

#4 klueless

klueless

    CC Lurker

  • New Member
  • Pip
  • 7 posts
  • Programming Language:C
  • Learning:C

Posted 16 May 2012 - 03:32 AM

sorry, this is my first programming class, i suck at it :P
we havent done alot of arrayLists in class. But do you have any tips on what i might need to fix it?
  • 0

#5 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 16 May 2012 - 03:36 AM

for(int i = 0; i < myTempList.size(); i++) {
return System.out.println(myTempList.get(i).toString(subjectCode, subjectName));
}


Does the method has to return a String?
  • 0

#6 klueless

klueless

    CC Lurker

  • New Member
  • Pip
  • 7 posts
  • Programming Language:C
  • Learning:C

Posted 16 May 2012 - 03:52 AM

I guess i need to, i cant think of any other types of return, but ill attach what Ive done under this post.
Removing the return still gives me a syntax error, i figured i could do the return outside the for loop, but if i do so, wont it return only one result when i need to return many results?

Attached Files


  • 0

#7 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 16 May 2012 - 03:54 AM

Just return nothing (void)
  • 0

#8 klueless

klueless

    CC Lurker

  • New Member
  • Pip
  • 7 posts
  • Programming Language:C
  • Learning:C

Posted 16 May 2012 - 04:12 AM

Im guessing there is something else is wrong with what I did, the quick version of what I did is:

Class A
user input 1
user input 2

put them both inside an arraylist.

static ArrayList myList = new ArrayList();
System.out.println("Please enter the subject code: ");
					    String mySubjectCode = JOptionPane.showInputDialog("Enter subject code", "ITC100");
					    System.out.println("Please enter the subject name: ");
					    String mySubjectName = JOptionPane.showInputDialog("Enter subject name", "Java Programming");
myList.add(new Subject(mySubjectCode, mySubjectName)); 

Class B
has another arraylist, which is sorta like a mirror so that B can work like A
process it to the expected result, however it wont due to syntax error.

private ArrayList <TestSubject> myTempList = null;
public void allDiciplines(ArrayList <String> allSubject)
   {
	   String a ="";
	   Collections.sort(allSubject);
	   for(int i = 0; i < myTempList.size(); i++)
		    {
			    ///System.out.println(myTempList.get(i).toString(subjectCode, subjectName));
			   
			    //return a = myTempList.get(i).substring(0,3 );
			    [color=#ff0000]System.out.println(myTempList.get(i).toString(subjectCode, subjectName));[/color]
		    }
	  
   }

The red line says:
method toString in class.java.lang.Object cannot be applied to given types;
required: no arguments
found: java.lang.String
Reason: actual and formal argument lists differ in length
  • 0

#9 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 16 May 2012 - 04:19 AM

Well, like the error says:
This method does not exist:
toString(subjectCode, subjectName)
So either make it exist (write a toString method with those 2 parameters), or else drop both parameters and then the default toString() method will run. (default toString will give you the address in the memory).

In my opinion: write this:
System.out.println( myTempList.get(i) );
Which is the same as:
System.out.println(myTempList.get(i).toString() );
Java will automatically call toString() to turn an object into a String.

Then navigate to your TestSubject class and create this method:
@Override
public String toString(){
    return subjectCode + subjectName;
}

  • 0

#10 klueless

klueless

    CC Lurker

  • New Member
  • Pip
  • 7 posts
  • Programming Language:C
  • Learning:C

Posted 16 May 2012 - 05:04 AM

The codes give no errors at all, thank you very much! however i did some other errors somewhere, the program still crashes im still very confused with arrayList and how to make both class aware of one single arraylist, when i run the program it does:

Exception in thread "main" java.lang.NullPointerException
at subject.Subject.allDiciplines(Subject.java:73)
at subject.TestSubject.main(TestSubject.java:102)
Java Result: 1

where subject.java:73 is ---> for(int i = 0; i < myTempList.size(); i++)
and TestSubject.main(TestSubject.java:102) is --->objectSubject.allDiciplines(myList);

is it the arraylist that dont connect ?
  • 0

#11 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 16 May 2012 - 05:09 AM

You can't call any methods on objects that are null:

private ArrayList <TestSubject> myTempList = null

It is fine to declare it as null. But don't forget to create a new ArrayList ( myTempList = new ArrayList<TestSubject>(); ) before trying to use the variable, and maybe put something in it as well.
  • 0

#12 klueless

klueless

    CC Lurker

  • New Member
  • Pip
  • 7 posts
  • Programming Language:C
  • Learning:C

Posted 16 May 2012 - 11:37 PM

the toString method is created, everything else is fixed however with the array - i worked around it and found this:
the arrayList Subject stores object, and i want the method to return an arraylist of <String>, and only taking the first three letters of whatever there is in the object arrayList.

public void allDiciplines(/*ArrayList <String> myListTwo*/)
   {
	   //String a ="";
	   //Collections.sort(myListTwo);
	  
	    ArrayList<String> myString = new ArrayList<String>();
	   for(int i = 0; i < myListTwo.size(); i++)
		    {
			    ///System.out.println(myTempList.get(i).toString(subjectCode, subjectName));
			   
			    //return a = myTempList.get(i).substring(0,3 );
			    //System.out.println(myListTwo.get(i).toString());
			    myString.add(myListTwo.get(i).[color=#ff0000]substring[/color](0,3));    //substring word is redlined, and im suspecting there is another error somewhere, not in where the substring is.
			    //System.out.println(myString.add(myListTwo.get(i).substring(0,3)));
			    //System.out.println()
		    }
	   Collections.sort(myString);
	   for(int x = 0; x < myString.size(); x++)
	   {
		   System.out.println(myString.get(x));
	   }
	   //return myListTwo;
	  
   }

my syntax error now is in the substring word (marked at code), any tips on how to fix this?
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download