Results 1 to 4 of 4
Thread: Problem with ragged array
- 09-15-2010, 03:40 AM #1
Problem with ragged array
The problem I'm having is that I need to read the data from a file into a ragged array based on country region number. I have it in an array and was attempting to just read from the array but i keep getting an array index out of bounds exception. It's a small program so I'll post all of it.
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package project1cquast; import java.io.*; /** * * @author Casey Quast */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) throws java.io.IOException { FileInputStream fis1 = new FileInputStream("AsiaCountries.Fall2010.txt"); BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1)); String inline; int index = 0; Country[] myCountries; String searchKey[] = new String[10]; myCountries = new Country[43]; SearchSort mySort = new SearchSort(); Country[][] myCountries2 = new Country[4][]; myCountries2[0] = new Country[20]; myCountries2[1] = new Country[6]; myCountries2[2] = new Country[7]; myCountries2[3] = new Country[14]; while ((inline = br1.readLine()) != null) { myCountries[index] = new Country(inline); index++; } // end while System.out.println("Country Name Country Capital " + " Country Region Name Region # " + "Country Abbreviation "); System.out.println("~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ " + "~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ " + "~~~~~~~~~~~~~~~~~~~~"); //Formatted printout of myStates array for (int count = 0; count < index; count++) { System.out.println(myCountries[count]); }//end for mySort.bubbleSort(myCountries, 1); System.out.println("Sorted Array by Country Name\nCountry Name " + " Country Capital " + " Country Region Name " + " Region # " + "Country Abbreviation "); System.out.println("~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ " + "~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ " + "~~~~~~~~~~~~~~~~~~~~"); for (int count = 0; count < index; count++) { System.out.println(myCountries[count]); }//end for mySort.bubbleSort(myCountries, 2); FileInputStream fis2 = new FileInputStream("Search.Fall2010.txt"); BufferedReader br2 = new BufferedReader(new InputStreamReader(fis2)); index = 0; while ((inline = br2.readLine()) != null) { searchKey[index] = inline; index++; } // end while for (int i = 0; i < searchKey.length; i++) { int tempResult = mySort.linearSearch(myCountries, searchKey[i]); if (tempResult > -1) { System.out.println(searchKey[i] + " " + mySort.getProbes() + " " + " found"); } else { System.out.println(searchKey[i] + " " + mySort.getProbes() + " " + " not found"); } } System.out.println("\n\n"); for (int i = 0; i < searchKey.length; i++) { int tempResult = mySort.binarySearch(myCountries, searchKey[i], 0, myCountries.length - 1); if (tempResult > -1) { System.out.println(searchKey[i] + " " + mySort.getProbes() + " " + " found"); } else { System.out.println(searchKey[i] + " " + mySort.getProbes() + " " + " not found"); } } for (int i = 0; i < (myCountries.length); i++) { if (myCountries[i].getCountryRegionNum() == 1) { myCountries2[0][i] = myCountries[i]; }//end Region1 if else if (myCountries[i].getCountryRegionNum() == 2) { myCountries2[1][i] = myCountries[i]; }//end Region2 if else if (myCountries[i].getCountryRegionNum() == 3) { Line 129 myCountries2[2][i] = myCountries[i]; }//end Region3 if else if (myCountries[i].getCountryRegionNum() == 4) { myCountries2[3][i] = myCountries[i]; }//end Region4 if }//end for for (int Count = 0; Count < (myCountries2[0].length); Count++) { System.out.print(myCountries2[0][Count]); }//end Reg1 print for (int Count2 = 0; Count2 < (myCountries2[1].length); Count2++) { System.out.print(myCountries2[1][Count2]); }//end Reg2 print for (int Count3 = 0; Count3 < (myCountries2[2].length); Count3++) { System.out.print(myCountries2[2][Count3]); }//end Reg3 print for (int Count4 = 0; Count4 < (myCountries2[3].length); Count4++) { System.out.print(myCountries2[3][Count4]); }//end Reg4 print }//end main() }//end MainJava Code:package project1cquast; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Casey Quast */ public class Country { private String countryName, countryCapital, countryRegion, countryAbbrev; private int countryRegionNum; public static int countryObjCounter; public Country(String countryInput) { countryName = countryInput.substring(0,17).trim(); countryCapital = countryInput.substring(21,40).trim(); countryRegion = countryInput.substring(44,55).trim(); countryRegionNum = Integer.parseInt(countryInput.substring(63,65).trim()); countryAbbrev = countryInput.substring(66,68).trim(); countryObjCounter++; }//end parseData Constructor() public String getCountryName() { return countryName; }//end getCountryName() public String getCountryCapital() { return countryCapital; }//end getCountryCapital() public String getCountryRegion() { return countryRegion; }//end getCountryRegion() public int getCountryRegionNum() { return countryRegionNum; }//end getCountryRegionNum() public String getCountryAbbrev() { return countryAbbrev; }//end getCountryAbbrev() public String toString() { return String.format("%-23s %-30s %-24s %-16d %-4s\n", countryName, countryCapital,countryRegion, countryRegionNum, countryAbbrev); }//end toString() }my input file and search file are as follows:Java Code:package project1cquast; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Casey Quast */ public class SearchSort { private int probes; public void bubbleSort(Country[] myCountries, int value) { int out, in; if (value == 1) { for (out = myCountries.length - 1; out > 1; out--) { for (in = 0; in < out; in++) { if (myCountries[in].getCountryName().compareTo(myCountries[in + 1].getCountryName()) > 0) { swap(in, in + 1, myCountries); } } } } else { for (out = myCountries.length - 1; out > 1; out--) { for (in = 0; in < out; in++) { if (myCountries[in].getCountryAbbrev().compareTo(myCountries[in + 1].getCountryAbbrev()) > 0) { swap(in, in + 1, myCountries); }// if }//end inner for }//end outer for } }//end bubbleSort() private void swap(int one, int two, Country[] myCountries) { Country temp = myCountries[one]; myCountries[one] = myCountries[two]; myCountries[two] = temp; }//end swap() public int linearSearch(Country[] myCountries, String searchKey) { int result = -1; int max = myCountries.length - 1; for (int trav = 0; trav <= max; trav++) { if ((myCountries[trav].getCountryAbbrev()).toUpperCase().compareTo(searchKey.toUpperCase()) == 0) { result = trav; setProbes(trav); } } return result; }//end linearSearch public int binarySearch(Country[] myCountries, String searchKey, int lower, int upper) { int result; int mid = (upper + lower) / 2; if (lower > upper) { result = -1; incrProbes(); } else if ((myCountries[mid].getCountryAbbrev()).toUpperCase().compareTo(searchKey.toUpperCase()) == 0) { result = mid; incrProbes(); } else if ((myCountries[mid].getCountryAbbrev()).toUpperCase().compareTo(searchKey.toUpperCase()) < 0) { result = binarySearch(myCountries, searchKey, (mid + 1), upper); incrProbes(); } else { result = binarySearch(myCountries, searchKey, lower, (mid - 1)); incrProbes(); } return result; }//end binarySearch() public void setProbes(int count) { probes = count; } public int getProbes() { return probes; } public void incrProbes() { probes += 1; } }//end SearchSort
AsiaCountries: (all line up in file)
Afghanistan Kabul South Asia 1 AF
Bangladesh Dacca South Asia 1 BA
Bhutan Thimpu South Asia 1 BH
Brunei Bandar Ser Begawan South Asia 1 BR
Cambodia Phnom Penh South Asia 1 CM
India New Delhi South Asia 1 IN
Indonesia Jakarta South Asia 1 ID
Laos Vientiane South Asia 1 LA
Malaysia Kuala Lumpur South Asia 1 MA
Maldives Male South Asia 1 ML
Myanmar Yangon South Asia 1 BU
Nepal Kathmandu South Asia 1 NE
Pakistan Islamagad South Asia 1 PK
Papus New Guinea Port Moresby South Asia 1 PM
Phillippines Manila South Asia 1 PH
Singapore Singapore South Asia 1 SI
Sri Lanka Colombo South Asia 1 SR
Thailand Bangkok South Asia 1 TH
Vietnam Hanoi South Asia 1 VT
Kazakhstan Astana North Asia 2 KZ
Kyrgyzstan Kishkek North Asia 2 KY
Tajikistan Dushanbe North Asia 2 TA
Turkmenistan Ashgabat North Asia 2 TK
Uzbekistan Tashkent North Asia 2 UZ
China Beijing East Asia 3 CH
Japan Tokyo East Asia 3 JA
North Korea Pyongyang East Asia 3 KR
South Korea Seoul East Asia 3 KR
Mongolia Ulan Bator East Asia 3 MO
Taiwan Taipei East Asia 3 TW
Bahrain Manama West Asia 4 BA
Georgia Tbilisi West Asia 4 GE
Iran Teheran West Asia 4 IR
Iraq Baghdad West Asia 4 IQ
Israel Tel Aviv West Asia 4 IS
Jordan Amman West Asia 4 JO
Kuwait Kuwait City West Asia 4 KU
Lebanon Beirut West Asia 4 LE
Oman Muscat West Asia 4 OM
Qatar Doha West Asia 4 QA
Saudi Arabia Riyadh West Asia 4 SB
U.A.E. Abu Dhahi West Asia 4 UA
Yemen Sanaa West Asia 4 YE
search file:
CH
In
ZZ
LA
LA
PH
MO
OO
OM
ye
the error I get is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 14
at project1cquast.Main.main(Main.java:129)
Java Result: 1
The only thing is that my 2d ragged array size is over the number of elements needed so I don't see why I'm getting the exception.
I marked 129 above because I am not sure how to put the code line numbers in here. Please let me know what you think :)Last edited by Metastar; 09-15-2010 at 04:01 AM.
- 09-15-2010, 04:09 AM #2
can you test if your index will be OOB before using it?i keep getting an array index out of bounds exception
Test the length of: myCountries2[2] before trying to index it
For debugging use println("myCs2=" + Arrays.toString(myCountries2[2])); to show the contents of that array.
- 09-15-2010, 04:20 AM #3
so for the Arrays part what would I use in place of Arrays? Should I create a new array for it? Confused on what goes there lol. The length is 7 though
Sorry for the trivial question, just not making the connection somewhere with the debug code
lol wow nevermind, I totally knew Arrays was in util just had a major brain fart lolLast edited by Metastar; 09-15-2010 at 05:07 AM.
- 09-15-2010, 05:40 AM #4
Similar Threads
-
Array Problem?
By noobgrammer in forum New To JavaReplies: 4Last Post: 06-19-2010, 11:25 PM -
Problem with Array Use
By Mike90 in forum New To JavaReplies: 1Last Post: 06-02-2010, 02:45 PM -
array problem
By jabo in forum New To JavaReplies: 2Last Post: 03-31-2010, 09:54 AM -
array problem
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-05-2008, 02:25 AM -
array problem
By Albert in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 01:13 AM


LinkBack URL
About LinkBacks

Bookmarks