2015-04-28 63 views
1

我有一些像這樣的代碼:不能從浮轉換爲int

void drawPlot() 
{ 
    String[] dataItemPrices = loadStrings("itemPrices.csv"); //load in .csv file, store as string array 
    itemName = new String[dataItemPrices.length-1]; 
    itemPrice = new float[dataItemPrices.length-1]; 

    for (int i=0; i<dataItemPrices.length-1; i++) 
    { 
     //split array to consider commas 
     String[] tokensItemPrices = dataItemPrices[i+1].split(","); 
     itemName[i] = tokensItemPrices[0]; 
     itemPrice[i] = Float.parseFloat(tokensItemPrices[1]); 

     dataMin = min(dataMin,itemPrice[i]); 
     dataMax = max(dataMax,itemPrice[i]); 

     itemPriceScaled = new float[map(itemPrice[i], dataMin, dataMax, 
             0, (height-100))]; 
    } 
} 

的最後一行代碼導致的問題,我編譯時得到「不能從浮到int轉換」。

唯一的int是[i]但是用來訪問數組,它不可能是這樣嗎?否則,我該如何訪問數組?

請幫忙!

謝謝!

+1

什麼是map方法? –

+0

你能在編譯時看到實際的錯誤行嗎? – Blip

+0

你可以發佈完整的'drawPlot()'方法嗎?你顯示的是缺少重要的聲明,比如'itemPriceScaled'和'map()'的聲明。 – QuantumMechanic

回答

1

有幾種可能的選擇:

  1. 您正在創建一個新的int數組,你需要一個int的大小呢,確實map返回浮點?如果是這種情況,map可能需要重大修改。

  2. map需要什麼樣的參數? itemPrice [i],dataMin和dataMax都是浮動的(高度也可以),這是正確的嗎?在這種情況下,強制轉換爲(INT)可能足以或你的函數原型需要固定...

請爲map和代碼中所有變量聲明添加到您的問題。

編輯:

後您的評論,看着你的代碼,也許這是你想做的事:填充使用map具有明顯的價格數組(從ITEMPRICE陣列拍攝)「縮放」與其他兩個陣列的大小相同:

void drawPlot() 
{ 
    String[] dataItemPrices = loadStrings("itemPrices.csv"); //load in .csv file, store as string array 
    itemName = new String[dataItemPrices.length-1]; 
    itemPrice = new float[dataItemPrices.length-1]; 
    itemPriceScaled = new float[dataItemPrices.length-1]; // <<<<<Added 

    for (int i=0; i<dataItemPrices.length-1; i++) 
    { 
     //split array to consider commas 
     String[] tokensItemPrices = dataItemPrices[i+1].split(","); 
     itemName[i] = tokensItemPrices[0]; 
     itemPrice[i] = Float.parseFloat(tokensItemPrices[1]); 

     dataMin = min(dataMin,itemPrice[i]); 
     dataMax = max(dataMax,itemPrice[i]); 

     itemPriceScaled[i] = map(itemPrice[i], dataMin, dataMax, 
             0, (height-100)); //<<<Modified 
    } 
} 
+0

的地圖的方法是在這裏: https://processing.org/reference/map_.html 地圖(值,啓動1,停止1,START2,停止2 )。參數\t 值\t浮動:要被轉換進入的值 啓動1 \t浮動:下界的值的電流範圍 STOP1 \t浮子:上限的值的電流範圍的 START2 \t浮動:下界的值的目標範圍 的stop2 \t float:值的目標範圍的上限 返回\t float – superquack

+0

它返回一個浮點數,你不能用它來給數組的大小(例如一個數組有1.5個元素??),你確定這是什麼你想要做什麼?我在編輯我的答案。 –

+0

user43250937我很抱歉我的問題措辭不好。我看到你做了什麼,這真的很聰明,錯誤消失了。但是,由於某些原因,地圖功能沒有達到我的預期。我試圖解決這個問題,itemPrice [0]是11.29,所以在0到600範圍內(高度是700,原始範圍是0.6到58.4),itemPriceScaled [0]應該在117左右。相反,我得到NaN的。對於itemPrice [1] = 39.63,itemPriceScaled [1]是600.我再次感到困惑= /我吮吸。 – superquack