5 Replies - 12059 Views - Last Post: 03 November 2011 - 04:56 PM Rate Topic: -----

#1 Lusent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-April 11

actual and formal argument lists differ in length ?

Posted 03 November 2011 - 03:19 PM

In this project I'm working on. I am trying to pass a String[] object from one class to another (for processing) and hopefully back again.

I'm trying to find the easiest way to do this, but I keep getting compile errors:
 ----jGRASP exec: javac -g IndexSort.java

IndexSort.java:7: error: method getStringList in class OrderedStringListTypeDemo cannot be applied to given types;
 String[] sa = OrderedStringListTypeDemo.getStringList();
                                        ^
  required: String[]
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error



My IndexSort class is trying to use the String[] sa = OrderedStringListTypeDemo.getStringList(); to grab the "uSet" variable in my OrderedStringListTypeDemo class- but this line is throwing the compile error. If I try this: String[] sa = {"aaav", "dddea", "oobaw", "gddde", "egleeeddbao", "sveqqwt"}; - then it compiles and works fine. I need to be able to get uSet(a String[] object) passed into here so I can sort it and then pass it back... Here is my IndexSort class:
import java.util.*;

 public class IndexSort{

 public static void main(String[] args){

 String[] sa = OrderedStringListTypeDemo.getStringList();

 Arrays.sort(sa, new IndexComparator("ba"));

 System.out.println(Arrays.deepToString(sa)); // JDK 1.5
 }

 static class IndexComparator implements Comparator<String>{
 String str;

 public IndexComparator(String subs){
 str = subs;
 }

 public int compare(String o1, String o2){

 return o1.indexOf(str) - o2.indexOf(str);
 }
 }
 }



OrderedStringTypeDemo contains the object I'm trying to pass, a String[] object named uSet from this method:
public String[] getStringList(String[] uSet)
	{
	return uSet;
	}

uSet = String[] i'm trying to pass..

I'm trying to get this program to sort strings from uSet in ascending order, but I'm not having much luck with it. I've been working on it for quite a while and I'm afraid I might be over-complicating things (or just straight doing it wrong). If anyone could give me some advice, I'd appreciate it. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: actual and formal argument lists differ in length ?

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: actual and formal argument lists differ in length ?

Posted 03 November 2011 - 03:34 PM

If you mean the method to be a getter method, then the definition should look like this:
public String[] getStringList() // with no parameter and this is how you were trying to call it

Was This Post Helpful? 1
  • +
  • -

#3 Lusent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-April 11

Re: actual and formal argument lists differ in length ?

Posted 03 November 2011 - 03:51 PM

it's grabbing uSet from the Main method in OrderedStringListTypeDemo

Right now it's like

OrderedStringListType contains all the overridden list functions (add, remove, indexOf, etc) and handles the strings- it works fine

OrderedStringListTypeDemo is the driver for the program, which interfaces with the methods in OrderedStringListType (add, remove etc like i mentioned earlier)

then IndexSort is the final step of the program- it needs to sort uSet (passed from OrderedStringListTypeDemo getStringList method - the original object which I copied the value of uSet from resides in the main method..

I tried to invoke a copy of the class as an object and then reference the object's method... didnt work either..

IndexSort.java:6: error: cannot find symbol
OrderedStringListTypeDemo myObject = new OrderedListTypeDemo();
                                         ^
  symbol:   class OrderedListTypeDemo
  location: class IndexSort


import java.util.*;

 public class IndexSort{

 public void Sorter(){
 OrderedStringListTypeDemo myObject = new OrderedListTypeDemo();
 String[] sa = myObject.getStringList();

 Arrays.sort(sa, new IndexComparator("ba"));

 System.out.println(Arrays.deepToString(sa)); // JDK 1.5
 }

 static class IndexComparator implements Comparator<String>{
 String str;

 public IndexComparator(String subs){
 str = subs;
 }

 public int compare(String o1, String o2){

 return o1.indexOf(str) - o2.indexOf(str);
 }
 }
 }


-edit- i spelled it wrong

but now it gives the error it did before:
IndexSort.java:7: error: method getStringList in class OrderedStringListTypeDemo cannot be applied to given types;
 String[] sa = myObject.getStringList();
                       ^
  required: String[]
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error



my edited current IndexSort class:

import java.util.*;

 public class IndexSort{

 public void Sorter(){
 OrderedStringListTypeDemo myObject = new OrderedStringListTypeDemo();
 String[] sa = myObject.getStringList();

 Arrays.sort(sa, new IndexComparator("ba"));

 System.out.println(Arrays.deepToString(sa)); // JDK 1.5
 }

 static class IndexComparator implements Comparator<String>{
 String str;

 public IndexComparator(String subs){
 str = subs;
 }

 public int compare(String o1, String o2){

 return o1.indexOf(str) - o2.indexOf(str);
 }
 }
 }

Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: actual and formal argument lists differ in length ?

Posted 03 November 2011 - 03:53 PM

The idea is that you will have two methods, one to pass it from your class to main, and one take the sorted value to your class. They will look like:
public String[] getStringList(){
   return uSet;}

// and one to return the edited one
public void setStringList(String[] uSet){
  this.uSet = uSet;
}
Other wise in your code, you are just passing and getting the samething

This post has been edited by smohd: 03 November 2011 - 03:57 PM

Was This Post Helpful? 1
  • +
  • -

#5 Lusent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-April 11

Re: actual and formal argument lists differ in length ?

Posted 03 November 2011 - 04:04 PM

tried it but it didnt work, here is my full OrderedStringListTypeDemo class.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;
public class OrderedStringListTypeDemo

{//begin definition of OrderedStringListTypeDemo class

   public static void main(String[] args)
   {//begin main method
      
      OrderedStringListType stringList = new OrderedStringListType();//create a list
      String name = new String("PoundCake");//add a string object to the list
      String name1 = new String("Smokie");//add a string object to the list
      String name2 = new String("BooBoo");//add a string object to the list
		String name3 = new String("Willie");
      stringList.add(name);
      stringList.add(name1);
      stringList.add(name2);
		stringList.add(name3);
      System.out.println("Here are the elements in the list:");
		displayList(stringList);
      stringList.remove(0);
      stringList.remove(stringList.size() - 1);
		
		
      System.out.println("\nNow with the first and last removed:");
      displayList(stringList);

      String name4 = new String("Perry");
      String name5 = new String("Lionel");
      String name6 = new String("Robert");
      String name7 = new String("David");
      String name8 = new String("Ashley");
      String name9 = new String("Kayla");
      
      stringList.add(0,name4);
      stringList.add(1,name5);
      stringList.add(2,name6);
      stringList.add(3,name7);
      stringList.add(4,name8);
      stringList.add(5,name9);
      
      System.out.println("\nNow with new elements:");
      displayList(stringList); 
      
      stringList.remove(1);
      stringList.remove(0);
      
      System.out.println("\nNow with the first and second removed:");
      displayList(stringList);

		      
    }
	 

   public static void displayList(OrderedStringListType list)
   {
      for (int index = 0; index < list.size(); index++)
      {
         System.out.println("Index " + index + ": " +
                     list.get(index));
      }
            
   }
	public String[] getStringList()
	{
	return uSet;
	}
}


Doesnt work because I'm not passing uSet into that method with an argument I'm assuming


and here is my IndexSort method:
import java.util.*;

 public class IndexSort{

 public void Sorter(){
 OrderedStringListTypeDemo myObject = new OrderedStringListTypeDemo();
 String[] sa = myObject.getStringList();

 Arrays.sort(sa, new IndexComparator("ba"));

 System.out.println(Arrays.deepToString(sa)); // JDK 1.5
 }

 static class IndexComparator implements Comparator<String>{
 String str;

 public IndexComparator(String subs){
 str = subs;
 }

 public int compare(String o1, String o2){

 return o1.indexOf(str) - o2.indexOf(str);
 }
 }
 }


New error when compiling OrderedStringListDemo since i removed that arg

OrderedStringListTypeDemo.java:69: error: cannot find symbol
	return uSet;
	       ^
  symbol:   variable uSet
  location: class OrderedStringListTypeDemo
1 error

This post has been edited by Lusent: 03 November 2011 - 04:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 Lusent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-April 11

Re: actual and formal argument lists differ in length ?

Posted 03 November 2011 - 04:56 PM

Well i just merged them both together instead of trying to do it all fancy-like and it works now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1