简体   繁体   English

调用已存储在ArrayList中的对象的方法

[英]Calling methods of an object that is already stored in an ArrayList

Everything works so far in my program but I'm having trouble with this section of my code: 到目前为止,一切都可以在我的程序中运行,但是我在代码的这一部分遇到了麻烦:

    else if(input.equals("2")) {
        System.out.println("Enter the stock symbol:");
        symbol2 = in.next();
        System.out.println("Enter the number of shares you wish to sell:");
        sellshares = in.nextInt();
        String tempsymbol = "";
        for(int i=0; i<array1.size(); i++) {
            tempsymbol = (array1.get(i)).getSymbol();
            if(symbol2.equals(tempsymbol)) {
                System.out.println("The dollar cost averaged price per share (LIFO): " + (array1.get(i)).averageCost(sellshares));
                System.out.println("The dollar cost averaged price per share (FIFO): " + (array2.get(i)).averageCost(sellshares));
            }
        }
    }

It'll go through the loop but tempsymbol will always = "". 它会遍历循环,但tempsymbol始终=“”。 Why doesn't array1 return anything? 为什么array1不返回任何东西?

Here's all my code. 这是我所有的代码。 Apologies ahead of time if any parts are redundant or messy. 如果任何部分多余或混乱,请提前道歉。

    import java.util.*;
public class Whoop {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String input = "";
    String symbol = "";
    String name = "";
    int shares = 0;
    double price = 0;
    String symbol2 = "";
    int sellshares = 0;
    int rolling = 0;
    stack theStack = null;
    queue theQ = null;
    String loopcheck = "1";
    ArrayList<stack> array1 = new ArrayList<stack>();
    ArrayList<queue> array2 = new ArrayList<queue>();

while(loopcheck.equals("1")) {
    System.out.println("Press 1 to enter a new stock or press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold");
    input = in.next();

    if(input.equals("1")) {
        System.out.println("Enter the stock symbol:");
        symbol = in.nextLine();
        in.nextLine();
        System.out.println("Enter the stock name:");
        name = in.nextLine(); 
        System.out.println("Enter the number of shares bought:");
        shares = in.nextInt();
        System.out.println("Enter the price per share when purchased");
        price = in.nextDouble();
        theStack = new stack(symbol,name,shares,price);
        theQ = new queue(symbol,name,shares,price);
        System.out.println("Press 1 to continue entering new shares or press 2 to finish input for " + theStack.getName());
        rolling = in.nextInt();
        while(rolling == 1) {
            System.out.println("Enter the number of shares bought:");
            shares = in.nextInt();
            System.out.println("Enter the price per share when purchased");
            price = in.nextDouble();
            theStack.bigPush(shares, price);
            theQ.bigAdd(shares, price);
            System.out.println("Press 1 to continue entering new shares or press 2 to finish input for " + theStack.getName());
            rolling = in.nextInt();
        }
        array1.add(theStack); //I added the objects after all the values were finalized
        array2.add(theQ);
    }

    else if(input.equals("2")) {
        System.out.println("Enter the stock symbol:");
        symbol2 = in.next();
        System.out.println("Enter the number of shares you wish to sell:");
        sellshares = in.nextInt();
        String tempsymbol = "";
        for(int i=0; i<array1.size(); i++) {
            tempsymbol = (array1.get(i)).getSymbol();
            if(symbol2.equals(tempsymbol)) {
                System.out.println("The dollar cost averaged price per share (LIFO): " + (array1.get(i)).averageCost(sellshares));
                System.out.println("The dollar cost averaged price per share (FIFO): " + (array2.get(i)).averageCost(sellshares));
            }
        }
    }
    else {
        System.out.println("Input invalid ):");
        System.exit(0);
    }

    System.out.println("Press 1 to continue working with your stocks or press anything else to finish up");
    loopcheck = in.next(); 
    }
    System.out.println("END");
}

} }

This is my queue class which works perfectly fine. 这是我的队列类,可以很好地工作。

    import java.util.LinkedList;
    public class queue<E> {

private LinkedList<Double> linklist;
private String symbol;
private String name;
private int shares;
private Double price;

public queue(String symbol2, String name2, int shares2, Double price2) { 
    linklist = new LinkedList<Double>();
    shares = shares2;
    price = price2;
    symbol = symbol2;
    name = name2;
    bigAdd(shares, price);
}

   public String getName() {
       return name;
   }

   public String getSymbol() {
       return symbol;
   }

public void add(Double e) {
    linklist.add(e);
}

public Double take() {
    return linklist.poll();
}       

public void bigAdd (int shares2, Double price2) { 
    while(shares2>0) {
        linklist.add(price2);
        shares2--;
    }
}

public double averageCost(int shares2) {
    double average = 0;
    int sizer = 0;
    while(sizer < shares2) {
        average = average + linklist.poll();
        sizer++;
    }
    average = average/shares2;
    return average;
}

And this is my stack class which also works fine. 这是我的堆栈类,它也可以正常工作。

    import java.util.*;
    public class stack { 

   private ArrayList<Double> stackArray = new ArrayList<Double>();
   private int top;
   private String symbol;
   private String name;
   private int shares;      
   private Double price;

   public stack(String symbol2, String name2, int shares2, Double price2) {
      symbol = symbol2;
      name = name2;
      shares=shares2;
      price=price2;
      top = -1;
      bigPush(shares, price);
   }

   public double averageCost(int shares2) {
       double average = 0;
       int sizer = shares2;
       while(sizer > 0) {
            average = average + stackArray.get(top--);
            sizer--;
        }
       average = average/shares2;
       return average;
   }

   public void push(Double value) {
      stackArray.add(++top, value);
   }
   public Double pop() {
      return stackArray.get(top--);
   }

   public String getName() {
       return name;
   }

   public String getSymbol() {
       return symbol;
   }

   public void bigPush(int shares2, Double price2) {
       while(shares2>0) {
            stackArray.add(++top, price2);
            shares2--;
        }
   }

   public static void main(String[] args) {
       stack theStack = new stack("Dave", "Franco", 2,10.0);
       theStack.bigPush(2,20.0);
       System.out.println(theStack.getSymbol());
   }

} }

Also heres an example of my output: 这也是我的输出示例:

    Press 1 to enter a new stock or press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold
    1
    Enter the stock symbol:
    DAVE
    Enter the stock name:
    FRANCO
    Enter the number of shares bought:
    5
    Enter the price per share when purchased
    5
    Press 1 to continue entering new shares or press 2 to finish input for FRANCO
    2
    Press 1 to continue working with your stocks or press anything else to finish up
    1
    Press 1 to enter a new stock or press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold
    2
    Enter the stock symbol:
    DAVE
    Enter the number of shares you wish to sell:
    1
    //AND THEN NOTHING HERE WHEN IT SHOULD RETURN AVERAGECOST()
    Press 1 to continue working with your stocks or press anything else to finish up

Following your long code, tt looks like it all boils down to a wrong usage of the Scanner class : 按照您的长代码,tt看起来全部归结为对Scanner类的错误使用:

while(loopcheck.equals("1")) {
    System.out.println("Press 1 to enter a new stock or press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold");
    input = in.next();

    if(input.equals("1")) {
        System.out.println("Enter the stock symbol:");
        symbol = in.nextLine(); // problem here
        in.nextLine();

this assigns an empty String to symbol , because it consumes the end of line of the previous in.next() . 这将为symbol分配一个空String,因为它消耗了先前in.next()

If you change it to : 如果将其更改为:

while(loopcheck.equals("1")) {
    System.out.println("Press 1 to enter a new stock or press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold");
    input = in.next();
    in.nextLine();
    if(input.equals("1")) {
        System.out.println("Enter the stock symbol:");
        symbol = in.nextLine();

it will work. 它会工作。

Edit : 编辑:

It looks like you are aware of the need to sometimes call in.nextLine() without using its returned value, but you put in.nextLine() in the wrong place. 看起来您知道有时需要使用in.nextLine()而不使用其返回值,但是您将in.nextLine()放在错误的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM