I need several methods to encode and decode my string. I am having trouble getti
ID: 3538084 • Letter: I
Question
I need several methods to encode and decode my string. I am having trouble getting it set up correctly... I only want it to shift by one letter...
import java.io.*;
import java.util.*;
public class endec
{
public static void main(String[] args)
throws java.io.IOException
{
char[] alpha = new char[26];
char[] cipher = new char[26];
Scanner input =new Scanner(System.in);
loadData(alpha);///I want it to load my data first
displayMenu();//display menu
}
public static void loadData(char[] alpha)
{ //passing in no parameters and returning nothing..this will just build the array containg alphabet
for (int i = 0; i <= 25; i++)
alpha[i] = (char)(65 + i);
}
public static void displayMenu(){
//Passing in no parameters and returns nothing...This will display menu options.
Scanner input =new Scanner(System.in);
System.out.println("Please select a number from the options below");
System.out.println("");
while(true){
// Give the user a list of their options
System.out.println("1: Decode a message");
System.out.println("2: Encode a message.");
System.out.println("3: Display the alphabet.");
System.out.println("0: Exit");
// Get the user input
int userChoice = input.nextInt();
switch(userChoice){
case 1:
// prompt the user to enter an encoded phrase or sentence of text. Convert it to uppercase. Decode it. Print out each //decoded letter as you convert it..call following methods
getPhrase();
decodeMessage(newphrase);
displayAlphabet();
break;
case 2:
// prompt the user to enter a decoded phrase or sentence of text. Convert it to uppercase. Encode it. Print out each //encoded letter as you convert it. call following methods
getPhrase();
encodeMessage(newphrase);
displayAlphabet();
break;
case 3:
//display the alphabet on the monitor screen
displayAlphabet();
break;
case 0:
System.out.println("Exiting the program now");
System.exit(0);
}
}}//end of DisplayMenu method
public static String getPhrase(String alpha)
{ //Method will return string from phrase
System.out.println("Enter a message to encode or decode");
String phrase=input.nextLine();
char c[] = phrase.toCharArray();//Parse the string character by character to put into a new string to cap. letters
String newphrase="";
for(int i=0;i<c.length;i++){
newphrase =newphrase + c[i];{
StringBuilder sb = new StringBuilder(newphrase);}}
newphrase.toUpperCase();
return newphrase;
}
public void encodeMessage(String newphrase) {
//passes in string message and returns nothing, encodes message using caesar cipher and prints to screen
for (int i=0;i<newphrase.length();i++)
{char ch = newphrase.charAt(i);
if (ch >='A' && ch <='Z')
{
// int shift = (cw.charAt(i % cw.length()) - 'A');
int oldPositionInAlphabet = ch - 'A';
int newPositioninAlphabet = (oldPositionInAlphabet +1) % 26;
encryptedMessage = encryptedMessage + (char)(newPositioninAlphabet + 'A');
}}
}
public static void decodeMessage(char[] alpha) {
//passes in message string and returns nothing, decodes message using cipher and prints to screen
for (int i=0;i<newphrase.length();i++)
{char ch = newphrase.charAt(i);
if (ch >='A' && ch <='Z')
{
// int shift = (cw.charAt(i % cw.length()) - 'A');
int oldPositionInAlphabet = ch - 'A';
int newPositioninAlphabet = (oldPositionInAlphabet +1) % 26;
encryptedMessage = encryptedMessage + (char)(newPositioninAlphabet + 'A');
}}
}
public static void displayAlphabet(char[] alpha) {
/ prints out the alphabet array
for (i = 0; i < 26; i++)
System.out.print(alpha[i] + " ");
System.out.println();
System.out.println();
}
}
Explanation / Answer
As per my understanding, I have just modfied the program.
package org.students;
import java.io.*;
import java.util.*;
public class endec {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws java.io.IOException {
char[] alpha = new char[26];
char[] cipher = new char[26];
loadData(alpha);// /I want it to load my data first
displayMenu();// display menu
}
public static void loadData(char[] alpha){ // passing in no parameters and returning nothing..this will just build
// the array containg alphabet
for (int i = 0; i <= 25; i++)
alpha[i] = (char) (65 + i);
}
public static void displayMenu() {
// Passing in no parameters and returns nothing...This will display menu
// options.
Scanner input = new Scanner(System.in);
System.out.println("Please select a number from the options below");
System.out.println("");
String monitor = "";
while (true) {
// Give the user a list of their options
System.out.println("1: Decode a message");
System.out.println("2: Encode a message.");
System.out.println("3: Display the alphabet.");
System.out.println("0: Exit");
// Get the user input
int userChoice = input.nextInt();
switch (userChoice) {
case 1:
// prompt the user to enter an encoded phrase or sentence of
// text. Convert it to uppercase. Decode it. Print out each
// //decoded letter as you convert it..call following methods
monitor = getPhrase();
String decoded = decodeMessage(monitor);
displayAlphabet(decoded);
break;
case 2:
// prompt the user to enter a decoded phrase or sentence of
// text. Convert it to uppercase. Encode it. Print out each
// //encoded letter as you convert it. call following methods
monitor = getPhrase();
String encoded = encodeMessage(monitor);
displayAlphabet(encoded);
break;
case 3:
// display the alphabet on the monitor screen
displayAlphabet(monitor);
break;
case 0:
System.out.println("Exiting the program now");
System.exit(0);
}
}
}// end of DisplayMenu method
public static String getPhrase(/*String alpha*/){
// Method will return string from phrase
System.out.println("Enter a message to encode or decode");
String phrase = input.nextLine();
return phrase.toUpperCase();
/*//char c[] = phrase.toCharArray();
// Parse the string character by
// character to put into a new string to
// cap. letters
String newphrase = "";
for (int i = 0; i < c.length; i++) {
newphrase = newphrase + c[i];
{
StringBuilder sb = new StringBuilder(newphrase);
}
}
newphrase.toUpperCase();
return newphrase;*/
}
public static String encodeMessage(String newphrase) {
// passes in string message and returns nothing, encodes message using
// caesar cipher and prints to screen
byte b[] = newphrase.getBytes();
for (int i = 0; i < b.length; i++){
if (b[i] >= 'A' && b[i] < 'Z'){
// int shift = (cw.charAt(i % cw.length()) - 'A');
b[i] += 1;
}else if(b[i] == 'Z'){
b[i] = 'A';
}
}
return new String(b);
}
public static String decodeMessage(String newphrase) {
// passes in message string and returns nothing, decodes message using
// cipher and prints to screen
byte b[] = newphrase.getBytes();
for (int i = 0; i < b.length; i++){
char ch = newphrase.charAt(i);
if (b[i] > 'A' && b[i] <= 'Z'){
// int shift = (cw.charAt(i % cw.length()) - 'A');
b[i] -= 1;
}else if(b[i] == 'A'){
b[i] = 'Z';
}
}
return new String(b);
}
public static void displayAlphabet(String content) {
// prints out the alphabet array
/* for (int i = 0; i < 26; i++)
System.out.print(alpha[i] + " ");
System.out.println();
System.out.println();
}*/
System.out.println(content);
}
}