/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code4decf18pracfinal; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code4DecF18PracFinal { /** * @param args the command line arguments */ public static void main(String[] args) { /* ABC airport has a list of travelers kept in a file and has asked you to write a complete program that reads from the user a filename and a destination and prints all the travelers (Format: --Last name, First name) that travelled to that destination. 0 points if you code everything in main. You should have at least 3 methods: main, another method and the readFile method). You must write a method: public static String[][] readFile(String filename) It returns a String[][] with as many rows as lines in the file, and each row will have as many Strings as comma-separated items on that line in the file. For the sample file below, the String[][] will look like this: [ ["Alex Aiono","Johanesburg","8-6-2017"], ["Marcus Schultz","Marrakech","1-24-2017"], ….. ] Sample data in file: Alex Aiono,Johanesburg,8-6-2017 Marcus Schultz,Marrakech,1-24-2017 Output Welcome to ABC airport! Enter the filename: travelers.txt Enter the destination: Marrakech List of travelers: --Schultz, Marcus 1/24/17 --Verwest, Tijs Good bye. */ String[][] tr; String filename = ""; String dest = ""; Scanner input = new Scanner(System.in); System.out.println("Welcome to ABC airport!"); System.out.print("Enter the filename: "); filename = input.nextLine(); System.out.print("Enter the destination: "); dest = input.nextLine(); tr = readFile(filename); // search for destination System.out.println("List of travelers:"); for (int d = 0; d < tr.length; d++) { if (tr[d][1].equalsIgnoreCase(dest)) { printTraveler(tr[d]); } } System.out.println("Good bye."); } /* --Schultz, Marcus 1/24/17 --Verwest, Tijs 9/3/17 */ public static void printTraveler(String[] trRow) { System.out.print("--"); // "Marcus Schultz" ---> Schultz, Marcus String[] names = new String[3]; names = trRow[0].split(" "); // Can't have "de los Santos" as one name System.out.print(names[1]+", "+names[0]); // 1-24-2017 --> 1/24/17 names = trRow[2].split("-"); System.out.print(" "+names[0]+"/"+names[1]+"/"+names[2].substring(2)+"\n"); } public static String[][] readFile(String filename) { //filename = "src/code4decf18pracfinal/travelers.txt"; Scanner travelData = new Scanner(System.in); try { travelData = new Scanner(new File(filename)); } catch (FileNotFoundException fnfe) { System.out.println("No input file"); } int count = 0; while (travelData.hasNextLine()) { travelData.nextLine(); count++; } travelData.close(); try { travelData = new Scanner(new File(filename)); } catch (FileNotFoundException fnfe) { System.out.println("No input file"); } /* Alex Aiono,Johanesburg,8-6-2017 Marcus Schultz,Marrakech,1-24-2017 */ String[][] temp = new String[count][3]; count--; int index = 0; while (travelData.hasNextLine() && index <= count) { travelData.useDelimiter(","); temp[index][0] = travelData.next(); //travelData.next(); // comma temp[index][1] = travelData.next(); travelData.reset(); //travelData.next(); // comma temp[index][2] = travelData.next().substring(1); //System.out.println("Line "+index+" *"+temp[index][0]+"* "+temp[index][1]+" "+temp[index][2]); index++; } return temp; } }