Results 1 to 5 of 5
  1. #1
    superblack is offline Member
    Join Date
    Apr 2017
    Posts
    3
    Rep Power
    0

    Default Supplied String to a function is not working

    I am relatively new to java but not to programming languages. I encountered a problem which I could not comprehend why. Here is the code.

    Java Code:
    package stockscreener;
    
    import java.io.IOException;
    import java.math.BigDecimal;
    import java.math.BigInteger;
    import java.math.RoundingMode;
    import java.util.Calendar;
    import java.util.*;  
    import yahoofinance.Stock; 
    import yahoofinance.YahooFinance;
    import yahoofinance.histquotes.HistoricalQuote;
    import java.io.BufferedReader;
    import java.io.FileReader;
    
    public class StockScreener {
    
        //location of the ticker file
        private static final String FILENAME = "C:\\Users\\USER\\Desktop\\Analysis.txt";
    
        
        public static void run(String s){
            System.out.println(s); // for debugging // s is printing correctly
            //it prints F
            // tried using s.trim(), but no success.
            Stock stock1 = new Stock(s); // when enter literal "F", it works
            //Stock constructor takes String as it's parameter.
            //stock1 become a null value when taken s as the input. But when I enter "F", it works.
       
            // more codes here
            
        }
        public static void main(String[] args) {
    
            BufferedReader br = null;
            
            try{
                br = new BufferedReader(new FileReader(FILENAME));
                
                String sCurrentLine;
                
                while ((sCurrentLine = br.readLine()) != null) {
                    System.out.println(sCurrentLine);
                    run(sCurrentLine);
                    //assume there is a stock name "F" in the file
                }
            }
            catch (IOException e){            
                e.printStackTrace();
            } 
            finally {
                try {
                    if (br != null)
                    br.close();
                } 
                catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            
        }
        
    }
    The problem occurs as per comment in the code.

    Here is the link to the Stock library.
    Stock (YahooFinanceAPI 3.6.1 API)
    Last edited by superblack; 04-30-2017 at 02:02 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Supplied String to a function is not working

    First, I presume you know that s.trim() doesn't change the string since Strings are immutable. You need to do
    s = s.trim() as it returns the trimmed string.

    Are you certain that the analysis file contains a single F on one line? There may be other things there. You
    might try printing out the length of the input line (e.g. s.length()) and when you print out the String,
    do it like this:

    System.out.println("[" + s + "]");

    That will help show if whitespace is present.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    superblack is offline Member
    Join Date
    Apr 2017
    Posts
    3
    Rep Power
    0

    Default Re: Supplied String to a function is not working

    Quote Originally Posted by jim829 View Post
    First, I presume you know that s.trim() doesn't change the string since Strings are immutable. You need to do
    s = s.trim() as it returns the trimmed string.

    Are you certain that the analysis file contains a single F on one line? There may be other things there. You
    might try printing out the length of the input line (e.g. s.length()) and when you print out the String,
    do it like this:

    System.out.println("[" + s + "]");

    That will help show if whitespace is present.

    Regards,
    Jim
    I am aware to assign the s.trim() to s.
    I have checked the length and everything is fine.

    I just found out that actually the function from the library (Stock) is not working, hence the problem. I have tried disabling that particular function and everything is fine now.

    Anyway, Thank you.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Supplied String to a function is not working

    Quote Originally Posted by superblack View Post
    I just found out that actually the function from the library (Stock) is not working, hence the problem. I have tried disabling that particular function and everything is fine now.
    Curious though that it worked when you supplied the stock name but not when you read it from a file. That would indicate it was something else (unless
    it was all just a coincidence).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    superblack is offline Member
    Join Date
    Apr 2017
    Posts
    3
    Rep Power
    0

    Default Re: Supplied String to a function is not working

    Quote Originally Posted by jim829 View Post
    Curious though that it worked when you supplied the stock name but not when you read it from a file. That would indicate it was something else (unless
    it was all just a coincidence).

    Regards,
    Jim
    I am not exactly sure myself. I think I have mistaken something. Now it is working as intended.

Similar Threads

  1. JSP EL function is not working for var arg method.
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-28-2013, 01:12 PM
  2. String trim() member function not working
    By shall in forum New To Java
    Replies: 2
    Last Post: 04-29-2012, 07:28 AM
  3. Replies: 7
    Last Post: 04-28-2012, 02:10 AM
  4. The function append() not working.
    By gautham0209 in forum AWT / Swing
    Replies: 4
    Last Post: 11-20-2011, 07:32 AM
  5. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •