I have a program that takes a list of names from a file and puts it in a List. Then the user can do what they want from it using the methods provided. I am wondering how I would go about putting a second file in there so the user can choose to upload a list of names or a list of integers. I have created the File I just can’t seem to put it in…Here is my code:
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class MyProgram7{
public static void main(String[] args)throws Exception{
//Declare Variables
File file = new File ("data7_names_Fall_2011.txt");
File file2 = new File ("data7_integers_Fall_2011.txt");
Scanner input = new Scanner(file);
Prog7Methods pm = new Prog7Methods();
Scanner scan = new Scanner(System.in);
String menu, outputString;
int option = 1;
MyList<String> list = new MyLinkedList<String>();
while (input.hasNext()){
list.add(input.next());
}
//Create menu
menu ="\n\t1 Create list of names." +
"\n\t2 Display list of names." +
"\n\t3 Search for element in list." +
"\n\t4 Check if list is empty." +
"\n\t5 Check the size of list." +
"\n\t6 Remove item from list." +
"\n\t7 Clear the list." +
"\n\t0 Quit\n\n\n";
System.out.println(menu);
System.out.println("Enter your selection:\t");
option = scan.nextInt();
//Continue menu
while (option != 0) {
switch (option){
case 1: //Reading
pm.createLists(scan, input);
break;
case 2: pm.displayList(list);
break;
case 3: pm.searchList(scan, list);
break;
case 4: pm.checkList(list);
break;
case 5: pm.checkSize(list);
break;
case 6: pm.remove(scan, list);
break;
case 7: pm.clearList(list);
break;
default: outputString = "\nInvalid Selection\n";
System.out.println(outputString);
break;
}//end switch
System.out.print(menu);
System.out.println("Enter your selection:\t");
option = scan.nextInt();
}
}
}
You can use Scanner class to ask about the type of list that the user wants to handle.
Depending on the user input (int or String), you can load either one of the Lists (from the corresponding file). After that, you can continue as you have listed in your code.
Regards!