java - Calling methods of an object that is already stored in an ArrayList -
everything works far in program i'm having trouble section of code:
else if(input.equals("2")) { system.out.println("enter stock symbol:"); symbol2 = in.next(); system.out.println("enter number of shares wish 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 loop tempsymbol = "". why doesn't array1 return anything?
here's code. apologies ahead of time if parts 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 enter new stock or press 2 find lifo , fifo dollar cost average number of shares sold"); input = in.next(); if(input.equals("1")) { system.out.println("enter stock symbol:"); symbol = in.nextline(); in.nextline(); system.out.println("enter stock name:"); name = in.nextline(); system.out.println("enter number of shares bought:"); shares = in.nextint(); system.out.println("enter 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 continue entering new shares or press 2 finish input " + thestack.getname()); rolling = in.nextint(); while(rolling == 1) { system.out.println("enter number of shares bought:"); shares = in.nextint(); system.out.println("enter price per share when purchased"); price = in.nextdouble(); thestack.bigpush(shares, price); theq.bigadd(shares, price); system.out.println("press 1 continue entering new shares or press 2 finish input " + thestack.getname()); rolling = in.nextint(); } array1.add(thestack); //i added objects after values finalized array2.add(theq); } else if(input.equals("2")) { system.out.println("enter stock symbol:"); symbol2 = in.next(); system.out.println("enter number of shares wish 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 continue working stocks or press else finish up"); loopcheck = in.next(); } system.out.println("end"); } }
this queue class works 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 stack class 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 example of output:
press 1 enter new stock or press 2 find lifo , fifo dollar cost average number of shares sold 1 enter stock symbol: dave enter stock name: franco enter number of shares bought: 5 enter price per share when purchased 5 press 1 continue entering new shares or press 2 finish input franco 2 press 1 continue working stocks or press else finish 1 press 1 enter new stock or press 2 find lifo , fifo dollar cost average number of shares sold 2 enter stock symbol: dave enter number of shares wish sell: 1 //and nothing here when should return averagecost() press 1 continue working stocks or press else finish
following long code, tt looks boils down wrong usage of scanner class :
while(loopcheck.equals("1")) { system.out.println("press 1 enter new stock or press 2 find lifo , fifo dollar cost average number of shares sold"); input = in.next(); if(input.equals("1")) { system.out.println("enter stock symbol:"); symbol = in.nextline(); // problem here in.nextline(); this assigns empty string symbol, because consumes end of line of previous in.next().
if change :
while(loopcheck.equals("1")) { system.out.println("press 1 enter new stock or press 2 find lifo , fifo dollar cost average number of shares sold"); input = in.next(); in.nextline(); if(input.equals("1")) { system.out.println("enter stock symbol:"); symbol = in.nextline(); it work.
edit :
it looks aware of need call in.nextline() without using returned value, put in.nextline() in wrong place.
Comments
Post a Comment