我有2个Java类,一个是Main(duh),另一个是我阻止或逐步运行代码的地方。
主要
public class W_CS230_Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
String content = new String();
int count=1;
File file2 = new File("/Users/kilown/Downloads/contributors.csv");
LinkedList<String> list = new LinkedList<String>();
try {
Scanner sc = new Scanner(new FileInputStream(file2));
while (sc.hasNextLine()){
content = sc.nextLine();
list.add(content);
}
sc.close();
}catch(FileNotFoundException fnf){
fnf.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
System.out.println("\nProgram terminated Safely...");
}
Collections.reverse(list);
Iterator i = list.iterator();
while (i.hasNext()) {
System.out.print( (count++) );
System.out.println(i.next());
System.out.println("\n");
}
Scanner in = new Scanner (System.in);
int choice;
System.out.println("\n");
System.out.println("Welcome User: ");
System.out.println("Please select your option: ");
System.out.println("1: Display current list of Contributors: ");
System.out.println("2: Enter new Contributor: ");
System.out.println("3: Save File: ");
System.out.println("4: NDY Pop: ");
System.out.println("5: NDY (Not Developed Yet)");
System.out.println("6: NDY (Not Developed Yet)");
System.out.println("7: Exit: ");
System.out.println("\n");
// System.out.println("4: show stats");
choice = in.nextInt();
while (choice !=7){
if (choice == 1){
//Read CSV file
System.out.println("The current list of Contributors: ");
//function from other class
readFile.readFile();
System.out.println("\n");
E2.selection();
in.nextLine();
choice = in.nextInt();
}
if (choice == 2){
// Create stack and input new member into csv file
System.out.println("Choice 2");
//function from other class
E2.userInfo();
System.out.println("\n");
E2.selection();
in.nextLine();
choice = in.nextInt();
}
if (choice == 3){
System.out.println("choice 3");
//function from other class
E2.writeFile();
System.out.println("\n");
E2.selection();
in.nextLine();
choice = in.nextInt();
}
if (choice == 4){
System.out.println("Choice 4");
//function from other class
System.out.println("\n");
E2.selection();
in.nextLine();
choice = in.nextInt();
}
if (choice == 5){
System.out.println("choice 5");
//function from other class
System.out.println("\n");
E2.selection();
in.nextLine();
choice = in.nextInt();
}
if (choice == 6){
System.out.println("Choice 6");
//function from other class
System.out.println("\n");
E2.selection();
in.nextLine();
choice = in.nextInt();
}
// Exit statement for selection 7
//System.out.println("W_CS230_Main.java just ran");
System.out.println("Safely ended application.");
in.close();
}
}
}
//
走出去
public class E2{
// Public Class works Do not change info
public static void readFile(){
//Read.readFile();
//choice = in.nextInt();
//in.nextLine();
String fileName = "/Users/kilown/Downloads/contributors.csv";
File file = new File(fileName);
try {
Scanner inputStream = new Scanner(file);
while (inputStream.hasNext()){
String data = inputStream.next();
System.out.println(data + "\n");
}
inputStream.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(readFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void selection(){
Scanner in = new Scanner (System.in);
int choice;
System.out.println("Welcome User: ");
System.out.println("Please select your option:");
System.out.println("1: input new contributor ");
System.out.println("2: read selected file");
System.out.println("3: Save File");
System.out.println("4: Pop");
System.out.println("5: Exit");
// System.out.println("4: show stats");
choice = in.nextInt();
}
public static void writeFile() throws IOException{
File contList = new File("Contributor.csv");
FileWriter fileWriter = new FileWriter(contList, true);
BufferedWriter buffer = new BufferedWriter(fileWriter);
PrintWriter printWriter = new PrintWriter(buffer);
if (contList.exists() == false){
contList.createNewFile();
System.out.println("file created");
}else {
System.out.println("file already exist");
}
printWriter.println("scanner information");
printWriter.close();
}
public static void userInfo(){
String name = JOptionPane.showInputDialog("Please enter first and last name: ");
String city = JOptionPane.showInputDialog("Please enter "+name+ " city: ");
String country = JOptionPane.showInputDialog("Please enter "+name+" country: ");
System.out.print(name);
System.out.print(city);
System.out.print(country);
}
public static void sortStack(){
public Stack<Integer> sort(Stack<Integer> a) {
a.sort(new Comparator<Integer>());
public int compare(Integer contID1, Integer ContID2){
return contID2.compare(contID1);
}
});
return a;
}
}
}
我喜欢做的是从E2.userInput调用let''name',并将它放在Main的输出中。
我需要在Main中看到userInput中的所有这些项目。
接下来我需要有关堆栈排序的帮助,我没有为contID做过rand但是在创建用户时会这样做。我喜欢在贡献者创建之后发生的那种(进入堆栈或链表 - 但又不确定哪一个是最好的)。
提前致谢(是的,这是针对大学课程的)