محاولة الحصول على كل سطر من ملف نصي ليكون قائمة ArrayList

StackOverflow https://stackoverflow.com/questions/3846922

سؤال

حسنًا ، أحاول قراءة ملف نصي. ثم قم بتقسيمه إلى خطوط تمثل بالفعل عمليات في قائمة ArrayList. ثم أريد تقسيم جميع الرموز في الخط وجعل جميع الرموز المميزة للخط قائمة arraylist مرة أخرى حتى الآن هذا هو كل ما لدي:

لا يعمل بشكل صحيح. تلقيت:

processes:[[netscape, 1, 00ACF3, 20990, 12DFFE, 000F00, 000000, 000000, 000000, AF,   011356, 000000, 000000, 4FFFFF, 39AB00, 000000, 0A0B92, FFFFFF]]
processes:[[netscape, 1, 00ACF3, 20990, 12DFFE, 000F00, 000000, 000000, 000000, AF, 011356, 000000, 000000, 4FFFFF, 39AB00, 000000, 0A0B92, FFFFFF, textpad, 2, 391BCA, 871BAF, DEA14C, EEFC30, 000000, 000000, 0000AA, AF, 000000, 000000, 000000, 000000, 000000, FFFFFF, B4344D, 000000], [netscape, 1, 00ACF3, 20990, 12DFFE, 000F00, 000000, 000000, 000000, AF, 011356, 000000, 000000, 4FFFFF, 39AB00, 000000, 0A0B92, FFFFFF, textpad, 2, 391BCA, 871BAF, DEA14C, EEFC30, 000000, 000000, 0000AA, AF, 000000, 000000, 000000, 000000, 000000, FFFFFF, B4344D, 000000]]

BufferedReader inputStream = null;
ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();
ArrayList<String> tokens = new ArrayList<String>();
/* read the file*/
try {
    inputStream = new BufferedReader(new FileReader("p56.txt"));

    while (true) {
        /* while the file was read*/
        /* now. split the file into the lines.*/
        String line = inputStream.readLine();
        if (line == null) {
            break;
        }
        //if there are no more lines left. break

        // split the lines into tokens and make into an arraylist*/
        Scanner tokenize = new Scanner(line);
        while (tokenize.hasNext()) {
            /*while there are still more*/
            tokens.add(tokenize.next());
        }
        lines.add(tokens);

        System.out.println("processes:" + lines);
    }
}
هل كانت مفيدة؟

المحلول

تحتاج إلى تحريك الخط

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

إلى اليمين من قبل

while (tokenize.hasNext()) {

سيقوم بعد ذلك بإنشاء قائمة جديدة من الرموز قبل معالجة كل سطر. وإلا فإنك ستنتهي بقائمة من جميع الرموز لجميع خطوط الملف.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top