views:

38

answers:

1

----What I'm doing---- So I have an assignment that takes an input of characters then calculates the capital gain for the string using a queue.

I'm stripping the input data then putting it into an arraylist that I then queue. I'm suppose to then dequeue the data to calculate the the capital gain.

----The problem----

The problem I'm having is accessing the commands I created in the Shares class after I've put the arraylist into the queue. I've tried to remove an element from the queue and it seems to have lost it's shares class. Any ideas what I've been missing?

-----The input:-----

“buy 100 share(s) at $20 each;buy 20 share(s) at $24 each;buy 200 share(s) at $36 each;sell 150 share(s) at $30 each;buy 50 share(s) at $25 each;sell 200 share(s) at $35 each;”

-------GainCalc.java-------

package Package1;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;


public class GainCalc {

public static void main(String[] args)
{


//User input is put into a String[]

Scanner sc = new Scanner(System.in);

String stocks = sc.nextLine();

stocks = stocks.trim().substring(1, stocks.length() - 1);

String s[]=new String[100];

Queue stockQ = new LinkedList();

ArrayList <Shares> shareList = new ArrayList<Shares>();


//Input string is stripped of extra chars and put into an ArrayList

for(int i=0;i<s.length;i++)

s = stocks.split(";");



for (String stock : s)
{
if (stock.contains("buy"))
{

stock = stock.substring(3, stock.length() - 5);

stock = stock.replaceFirst(" ", "+,");

stock = stock.replaceFirst("s", ",");

stock = stock.replaceAll("[^+,0-9]", "");


String[] tokens = stock.split(",");

stockQ.add(new Shares(tokens[0],Double.valueOf(tokens[1]),Double.valueOf(tokens[2])));
}


if (stock.contains("sell"))
{

stock = stock.substring(4, stock.length() - 5);

stock = stock.replaceFirst(" ", "-,");

stock = stock.replaceFirst("s", ",");

stock = stock.replaceAll("[^-,0-9]", "");


String[] tokens = stock.split(",");

stockQ.add(new Shares(tokens[0],Double.valueOf(tokens[1]),Double.valueOf(tokens[2])));
}
}



while(!stockQ.isEmpty())
{
addStock = stockQ.remove();
}

while(!stockQ.isEmpty())
{
System.out.println(stockQ.remove());
}


//close the output file

sc.close();


}
}

---------NEW FILE---------- -----------------shares.java-----------------

package Package1;


public class Shares
{

//private instance variables

private String command;
private double shares;
private double value;


/**
* Score constructor
* @param uCommand the command of the score
* @param uShares the shares of the score
* @param uScoreValue the scoreValue of the score
*/


public Shares(String uCommand, double uShares, double uValue)

// public Shares(double uShares, double uValue)

{
command = uCommand;
shares = uShares;
value = uValue;
}


/**
* Gets the command of the score
* @return the command
*/

public String getCommand()
{
return command;
}



/**
* Gets the shares of the score
* @return the shares

*/

public double getShares()
{
return shares;
}



/**
* Gets the scoreValue of the score
* @return the scoreValue
*/

public double getValue()
{
return value;
}


/**
* returns the String description
* @return the String description of the class
*/

@Override
public String toString()
{
return command + " " + (int) shares + " " + (int) value ;

}
}
A: 

You talk about "putting the ArrayList into the Queue" but your code does no such thing. Your ArrayList (shareList) is never used at all. Now look at this code:

while(!stockQ.isEmpty())
{
    addStock = stockQ.remove();
}

while(!stockQ.isEmpty())
{
    System.out.println(stockQ.remove());
}

You're effectively emptying the queue, and doing nothing significant with each item: you're just assigning the reference to a variable which is never read later on. So after the first block, the queue is empty. You're then trying to print out the contents of the queue - but it's empty, so nothing is printed.

Jon Skeet
I know I defined the arrayList and planned on using it but have not. I thought that was the route I should be taking but I'm not sure. My issue is being able to remove from the queue and still use my methods defined in the Shares class.
James