我试图在控制台中每行打印80个字符。我可以每行打印80个字符,但是当字符串中剩余的字符少于80个时,就不会打印它们。我该如何补救?

public void output(String plainText, String cipherText, String iv, String key, String filename) {
        System.out.println("CBC Vigenere by ");
        System.out.println("Plaintext file name: " + filename);
        System.out.println("Vigenere Keyword: " + key);
        System.out.println("Initialization vector: " + iv + "\n");
        //This is the code for printing 80 char per line
        System.out.println("Clear Text: \n");
        int j = 0;
        String output = "";
        for (int i = 0; i < plainText.length(); i++) {
            j++;
            output = output + plainText.charAt(i);
            if (j == 80) {
                System.out.println(output);
                output = "";
                j=0;
            }else{
               //an attempt to print the remaining characters
               // System.out.println(output);
            }


        }
        //same process as above.
        System.out.println(" \n Cipher Text:\n ");
        j = 0;
        output = "";
        for (int i = 0; i < cipherText.length(); i++) {
            j++;
            output = output + cipherText.charAt(i);
            if (j == 80) {
                System.out.println(output);
                output = "";
                j = 0;

            } else if ((cipherText.length() - i) <= 80) {
                if (j == 80) {
                    if (j == (cipherText.length() - i)) {
                        //if (j == 80) {
                        System.out.println(output);
                        output = "";
                        j = 0;
                        System.out.println((output));

                        j = 0;
                    }
                }
            }
        }
        System.out.println("\n\nNumber of characters in clean plaintext file:  " + (plainText.length() - padding));
        System.out.println("Block Size: " + key.length());
        System.out.println("Number of pad Characters added: " + padding);

    }


输出:

 Clear Text:

csthesciencethatdealswiththetheoryandmethodsofprocessinginformationindigitalcomp
utersthedesignofcomputerhardwareandsoftwareandtheapplicationsofcomputersitthedev
elopmentimplementationandmaintenanceofcomputerhardwareandsoftwaresystemstoorgani
zeandcommunicateinformationelectronicallyabbreviationitcomputersaremanmadetoolst
hataidusinsolvingotherproblemsabiologististryingtofigureouthowlifeworksphysicist
sandchemistsaretryingtofigureouthowitemsreactinouruniversemathematiciansaretryin
gtofigureoutrulesformanmadesystemsanyresearchproblemthatmayimproveacomputerscapa
bilityofhelpingsolveaproblemoranyresearchproblemthatshedslightaboutanewwaytodoso
methingwithacomputerispartofcsmostexcitingresearchmedicalapplicationsexpertsyste
msfordiagnosisremotesurgerynanodeviceswithcomputingpowertodelivermedicineetcwene
edhelptryingtocreateacomprehensiveemraccessibletotherightpeopleonlycarsthatcandr
ivethemselvesseemslikethebestwayweknowhowtosolvelotsofproblemsisbythrowinglotsof
computingpowerattheminsteadoflookingforelegantsolutionsthisdoesntsoundexcitingbu
titwillbeexcitingwhentheresultsareachievediewatsoncsstudentstendtofindjobswheret
heyprogramatleastsomeintheprocesstheyaresolvingproblemschallengesitsimpossibleto
teachallthenewlanguagestoysultimatelywejustneedtoteachourstudentshowtothinksotha
ttheycanpickupnewthingsontheirownourbiggestchallengeisgettingthemtobuyintothatet

 Cipher Text:

qiqxbxknvieuejlkvwcpbtquevshswdsbyitsndorkbkwxswngygxwrkgfsbqvxwmsgiatpheilbuxnu
ovzuyvmhwtvjiedrmnfkglrkfsddglexlsvsqfqjxiiqcbcqnyjvblktkjfeeaqklehghsfsutuiftqw
bpwmpyjzytumfgqbqjqzluspcjjzdaovvqeczpilbicjnibqzbcjavhqrqmdmhtgegjgjscscoxnkvel
kxrsbcwwpyjoswsihdtzrtyunqhczoipglabjjbcgkotmfovvwkseieszmvovataazltsolwviveqxpy
dgdosztziuhqztoobkbzmyixdgvsosvbkxctpjokdkdjizcncjozbbznupuepihytkwguytvhbgrphoi
nqhbqqbkqkqkxjikgqvlimdzsntmeifjtfwvwhphntudutoppoqjfmqlpxifiutoappzehyhfgqhondi
qjlwakbjmfngliduchxgqokirtcththfafhamzxzihgruoskadlxlmsnlvqljihejqqaluhwhifioqky
sokekkfxtjlgabnbiukgrdpzxsimdlkgpfaflnjxxuvqdjodosvtuiftemtahbokjmjudggknqnmxiwe
pqpfearegfundjwgxhigxgtorpmcvcoynzyehuhkbyzkywfvokhwrmauscozgppxpnkijunryqyqdqdc
wciygjvrimdmsmqyjujgiigwrvwqdlywjzyjjjmzmovkohpiedramexdxzhlzbbnzhkszcoxzjztcfwr
hcvonlnksamomkvjgrgdnnihegpvaqzgbsmmkuteeedulnworpfbdrqgqdbzxsiochhroqnrbzbjiesd
hgshgtztkmmylywkpkhaymybjkukuobuegvbqfebhcccxvzhrdrdmqipdyvkokdklqlfjbmrgfybybto
ckvhwildyateedleltwnlikjvrfnrgmcdsxgjchozamigjffxhxxssrtfjeuzgybybtdnoythjhzbnjp
jfjjycqlbxfhuijdwhkteusgqspuhvgdeplzzpksvdkpnxnxisrsqtreczczclwwwsiictspapdeodze
nqsnqjdikcrdjpqpqpljwzqaemalvvgvfwtiyqwcvynbcwptnpvigcfmtulnwijuikikxetasahjzvzr
zdgnuvmomdxyicakuyvgishtczbbtciwpxaqfuqlrlrgcsmthuscbrwtutuvehyixxsbsxsmhhyhtihq
hrvdanhidymqnnhzkayqseralbznomjqdmelmbzpkpkzyhqabvohdhqtqufpsqgczdumvsknkjkyrzca


我缺少字符串中的最后20个字符。添加它们的实际方法是什么?字符串大小会有所不同,因此其大小并不总是相同。

最佳答案

如果output循环完成后for不为空,请打印它。

07-25 21:26
查看更多