0

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 = "". Why doesn't array1 return anything?

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
4
  • 1
    Stepping through this with a debugger would enable you to find the answer quickly. Commented Jun 19, 2015 at 6:59
  • I edited my initial code with an example of the output if that helps! Commented Jun 19, 2015 at 7:11
  • Well, not really. Stepping through it with a debugger would help. Commented Jun 19, 2015 at 7:12
  • nevermind I figured it out! Commented Jun 19, 2015 at 7:30

1 Answer 1

2

Following your long code, tt looks like it all boils down to a wrong usage of the Scanner class :

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().

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I did this and nothing changed ): I did a lot of trail error with scanner to get why I had to read input correctly without skipping stuff
@MichaelKibler well, there may be other errors I didn't spot. It's a lot of code. I suggest you run it with a debugger to make sure you get the input right.
I figured it out I had to change nextLine to just next for symbol. Thanks though!
@MichaelKibler Well, there are multiple ways to read input correctly. Personally I prefer to always use nextLine (instead of next,nextInt,...). It forces me to parse the input into the correct type (int, double, etc...), but at least I don't have to add extra nextLine calls. Anyway, you're welcome!

Your Answer

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.