/* Array6.java CIS 160 David Klick 2004-11-01 Sorting parallel arrays. */ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; public class Array6 { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("people2.txt")); final int MAXSIZE = 100; int numRecs = 0; String[] names = new String[MAXSIZE]; int[] ids = new int[MAXSIZE]; // read records into parallel arrays String s; s = in.readLine(); while (s != null) { if (numRecs < MAXSIZE) { names[numRecs] = processName(s); ids[numRecs] = processID(s); numRecs++; } else { System.out.println("Error: Too many records in file"); break; } s = in.readLine(); } in.close(); // sort arrays based on ids[] for (int i=0; i ids[j]) { int tmp = ids[i]; String stmp = names[i]; ids[i] = ids[j]; names[i] = names[j]; ids[j] = tmp; names[j] = stmp; } } } // display records from arrays for (int i=0; i= 0) { if (sb.charAt(ndx) == '"') sb.deleteCharAt(ndx); ndx--; } return sb.toString().trim(); } public static int processID(String s) { StringTokenizer tok = new StringTokenizer(s, ","); if (tok.countTokens() != 2) { System.out.println("Invalid number of fields in record"); return -1; } return Integer.parseInt(tok.nextToken()); } }