Java Help

Discussion in 'School Work Help' started by Woody, Mar 6, 2010.

  1. Woody

    Woody Well-Known Member

    213
    41
    0
    I'm suppose to find the stock symbol, quantity and price.
    The first line is RY, 15, 49.5 (symbol, quantity and price).
    Please help.

    I don't know what method to use.
    Note that the stock can be this: ><Stock symbol="RY"></Stock>
    or this: ><Stock symbol = "RY"> </Stock>






    [HIDE]<?xml version="1.0"?>
    <Portfolio>
    <Investment><Stock symbol="RY"></Stock><Qty>15</Qty><Comment>this is a good one; esp. the BookValue</Comment><BookValue>49.5</BookValue></Investment>
    <Investment><Comment>this is not "bad"</Comment><Stock symbol = "NT"></Stock><Qty>10</Qty> <BookValue>1.85</BookValue></Investment>
    <Investment><Qty>2</Qty><BookValue>37.02</BookValue><Comment>not sure about this; >= last time</Comment><Stock symbol="BMO"> </Stock></Investment>
    </Portfolio>
    [/HIDE]
     
    #1 Woody, Mar 6, 2010
    Last edited: Mar 6, 2010
  2. pretty straight forward. use xpath or domparser or some xml parser

    those will give you the methods you need to get your data. xpath is excellent, but if you want, domparser is very easy to use too.
     
  3. Woody

    Woody Well-Known Member

    213
    41
    0
    I didn't learn these yet. So I did this. lol


    String Symbol = "";
    int Quantity = 0;
    double Price = 0.0;
    for(; fileInput.hasNext(); )
    {
    String line = fileInput.nextLine();
    line = line.replace(" ", "");
    int frontSymbol = (line.indexOf("<Stocksymbol")) ;
    int endSymbol = (line.indexOf("</Stock>")) ;
    int frontQuantity = (line.indexOf("<Qty>")) ;
    int endQuantity = (line.indexOf("</Qty>"));
    int frontPrice = (line.indexOf("<BookValue>"));
    int endPrice = (line.indexOf("</BookValue>"));

    if((frontSymbol>-1)&&(endSymbol>-1)&&(frontQuantity>-1)&&(endQuantity>-1)&&(frontPrice>-1)&&(endPrice>-1))
    {
    Symbol = line.substring(frontSymbol+ 14,endSymbol- 2);
    Quantity = Integer.parseInt(line.substring(frontQuantity+ 5, endQuantity));
    Price = Double. parseDouble(line.substring(frontPrice+ 11, endPrice));
    output.println(Symbol + " ------------" + Quantity + "---------- " + Price);
    }
    }


    I only learn how to use String class, can you think of another way using the String class. My code prints out the symbol, quantity, price with delay. lol
     
    #3 Woody, Mar 6, 2010
    Last edited: Mar 6, 2010
  4. wait, did your prof/teacher allow you to use external methods? or is your assignment to CREATE a parser for that xml?
     
  5. Woody

    Woody Well-Known Member

    213
    41
    0

    I don't even know what xml is. The book says we will learn it later. I just have to treat it as normal text. So hard.
     
  6. oh.


    xml stands for extended markup language, the text that you have there, with all the "<" and ">", is a form of language, best to use when serializing data. what you have there, is xml. (note "<?xml version="1.0"?>" in your text)

    why is your teacher telling you to parse the xml without teaching it to you >.>

    ugh. i hate profs who do that. stupid profs
     
  7. anyways you do realize that this isn't really a programming forum, and we're just lending a hand where we can. i don't think there are a lot of programmers in PA.

    you'd have better chance in a java forum.
     
  8. Woody

    Woody Well-Known Member

    213
    41
    0

    lol ok. I'm learning "aggregations", and it's super confusing. Assignment is also due.