Well there are two parts of code I'm working on, one is a driver for Caesar Cipher and the other is the Caesar Cipher class itself. I can't figure out how to pass the information the user inputs from the driver class to the cipher class. Any help would be great, this is getting aggravating.

public class Driver
{
    public static void main (String [] args)
    {
        Scanner in = new Scanner(System.in);    // read input from terminal window
        CaesarCipher code;                      // an instance of a cipher
        String text;                            // user input to convert
        String result;                          // result of conversion
        int key;                                // key for Caesar cipher
        int menuSelection;                      // user's menu selection
        
        System.out.println("CS 160, Program 2: Caesar Cipher");
        System.out.println("--------------------------------\n");
        
        System.out.print("Enter the cipher key [0..25]: ");
        key = in.nextInt();
        code = new CaesarCipher(key);
        
        displayMenu();
        menuSelection = getMenuChoice(in);

        while (menuSelection != QUIT)
        {
            if (menuSelection == ENCODE)
            {
                System.out.print("Enter a sentence to encode: ");
                text = in.nextLine();
                result = code.encode(text);
                System.out.println("Plain:  " + text);
                System.out.println("Cipher: " + result);
            }
            else if (menuSelection == DECODE)
            {
                System.out.print("Enter numbers to decode: ");
                text = in.nextLine();
                result = code.decode(text);
                System.out.println("Cipher: " + text);
                System.out.println("Plain:  " + result);
            }
            else
            {
                System.out.println("Invalid option, please try again.");
            }
            
            displayMenu();
            menuSelection = getMenuChoice(in);
        }
        
        System.out.println("\nGoodbye.");
        
    }
    
    private static void displayMenu()
    {
        System.out.println();
        System.out.println("1. Encode a message.");
        System.out.println("2. Decode a message.");
        System.out.println("3. Quit.");
    }
    
    private static int getMenuChoice(Scanner in)
    {
        int menuChoice;     // user's menu selection     
        
        System.out.print("Please enter your menu choice: ");
        menuChoice = in.nextInt();
        in.nextLine();      // clear the user input line
        return menuChoice;
    }
    
    public static final int ENCODE = 1;     // user wants to encode text
    public static final int DECODE = 2;     // user wants to decode numbers
    public static final int QUIT = 3;       // user wants to quit
}
import java.util.Scanner;


public class CaesarCipher
{
    public CaesarCipher(int key)
    {
    }
    
    public String encode(String text)
    {
        char current;
        //int code;
        this.text = text;
        
        for (int position = 0; position < text.length(); position++)
        {
            current = text.charAt(position);
            current = Character.toLowerCase(current);
            System.out.print(current + " ");
            
            int code = (int) current;
            //System.out.print(code + " ");
            
            if (Character.isLowerCase(current))
            {
                code = code - A_VALUE; //+ key;
                System.out.print(code + " ");
            }
            
            //return code;
    }
    return "coded string";
    }
    
    public String decode(String text)
    {
       return "decoded string";
    }
    
    public int code;
    public String text;
    public int key;
    private static final int A_VALUE = (int) 'a';
}

Recommended Answers

All 2 Replies

Anybody? I'm so lost right now...

read your text in as a String Object, and pass that as arguments to the methods of your second class. Basic OO-stuff actually

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.