Why is my array returning 0 for BitRate, and ok everywhere else?

时间:2015-07-29 00:07:38

标签: java arrays object output

I'm trying to display the 10 objects in my musicLibrary array. Each object has its own Artist Name, Song Title, Bit Rate, and Total time of the song.

My run is fine everywhere except for the "BitRate = 0k" part. My run gives me BitRate = 0k for every one of the 10 objects under the Array Display. I want the BitRate for each object to be {250, 150, 350, 450, 550, 200, 100, 400, 300, 500}. These values are all within the range I set in class iTunes. In any case, if these values weren't in the set range, I should receive an output of "Bad _ _ input". However, what I get back is "0".

There are two separate sections in my code. The first part is comprised of four objects NOT in an array. To these four, I display them, mutate them, display them, set to default, then display. My run is fine here. (I didn't want to delete this portion in case of possible confusion).

The second part is my array of ten objects. For this part, I only need to display the data of the ten objects in the array. My problem is here, when I display my array and my BitRate data displays as 0. For both parts, I am checking to see whether the values are in the range I set.

On a lesser note, can I delete my else's (marked in my code) for cleanliness' sake?

I'm confused because the code I wrote for my BitRate is essentially the same as the code I wrote for my other instance members (artist name, song title, total time), though with different variable names. I can't seem to figure out what's causing Eclipse to display "0" instead of my values. I'm a beginner at Java, so a simplified explanation would be appreciated. Thanks.

Code:

SELECT
    SubQrySUMPermCashIN.Date
    ,SubQrySUMPermCashIN.PayeeID
    ,SubQrySUMPermCashIN.PermCashIn
    ,SubQrySUMPermCashIN.Year
    ,Sum(tblTest.PermCashIn) AS RunningTotal
FROM
    SubQrySUMPermCashIN
INNER JOIN
    SubQrySUMPermCashIN AS tblTest
ON
    SubQrySUMPermCashIN.Date = tblTest.Date
WHERE
    ((SubQrySUMPermCashIN.Date)>=[tblTest]![Date])
GROUP BY
    SubQrySUMPermCashIN.Date
    ,SubQrySUMPermCashIN.PayeeID
    ,SubQrySUMPermCashIN.PermCashIn
    ,SubQrySUMPermCashIN.Year;

Run:

Initial Display:

Title: / Artist: / Playing Time: 0.0 minutes and 0.0 seconds / BitRate: 0k

Title: / Artist: / Playing Time: 0.0 minutes and 0.0 seconds / BitRate: 0k

Title: / Artist: / Playing Time: 0.0 minutes and 0.0 seconds / BitRate: 0k

Title: Hold Back The River / Artist: James Bay / Playing Time: 4 minutes and 0 seconds / BitRate: 300k

//Assignment says to mutate only one or more members; I left BitRate = 0k here, so BitRate = 0k is expected here

Mutated Display:

Title: Viva La Vida / Artist: Coldplay / Playing Time: 4 minutes and 33 seconds / BitRate: 0k

Title: Come Away With Me / Artist: Norah Jones / Playing Time: 3 minutes and 16 seconds / BitRate: 0k

Title: Beggin For Thread / Artist: Banks / Playing Time: 4 minutes and 16 seconds / BitRate: 0k

Title: Let it Go / Artist: James Bay / Playing Time: 4 minutes and 18 seconds / BitRate: 0k

//Default BitRate = 64k here, is as expected

Default Display:

Title: (undefined) / Artist: (undefined) / Playing Time: 0 minutes and 8 seconds / BitRate: 64k

Title: (undefined) / Artist: (undefined) / Playing Time: 0 minutes and 8 seconds / BitRate: 64k

Title: (undefined) / Artist: (undefined) / Playing Time: 0 minutes and 8 seconds / BitRate: 64k

Title: (undefined) / Artist: (undefined) / Playing Time: 0 minutes and 8 seconds / BitRate: 64k

//My problem occurs in Array Display: all my BitRates = 0k

Array Display:

Title: Superheroes / Artist: The Script / Playing Time: 4 minutes and 50 seconds / BitRate: 0k

Title: Closer / Artist: Tegan and Sara / Playing Time: 3 minutes and 33 seconds / BitRate: 0k

Title: Flaws / Artist: Bastille / Playing Time: 3 minutes and 50 seconds / BitRate: 0k

Title: Let it Be / Artist: The Beatles / Playing Time: 4 minutes and 0 seconds / BitRate: 0k

Title: Take Me to Church / Artist: Hozier / Playing Time: 4 minutes and 3 seconds / BitRate: 0k

Title: Lay Me Down / Artist: Sam Smith / Playing Time: 4 minutes and 10 seconds / BitRate: 0k

Title: Tee Shirt / Artist: Birdy / Playing Time: 2 minutes and 41 seconds / BitRate: 0k

Title: Budapest / Artist: George Ezra / Playing Time: 3 minutes and 36 seconds / BitRate: 0k

Title: Falling / Artist: Haim / Playing Time: 4 minutes and 16 seconds / BitRate: 0k

Title: Mess Is Mine / Artist: Vance Joy / Playing Time: 3 minutes and 46 seconds / BitRate: 0k

2 个答案:

答案 0 :(得分:1)

The problem is in your setKiloBits method:

 function ValidateEmail(email) {
    var expr = /^([\w-\.]+)@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    return expr.test(email);
}

You divide an int by an int, and as the result is less than 1 (320/1000 = .32 etc) it truncates the remainder. Try this:

public boolean setKiloBits(int kiloBits) {
      if (kiloBits > MIN_BITRATE && kiloBits < MAX_BITRATE){
         bitRate = (kiloBits/1000);
         return true;
      }
      return false;
}

However, as you are converting from kilobit to bit, I think you actually meant to multiply by 1000? But thats up to you on the business logic.

Edit: As Alpertmd pointed out, you'd also need to change the definition of bitrate to double if you wished to divide.

答案 1 :(得分:1)

The problem is :

bitRate = (kiloBits/1000)

This is doing integer division which is giving you your answer of 0.

You should probably be declaring bitRate as a double and then doing:

bitRate = (kiloBits/1000.00) .