-3

For the past few hours, I've been trying to figure out my problem with this stock viewer application. I had it working perfectly fine yesterday, did some edits today, then attempted to revert it back to its original form. Now, instead of printing all the stock prices from the dates listed in StockClient (January 7, 2016 to January 7, 2017), it starts in February instead. Please help!

STOCK VIEWER CLASS:

import java.util.*;
import java.net.URL;
import java.net.URLConnection;

public class StockViewer 
{
    private ArrayList<String> dates;
    private ArrayList<String> opens;
    private ArrayList<String> highs;
    private ArrayList<String> lows;
    private ArrayList<String> closes;
    private ArrayList<String> volumes;
    private ArrayList<String> adjCloses;
    private String sym;
    private boolean success = false;

public StockViewer(String symbol)
{   
    dates = new ArrayList<String>();
    opens = new ArrayList<String>(); 
    highs = new ArrayList<String>(); 
    lows = new ArrayList<String>(); 
    closes = new ArrayList<String>(); 
    volumes = new ArrayList<String>(); 
    adjCloses = new ArrayList<String>();

    sym = symbol;
}

public void viewStock(GregorianCalendar begin, GregorianCalendar fin)
{
    while(!success)
    {
        String url = "http://chart.finance.yahoo.com/table.csv?s=" + sym + 
                "&a=" + begin.get(Calendar.MONTH) + 
                "&b=" + begin.get(Calendar.DAY_OF_MONTH) + 
                "&c=" + begin.get(Calendar.YEAR) + 
                "&d=" + fin.get(Calendar.MONTH) + 
                "&e=" + fin.get(Calendar.DAY_OF_MONTH) +
                "&f=" + fin.get(Calendar.YEAR) + 
                "&g=d&ignore=.csv";
        try
        {
            URL yhooFin = new URL(url);
            URLConnection data = yhooFin.openConnection();
            Scanner input = new Scanner(data.getInputStream());
            if(input.hasNext()) // skip a line (that's the header)
                input.nextLine();
            // start reading data
            while(input.hasNextLine())
            {
                String line = input.nextLine();
                String[] elements = line.split(",");
                success = true;
                dates.add(elements[0]);
                opens.add(elements[1]);
                highs.add(elements[2]);
                lows.add(elements[3]);
                closes.add(elements[4]);
                volumes.add(elements[5]);
                adjCloses.add(elements[6]);
                for(int count = 0; count < elements.length; count++)
                {
                    if(count < 6)
                        System.out.print(elements[count] + " - ");
                    else
                        System.out.print(elements[count] + "\n");
                }
            }
        }
        catch(Exception e)
        {}
    }
}

public void viewStockCurrent(GregorianCalendar c1, GregorianCalendar c2)
{
    viewStock(c1, c2);
}
}

STOCK CLIENT CLASS:

import java.util.*;

public class StockClient 
{

    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);

        System.out.print("Please enter a stock symbol: ");
        String symbol = scan.next();

        StockViewer stock = new StockViewer(symbol);

        GregorianCalendar start = new GregorianCalendar(2016, 1, 7);
        GregorianCalendar end = new GregorianCalendar(2017, 1, 7);

        stock.viewStock(start, end);
    }

}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 2
    Please take the [Tour](https://stackoverflow.com/tour), read [How To Ask](https://stackoverflow.com/help/how-to-ask) and provide an [MCVE](https://stackoverflow.com/help/mcve). Have you consulted your [Rubber Duck](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? – Hermann Döppes Jan 10 '17 at 00:08
  • 1
    Up-vote @HermannDöppes's comment, especially about the rubber duck. Original poster -- you're supposed to do debugging first *before* posting. If you've done debugging, then you should post the results of these attempts with your question. Please update. – Hovercraft Full Of Eels Jan 10 '17 at 00:10

1 Answers1

0

Month property is zero based, you have wrong value in

GregorianCalendar start = new GregorianCalendar(2016, 1, 7);
GregorianCalendar end = new GregorianCalendar(2017, 1, 7);

See Calendar.MONTH for details.

Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

Roman C
  • 49,761
  • 33
  • 66
  • 176