how do i get my output to come out in chunks that are 5 lett
how do i get my output to come out in chunks that are 5 letters so it would look like XXXXX XXXXX XXXXX XXX for example.... any help would be much appreciated
import java.util.Scanner;
public class gkerby_ROT13Arrays
{
public static Scanner input = new Scanner (System.in);
public static void main ( String args []){
String [] sentences = new String [5];
/*
System.out.println(\"Enter your 5 sentences below: \");
getSentence(sentences);
System.out.println(\"The original text: \");
displayOriginal(sentences);
System.out.println(\"The Line-by-Line ROT13: \");
displayROT13(sentences);
System.out.println(\"The combined: \");
displayCombinedROT13(sentences);
}
*/
getSentence(sentences);
displayOriginal(sentences);
displayROT13(sentences);
displayCombinedROT13(sentences);
}
/*
*
*getSentences
*
*This method allows the user to enter text into each of the elements of the String array that it recieves
*
*@param sentences An array of String[] data
*@return none
*
*/
// public static void getSentence(String[]sentences){
//
// for(int x=0;x<sentences.length;x++){
// sentences[x]=input.nextLine();
//
// }
//}
public static void getSentence(String [] sentences){
System.out.println(\"Enter your 5 sentences below: \");
int y=1;
for(int x=0;x<sentences.length;x++){
System.out.printf(\"Sentence %d> \",y++);
sentences[x]=input.nextLine();
}
}
/*
*
*displayOriginal
*
*This method displays all of the elements of the array of String data it recieves, line by lune (element by element)
*
*@param sentences An array of String[] data
*@return none
*
*/
// public static void displayOriginal(String[]sentences){
//
// for(int b=0;b<sentences.length;b++){
// System.out.println(sentences[b]);
// }
// }
public static void displayOriginal(String[]sentences){
System.out.print(\"\ The original Text:\ \");
for(int x=0;x<sentences.length;x++){
System.out.println(sentences[x]);
}
}
/*
*
*charConvert
*
*This method will take one char value as a parameter and convert it to its approriate ROT13 equivalent. The retunr value will be the new ROT13 char equivalent.
*
*This method will not do any output
*
*@param ch A character to convert as a char
*@retunr The new ROT13 equivalent as a char
*
*/
// public static char charConvert(char alphabet){
// char charmander;
/* int length = 0;
for (int i=0; i< sentence.length(); i++){
*/
// char alphabet=sentence.charAt[i];
//char letter = values[i];
// alphabet = sentence.charAt(i);
public static char charConvert(char ch){
char res;
switch(ch){
case \'a\':
case \'A\':
res=\'N\';
break;
case \'b\':
case \'B\':
// System.out.printf(\"O\");
// length++; // length++;
res=\'O\';
break;
case \'c\':
case \'C\':
// System.out.printf(\"P\");
// length++; // length++;
res=\'P\';
break;
case \'d\':
case \'D\':
// System.out.printf(\"Q\");
// length++; // length++;
res=\'Q\';
break;
case \'e\':
case \'E\':
// System.out.printf(\"R\");
// length++; // length++;
res=\'R\';
break;
case \'f\':
case \'F\':
// System.out.printf(\"S\");
// length++; // length++;
res=\'S\';
break;
case \'g\':
case \'G\':
// System.out.printf(\"T\");
// length++; // length++;
res=\'T\';
break;
case \'h\':
case \'H\':
// System.out.printf(\"U\");
// length++; // length++;
res=\'U\';
break;
case \'i\':
case \'I\':
// System.out.printf(\"V\");
// length++; // length++;
res=\'V\';
break;
case \'j\':
case \'J\':
// System.out.printf(\"W\");
// length++; // length++;
res=\'W\';
break;
case \'k\':
case \'K\':
// System.out.printf(\"X\");
// length++; // length++;
res=\'X\';
break;
case \'l\':
case \'L\':
// System.out.printf(\"Y\");
// length++; // length++;
res=\'Y\';
break;
case \'m\':
case \'M\':
// System.out.printf(\"Z\");
// length++; // length++;
res=\'Z\';
break;
case \'n\':
case \'N\':
// System.out.printf(\"A\");
// length++;
res=\'A\';
break;
case\'o\':
case\'O\':
// length++;
res=\'B\';
break;
case\'p\':
case\'P\':
// length++;
res=\'C\';
break;
case\'q\':
case\'Q\':
// length++;
res=\'D\';
break;
case\'r\':
case\'R\':
// length++;
res=\'E\';
break;
case\'s\':
case\'S\':
// length++;
res=\'F\';
break;
case\'t\':
case\'T\':
// length++;
res=\'G\';
break;
case\'u\':
case\'U\':
// length++;
res=\'H\';
break;
case\'v\':
case\'V\':
// length++;
res=\'I\';
break;
case\'w\':
case\'W\':
// length++;
res=\'J\';
break;
case\'x\':
case\'X\':
// length++;
res=\'K\';
break;
case\'y\':
case\'Y\':
// length++;
res=\'L\';
break;
case\'z\':
case\'Z\':
// length++;
res=\'M\';
break;
default:
// length++;
res=\' \';
return ch;
}
return res;
}
/*
*
*convertSentence
*
*This method will do the actual conversion of a String of data to its ROT13 equivalent in 5-character chunks of data. It should call on the charConvert() method to do the actual character conversion for each individual character conversion should not happen within this method.
*
*This method will not do any output
*
*@param sentences A String variable to convert
*@return the 5-characters in a group ROT13 result as a String
*
*/
public static String convertSentence(String sentences){
// String newsentence = \"\";
// int other = 0;
// for(int c = 0;c<sentences.length();c++){
// if (other % 5 == 0 && other != 0){
// newsentence += \' \';
// }
// if (charConvert(sentences.charAt(c)) != \' \'){
// newsentence+=charConvert(sentences.charAt(c));
// other++;
// }
// }
// return sentences;
// }
String converted = \"\";
for(int x=0;x<sentences.length();x++){
converted+=charConvert(sentences.charAt(x));
}
return converted;
}
/*
*
*displayROT13
*
*This method will display data in ROt13 fomrat all of the elements of the array of String data that it recieves. It will need to call on the method convertSentence() to convert each String before it displays it. Note that the original array should not be modified with ROT13 data
*
*@param sentences An array of String[] data
*@retunr none
*
*/
public static void displayROT13(String[]sentences){
// for(int d=0;d<sentences.length;d++){
// System.out.printf(convertSentence(sentences[d]));
// }
// }
System.out.print(\"\ The line-by-line ROT13: \ \");
for(int x=0;x<sentences.length;x++){
System.out.println(convertSentence(sentences[x]));
}
}
/*
*
*displayCombinedROT13
*
*This method takes an array of String data and combines all of the Strings into a single String that is then processed by the convertSentences() method. The method should essentially display the same results as the displayROT13() method, except that there won\'t be separate lines of output but rather one large result instead.
*
*@param sentences An array of String[] data
*@return none
*
*/
public static void displayCombinedROT13(String[]sentences){
// String charmander=\"\";
// int number = 0;
// for(int e = 0;e<sentences.length;e++){
// charmander+=convertSentence(sentences[e]);
// }//
// for(int i = 0; i < charmander.length(); i++){
// if(charmander.charAt(i) != \' \'){
// System.out.print(charmander.charAt(i));
// number++;
// }
// if(number % 5 == 0 && number != 0){
// System.out.print(\' \');
// }
// System.out.println();
// }
// }
// }
// }
//}
String res=\"\";
System.out.print(\"\ The combined ROT13: \ \");
for(int x=0;x<sentences.length;x++){
res+=sentences[x];
res+=\"\";
}
System.out.print(convertSentence(res));
System.out.println(\"\");
}
}
Solution
Please follow the code and comments for description :
CODE :
import java.util.Arrays; // required imports
import java.util.Scanner;
public class gkerby_ROT13Arrays { // class to run the code
public static Scanner input = new Scanner(System.in); // scanner class to get the data
public static void main(String args[]) { // driver method
String[] sentences = new String[5]; // local variables
/*
System.out.println(\"Enter your 5 sentences below: \");
getSentence(sentences);
System.out.println(\"The original text: \");
displayOriginal(sentences);
System.out.println(\"The Line-by-Line ROT13: \");
displayROT13(sentences);
System.out.println(\"The combined: \");
displayCombinedROT13(sentences);
}
*/
getSentence(sentences); // method declarations
displayOriginal(sentences);
displayROT13(sentences);
displayCombinedROT13(sentences);
}
/*
*
*getSentences
*
*This method allows the user to enter text into each of the elements of the String array that it recieves
*
*@param sentences An array of String[] data
*@return none
*
*/
// public static void getSentence(String[]sentences){
//
// for(int x=0;x<sentences.length;x++){
// sentences[x]=input.nextLine();
//
// }
//}
public static void getSentence(String[] sentences) { // method to get the data from the user
System.out.println(\"Enter your 5 sentences below: \");
int y = 1;
for (int x = 0; x < sentences.length; x++) {
System.out.printf(\"Sentence %d> \", y++);
sentences[x] = input.nextLine();
}
}
/*
*
*displayOriginal
*
*This method displays all of the elements of the array of String data it recieves, line by lune (element by element)
*
*@param sentences An array of String[] data
*@return none
*
*/
// public static void displayOriginal(String[]sentences){
//
// for(int b=0;b<sentences.length;b++){
// System.out.println(sentences[b]);
// }
// }
public static void displayOriginal(String[] sentences) { // method to display the original data
System.out.print(\"\ The original Text:\ \");
for (int x = 0; x < sentences.length; x++) {
System.out.println(sentences[x]);
}
}
/*
*
*charConvert
*
*This method will take one char value as a parameter and convert it to its approriate ROT13 equivalent. The retunr value will be the new ROT13 char equivalent.
*
*This method will not do any output
*
*@param ch A character to convert as a char
*@retunr The new ROT13 equivalent as a char
*
*/
// public static char charConvert(char alphabet){
// char charmander;
/* int length = 0;
for (int i=0; i< sentence.length(); i++){
*/
// char alphabet=sentence.charAt[i];
//char letter = values[i];
// alphabet = sentence.charAt(i);
public static char charConvert(char ch) {
char res;
switch (ch) {
case \'a\':
case \'A\':
res = \'N\';
break;
case \'b\':
case \'B\':
// System.out.printf(\"O\");
// length++; // length++;
res = \'O\';
break;
case \'c\':
case \'C\':
// System.out.printf(\"P\");
// length++; // length++;
res = \'P\';
break;
case \'d\':
case \'D\':
// System.out.printf(\"Q\");
// length++; // length++;
res = \'Q\';
break;
case \'e\':
case \'E\':
// System.out.printf(\"R\");
// length++; // length++;
res = \'R\';
break;
case \'f\':
case \'F\':
// System.out.printf(\"S\");
// length++; // length++;
res = \'S\';
break;
case \'g\':
case \'G\':
// System.out.printf(\"T\");
// length++; // length++;
res = \'T\';
break;
case \'h\':
case \'H\':
// System.out.printf(\"U\");
// length++; // length++;
res = \'U\';
break;
case \'i\':
case \'I\':
// System.out.printf(\"V\");
// length++; // length++;
res = \'V\';
break;
case \'j\':
case \'J\':
// System.out.printf(\"W\");
// length++; // length++;
res = \'W\';
break;
case \'k\':
case \'K\':
// System.out.printf(\"X\");
// length++; // length++;
res = \'X\';
break;
case \'l\':
case \'L\':
// System.out.printf(\"Y\");
// length++; // length++;
res = \'Y\';
break;
case \'m\':
case \'M\':
// System.out.printf(\"Z\");
// length++; // length++;
res = \'Z\';
break;
case \'n\':
case \'N\':
// System.out.printf(\"A\");
// length++;
res = \'A\';
break;
case \'o\':
case \'O\':
// length++;
res = \'B\';
break;
case \'p\':
case \'P\':
// length++;
res = \'C\';
break;
case \'q\':
case \'Q\':
// length++;
res = \'D\';
break;
case \'r\':
case \'R\':
// length++;
res = \'E\';
break;
case \'s\':
case \'S\':
// length++;
res = \'F\';
break;
case \'t\':
case \'T\':
// length++;
res = \'G\';
break;
case \'u\':
case \'U\':
// length++;
res = \'H\';
break;
case \'v\':
case \'V\':
// length++;
res = \'I\';
break;
case \'w\':
case \'W\':
// length++;
res = \'J\';
break;
case \'x\':
case \'X\':
// length++;
res = \'K\';
break;
case \'y\':
case \'Y\':
// length++;
res = \'L\';
break;
case \'z\':
case \'Z\':
// length++;
res = \'M\';
break;
default:
// length++;
res = \' \';
return ch;
}
return res;
}
/*
*
*convertSentence
*
*This method will do the actual conversion of a String of data to its ROT13 equivalent in 5-character chunks of data. It should call on the charConvert() method to do the actual character conversion for each individual character conversion should not happen within this method.
*
*This method will not do any output
*
*@param sentences A String variable to convert
*@return the 5-characters in a group ROT13 result as a String
*
*/
public static String convertSentence(String sentences) {
// String newsentence = \"\";
// int other = 0;
// for(int c = 0;c<sentences.length();c++){
// if (other % 5 == 0 && other != 0){
// newsentence += \' \';
// }
// if (charConvert(sentences.charAt(c)) != \' \'){
// newsentence+=charConvert(sentences.charAt(c));
// other++;
// }
// }
// return sentences;
// }
String converted = \"\";
for (int x = 0; x < sentences.length(); x++) {
converted += charConvert(sentences.charAt(x));
}
return converted;
}
/*
*
*displayROT13
*
*This method will display data in ROt13 fomrat all of the elements of the array of String data that it recieves. It will need to call on the method convertSentence() to convert each String before it displays it. Note that the original array should not be modified with ROT13 data
*
*@param sentences An array of String[] data
*@retunr none
*
*/
public static void displayROT13(String[] sentences) {
// for(int d=0;d<sentences.length;d++){
// System.out.printf(convertSentence(sentences[d]));
// }
// }
System.out.print(\"\ The line-by-line ROT13: \ \");
for (int x = 0; x < sentences.length; x++) {
System.out.println(convertSentence(sentences[x]));
}
}
/*
*
*displayCombinedROT13
*
*This method takes an array of String data and combines all of the Strings into a single String that is then processed by the convertSentences() method. The method should essentially display the same results as the displayROT13() method, except that there won\'t be separate lines of output but rather one large result instead.
*
*@param sentences An array of String[] data
*@return none
*
*/
public static void displayCombinedROT13(String[] sentences) {
// String charmander=\"\";
// int number = 0;
// for(int e = 0;e<sentences.length;e++){
// charmander+=convertSentence(sentences[e]);
// }//
// for(int i = 0; i < charmander.length(); i++){
// if(charmander.charAt(i) != \' \'){
// System.out.print(charmander.charAt(i));
// number++;
// }
// if(number % 5 == 0 && number != 0){
// System.out.print(\' \');
// }
// System.out.println();
// }
// }
// }
// }
//}
String res = \"\";
System.out.print(\"\ The combined ROT13: \ \");
for (int x = 0; x < sentences.length; x++) {
res += sentences[x];
res += \"\";
}
String result = convertSentence(res); // save the result to a string variable
result = result.replaceAll(\"\\\\s+\", \"\"); // this is the line if the whitespaces are to be eradicated else could be commented to get the original sequence of data
int count = 0; // local variable
char[] array = result.toCharArray(); // convert to array data
for (int len = 0; len < array.length; len++) { // iterate over the data
count++; // increment the count
if (count % 5 == 0) { // check for the chunk size
System.out.print(array[len] + \" \"); // print the space
} else {
System.out.print(array[len]); // else print the data
}
}
System.out.println(\"\"); // new line character
}
}
OUTPUT :
Enter your 5 sentences below:
Sentence 1> hi
Sentence 2> how are you
Sentence 3> iam fine
Sentence 4> iam coming home
Sentence 5> this weekend
The original Text:
hi
how are you
iam fine
iam coming home
this weekend
The line-by-line ROT13:
UV
UBJ NER LBH
VNZ SVAR
VNZ PBZVAT UBZR
GUVF JRRXRAQ
The combined ROT13:
UVUBJ NERLB HVNZS VARVN ZPBZV ATUBZ RGUVF JRRXR AQ
Hope this is helpful.
















