将元素添加到字符串数组列表

时间:2016-07-24 10:05:57

标签: java arrays arraylist bufferedreader

如何将用户输入(元素)添加到字符串数组列表中? Java的。所以当我提示时 - "输入一个问题:",它应该添加到数组列表中。原谅我的代码非常混乱。我花了几天时间在互联网上寻找答案。请帮忙!

public class OIP2 {

        public static void main(String[]args) throws Exception{
        // TODO Auto-generated method stub
        Scanner sc= new Scanner(System.in);
        String input;
        String fileName = "storeOIP.txt";
        java.io.File file = new java.io.File("storeOIP.txt");
            String entry;
            int n;
            int question12 = 0;
            String question1 ;
            String question11;

          try {

          System.out.println(" Organization in project work");
          System.out.println(" ============================");
          Scanner in = new Scanner(System.in);
          ArrayList<String> questions= new ArrayList<String>();


        questions.add(" 1. Qn  :  How to organize your time well when you're juggling"); 
        questions.add(" with so many project work and assignments on the same day?");
        questions.add("");
        questions.add(" Best answer : The best solution to this is to early planning or schedule");
        questions.add(" your time wisely. Write in a calendar beforehand  the work you are going");
        questions.add(" to do for an assignment");
        questions.add("");
        questions.add(" Andy23: Finish your work faster everyday");
        String questionsList[]=questions.toArray(new String[questions.size()]);

        /*Displaying Array elements*/
        for(String k: questionsList){
            System.out.println(k);
        }
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        PrintWriter outputStream = new PrintWriter(fileName);
        System.out.println("");

        System.out.println(" Do you want ask a question related to this category? (Y/N)" );
        char answer1=sc.next().charAt(0);

        if(answer1== 'Y'|| answer1 == 'y'){
            System.out.println("  Enter a question:");//when this prompt appears, it should add to the array list of qns//
            question1 = br.readLine();
            questions.add (question1);
            System.out.println("   Anonymous:" + question1);
            outputStream.println("Anonymous:" + question1); 
            System.out.println("  Your question has been posted."); 
        }

        System.out.println("  Do youwant to reply to any of the posted questions?(Y/N):");
        char answer = sc.next().charAt(0);
        if(answer== 'Y' || answer== 'y'){

            System.out.println("  Which question do you want to add answer?");
            question12= sc.nextInt();
        }

        while(question12 >2){
            System.out.println("  Please enter a valid question number:");
            question12 = sc.nextInt();

            outputStream.println(question12);
        }
        System.out.println("  Add an answer:");
        question11 = br.readLine();
        outputStream.println("Anonymous:" + question11);
        System.out.println("Anonymous:" + question11);
        outputStream.flush();
        outputStream.close();
        System.out.println("  Thank you for your answer!");
    }catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    System.out.println("  Do you want to stay at this page?(Y/N):");
    char ans = sc.next().charAt(0);
    if (ans== 'N' || ans == 'n'){
    }else{
        if (ans=='N'|| ans=='n'){
            System.out.println("  Do you want to still stay at this page?(Y/N):");
            char ans1 = sc.next().charAt(0);
            if (ans1== 'N' || ans1 == 'n'){
                return;
            }
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

public static void main(String[]args) {

    Scanner scanner = new Scanner(System.in);

    ArrayList<String> questions= new ArrayList<String>();

    String input = "";
    while(!input.equals("exit")) {
        System.out.print("Insert a question: ");
        input = scanner.nextLine();
        questions.add(input);    
    }
}