0

So i am trying to add items to an arraylist, However when I try to write the add method the IDE gives me an error that I'm passing "name, cusip and ticker wrong. Can someone explain to me what I am doing wrong here? Thank you in advance.

Here is my ETF class

package fundProject;
import java.util.Scanner;
public class ETF extends AbstractETF {
 private String name;
 private int cusip;
 private int ticker;
 public ETF(String name, int cusip, int ticker) {
 this.name = getName();
 this.cusip = getCusip();
 this.ticker = getTicker();
 }
 public int getCusip() {
 System.out.println("Please enter the Cusip of the ETF");
 Scanner sc = new Scanner(System.in);
 cusip = sc.nextInt();
 return cusip;
 }
 public int getTicker() {
 System.out.println("Please enter the Ticker of the ETF");
 Scanner sc = new Scanner(System.in);
 ticker = sc.nextInt();
 return ticker;
 }
 public String getName() {
 System.out.println("Please enter the Name of the ETF");
 Scanner sc = new Scanner(System.in);
 name = sc.next();
 return name;
 }
}

And here is my main class

package fundProject;
import java.util.ArrayList;
public class mainClass {
 public static void main(String[] args) {
 ArrayList<ETF> etfArrayList = new ArrayList<ETF>();
 etfArrayList.add(new ETF(name, cusip, ticker));
 }
 }
asked Apr 16, 2015 at 17:05
3
  • 1
    Where are name, cusip, and ticker declared? Commented Apr 16, 2015 at 17:07
  • 1
    ^^^ @RobertMoskal is right, you need those values declared, and they need to BE something, they need a value or they are useless. You didnt think this through mate, research and attempt to debug before you post a question. Just a solid tip. After reading through the answers, i suggest you delete this question as it does not help the community and should not have been posted in the first place. Commented Apr 16, 2015 at 17:10
  • 1
    Your ETF class is very confusing. First it calls overrideable method from a constructor. Then, whenever anyone tries to use the accessors for its variables, the thing tries to read a new value from a Scanner! I would seriously rethink your design. Also always close your resources. Commented Apr 16, 2015 at 17:10

2 Answers 2

1

First, you haven't defined the variables name, cusip, and ticker in the mainClass class, so the compiler generates an error here.

However, you don't even use those 3 parameters in your ETF constructor.

I would do one of the following things:

  • Eliminate the parameters to the constructor, and don't pass anything into the constructor.
  • Or, move the code to ask the user for input to main, so you can pass those variables into the constructor. The constructor would simply copy the values.
answered Apr 16, 2015 at 17:08

Comments

1

It's because you're not defining what exactly name cusip and ticker are. You should declare them somewhere first.

Example:

public static void main(String[] args) {
 ArrayList<ETF> etfArrayList = new ArrayList<ETF>();
 String name = "John Doe";
 int cusip = 1;
 int ticker = 1;
 etfArrayList.add(new ETF(name, cusip, ticker));
 }
}

You also need to rewrite your constructor for those arguments to be accepted:

public ETF(String name, int cusip, int ticker) {
 this.name = name;
 this.cusip = cusip;
 this.ticker = ticker;
}

Overall your ETF class could use another look-over. It's not easy to understand.

answered Apr 16, 2015 at 17:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.