Reputation: 37
I am trying to print 80 characters per line in the console. I can print 80 characters per line, but when i have less than 80 characters left in the string , they are not printed. How could i remedy this?
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);
}
output:
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
I am missing the last 20 char in the string. What is a practical way to add them in? The string size will vary, so its not always the same size.
Upvotes: 0
Views: 3076
Reputation: 562
Your code is extremely inefficient as you are concatenating so many strings. I suggest to split it into substrings of length 80 and then print them.
final int lineLength = 80;
final int numLines = Math.ceil(plainText.length / lineLength);
String[] lines = new String[num_lines];
int charend;
for(int i=0, charini=0; i < numLines; i++) {
charend = Math.min(charini + 80, plainText.lenght());
lines[i] = plainText.substring(charini,charend);
charini += lineLength;
}
// The print function
for(int i=0; i<lines.length; i++) {
System.out.println(lines[i]);
}
Also I suggest you to split your code into methods, instead of copying it twice.
Upvotes: 0
Reputation:
Try this.
for (int i = 0, size = cipherText.length(); i < size; i += 80)
System.out.println(cipherText.substring(i, Math.min(i + 80, size)));
Upvotes: 0