2010-07-29 44 views
0

我是Stack Overflow的新手,這是我的第一個問題!我正在爲使用Java的學校開展一個項目。第一部分我遇到了麻煩inolves:如何填充正在讀取文本文件的值的「衣衫襤褸」數組?

  1. 閱讀文件的每一行(在我的帖子的末尾列出)一次
  2. 創建一個整數的「破爛」陣列,4由X,其中我的4行將是「區域號碼」(在區域號碼中找到的號碼)列,並填寫每個列的州區號該區域

因此,例如,第1行將保持區域1的狀態數量爲6列,第2行表示區域2導致7列,從而導致「不齊」數組。

我的問題是如何填充,或者什麼是最好的方式來填充我的數組與我的文件的結果讀?我知道如何聲明數組,初始化數組並在數組中創建空間,但是我不確定如何在我的State類中編寫我的方法來填充讀入數組的文件的結果。現在,當我嘗試使用Netbeans編譯此代碼時,出現「越界」錯誤。

這是我的Main和State的代碼。我的輸入文件列其下:

import java.util.*; 
import java.io.*; 

public class Main 
{ 
    public static void main(String[] args) throws IOException 
    { 
     // create new jagged array obj and fill it with some 
     // initial "dummy" values 
     int[][] arrPopulation = 
     { 
      {0,1,2,3,4,5}, 
      {0,1,2,3,4,5,6}, 
      {0,1,2,3,4,5,6,7,8,9}, 
      {0,1,2,3,4,5,6,7,8,9,10} 
     };//end array declaration 

     // read in file States.txt, instantiate BufferedReader object, 
     // set new BufferedReader object to variable @newLine 
     FileReader f = new FileReader("States.txt"); 
     BufferedReader br = new BufferedReader(f); 
     String newLine = br.readLine(); 

     for (int rows = 0; rows < arrPopulation.length; rows++) 
     { 
      for (int col = 0; col < arrPopulation[col].length; col++) { 
       System.out.print(arrPopulation[rows][col] + " "); 
      } 
      // display on new lines; print out in a "table" format 
      System.out.println(); 
     } // end for 

     State newState = new State(newLine); 
     int count = 0; 
     while(newLine != null) 
     { 
      newLine = br.readLine(); 
      System.out.println(newState.getRegionNum()); 
     }// end while 
     br.close();//close stream 
    } // end public static void main 
} // end main 

這是我爲我的國家一流至今:

import java.util.*; 

public class State 
{ 
    private String statePop, stateNum, regionNum; 

    public State(String fileRead) 
    { 
     statePop = fileRead.substring(32,39); 
     regionNum = fileRead.substring(55,fileRead.length()); 
    } // end constructor 

    public int getStatePop() 
    { 
     int population = Integer.parseInt(statePop); 
     return population; 
    } // @method getStatePop end method 

    public int getRegionNum() 
    { 
     int numOfRegion = Integer.parseInt(regionNum); 
     return numOfRegion; 
    }// end getRegionNum 

    public int getAvgPop() 
    { 
     int average = 2+2; 
     return average; 
     // total number of populations 
     // divide number of populations 
    }// @return the average population of states 

    public int getStateTotal() 
    { 
     //initialize static variable 
     int totalPopulation = 0; 
     int stateTotal = this.getStatePop() + totalPopulation; 
     return stateTotal; 
    } // @return stateTotal 

    public String toString() 
    { 
     return statePop + " " + stateNum + " "; 
    } // @method end toString method 
} // end State class 

列(在文件讀取未使用的名稱,只是爲了解釋目的):

國家首都人口縮寫區地區

的樹木包
Washington  Olympia  WA 5689263West   6 
Oregon   Salem   OR 3281974West   6 
Massachusetts Boston   MA 6147132New_England 1 
Connecticut Hartford  CT 3274069New_England 1 
Rhode_Island Providence  RI 988480New_England 1 
New_York  Albany   NY18146200Middle_Atlantic2 
Pennsylvania Harrisburg  PA12001451Middle_Atlantic2 
New_Jersey  Trenton  NJ 8115011Middle_Atlantic2 
Maryland  Annapolis  MD 5134808Middle_Atlantic2 
West_Virginia Charleston  WV 1811156Middle_Atlantic2 
Delaware  Dover   DE 743603Middle_Atlantic2 
Virginia  Richmond  VA 6791345Middle_Atlantic2 
South_Carolina Columbia  SC 3835962South   3 
Tennessee  Nashville  TN 5430621South   3 
Maine   Augusta  ME 1244250New_England 1 
Vermont  Montpelier  VT 588632New_England 1 
New_Hampshire Concord  NH 1185048New_England 1 
Georgia  Atlanta  GA 7642207South   3 
Florida  Tallahassee FL14915980South   3 
Alabama  Montgomery  AL 4351999South   3 
Arkansas  Little_Rock AR 2538303South   3 
Louisiana  Baton_Rouge LA 4368967South   3 
Kentucky  Frankfort  KY 3936499South   3 
Mississippi Jackson  MS 2752092South   3 
North_Carolina Raleigh  NC 7546493South   3 
California  Sacramento  CA32182118West   6 
Idaho   Boise   ID 1228684West   6 
Montana  Helena   MT 880453West   6 
Wyoming  Cheyenne  WY 480907West   6 
Nevada   Carson_City NV 1746898West   6 
Utah   Salt_Lake_City UT 2099758West   6 
Colorado  Denver   CO 3970971West   6 
Alaska   Juno   AK 614010West   6 
Hawaii   Honolulu  HI 1193001West   6 

上午我在這條正確的軌道上?

感謝您提前提供任何幫助,並對長期發帖感到抱歉!

+0

嘿,在那裏,我還沒有讀完你的整個問題,因爲它太長了。我會做的一件事就是減少輸入的大小。它只是讓問題看起來更大,但我不認爲這是真的有必要。 (我發現如果你提出更短的問題,你會得到更多的幫助[更快])。歡迎來到SO! – sixtyfootersdude 2010-07-30 01:09:23

+0

嗨,埃米爾,謝謝你的歡迎! 一般來說,我不會提出這麼長的帖子,但我很偏執,確保所有相關數據都可以提供給SO社區,以幫助我解決問題。我對這種事情很偏執。 感謝您的建議! ;) – carlos 2010-07-30 01:54:52

回答

1

集合包在這裏很有用。

  1. 創建地圖<整數,列表< <整數> >
  2. 當你同時掃描文件的一行,抓住區域數量和人口。
    1. 區域號碼是地圖中的關鍵字嗎?
      1. 否然後創建一個新列表<整數>並將其添加到地圖。現在它是。
    2. 將該人口添加到列表中。
  3. 現在,地圖應該有四個條目。創建外部數組,如:int [] [] array = new int [4] [];
  4. 然後迭代映射中的列表並填充外部數組,如下所示:array [i] = new int [list.size()];
  5. 然後迭代列表並填充內部數組,如下所示:array [i] [j] = list.get(j);
+0

我看了這個問題,立刻想出了大約50行代碼來做這件事,但後來我讀到這些,並感到羞恥,它很簡單,而我沒有想到它。 +1 – 2016-12-07 00:50:09

0

感謝您的快速響應。

我的教授還沒有瀏覽Collections包(查看包中的一些類,我想我們會在下個學期討論這些問題),所以我對Map接口不太熟悉。另外,我認爲他希望我們專門使用陣列,雖然他確實說我們可以使用ArrayList ...

直到我讀到你的文章,我重新寫我的代碼,在另一個嘗試解決問題。我要看看Map界面,但出於好奇,我用下面的代碼關閉了嗎?好像我只需要解決一個線來糾正我的出界失誤......

import java.util.*; 
import java.io.*; 

public class Main 
{ 
    public static void main(String[] args) throws IOException 
    { 
     // read in file States.txt, instantiate BufferedReader object, 
     // set new BufferedReader object to variable @newLine 
     FileReader f = new FileReader("States.txt"); 
     BufferedReader br = new BufferedReader(f); 
     String newLine = br.readLine(); 

     //declare jagged array 
     int[][] arrPopulation; 
     arrPopulation = new int[4][]; 

     //create state object, and get 
     State newState = new State(newLine);  

     //read file 
     int col = 0; 

     arrPopulation[0] = new int[col]; 
     arrPopulation[1] = new int[col]; 
     arrPopulation[2] = new int[col]; 
     arrPopulation[3] = new int[col]; 

     int stateRegion = newState.getRegionNum(); 

     while (newLine != null) 
     { 
      switch (stateRegion) 
      { 
       case 1: 

        arrPopulation[0][col] = newState.getStatePop(); 
        System.out.println("Population added:" + 
          arrPopulation[0][col]);//test if col was created 
        col++; //increment columns 
        break; 
       case 2: 

        arrPopulation[1][col] = newState.getStatePop(); 
        col++; //increment columns 
        break; 
       case 3: 

        arrPopulation[2][col] = newState.getStatePop(); 
        col++; 
        break; 
       case 6: 
        arrPopulation[3][col] = newState.getStatePop(); 
        System.out.println("Population added:" + 
          arrPopulation[3][col]); 
        col++; //increment columns 
        break; 
      } 
      br.readLine(); 
     }//endwhile 
     br.close();//close stream 
    } // end public static void main 
} // end main 

很抱歉,如果我應該然而,把所有這一切在註釋部分,我打我的字符的限制,couldn不會發布我的代碼。再次感謝你的幫助!

+0

看起來不好。 (1)col = 0所以arrPopulation [0,1,2,3] = new int [0]。該程序需要計算不同陣列的大小。 (2)變量newState,stateRegion在while循環中是不變的(newState = Washington和stateRegion = 6)。您還需要添加代碼來爲從文件中讀取的每一行更新這些變量。 我認爲修復問題1比修復問題2更困難。 – emory 2010-07-30 02:41:34

+0

問題可以幫助你解決這個問題:** 1)**你的界限越界是什麼? (你可以通過閱讀例外來說明)。 ** 2)**您認爲他們的例外情況是由什麼造成的? (這是一個提示,你的數組只有10個東西,你做myarry [50],那麼這將是無效的。請記住,數組從0開始,所以10會超出範圍)。祝你好運 – sixtyfootersdude 2010-07-30 02:43:34

+0

對於#2,你可以改變你的while循環爲「for(String newLine = br.readLine(); newLine!= null; newLine = br.ReadLine())」。然後移動你在for循環中定義newState和stateRegion的部分。 – emory 2010-07-30 02:49:41

相關問題