0

I have a program that supposedly

  1. prompts user to input a word, phrase, or sentence.
  2. I should then "encrypt" what you entered 13 times, printing every single time. The last thing printed should match user input.
  3. I can only encrypt alphabetical characters. Anything else remains the same.
  4. "encrypt" by finding the ASCII value of each character then increasing it by 2. If the letter changes case, make it so that it starts over at the a for lowercase or A for uppercase instead.

My code right now just gives me 1 encryption and stops at 2. It also only works for the first letter. My class hasn't learned arrays yet but we can try it if we want.

import java.util.Scanner;
public class Encrypt{

    Scanner keyboard = new Scanner(System.in);
    String message = new String();
    String g = new String();
    char y;
    public void input(){
        System.out.printf("Welcome to Encrypt.java. Please enter a word,phrase, or sentence. \n");
        System.out.println();
        System.out.print("->    ");
        message = keyboard.nextLine();
    }
    public void code(){
        int x = message.length()-1;
        boolean enter = true;

        for(int i = 0; i <= x; i++){
            int j = message.charAt(i);
            if((j >= 32 && j <=64) ||
               (j >= 91 && j <=96) ||
               (j >= 123 && j <= 127)){ 
            }

            else if((j >= 65 && j <= 90)){
                j = j + 2;
                if(j>90){
                    j = (j-90)+64;
                }
            }
            else if(j>=97 && j <= 122){
                j = j + 2;
                if(j>122){
                    j = (j-122) + 96;
                }
            }

            if(enter == true){
                System.out.println();
                System.out.print("  ");
                enter = false;
            }
            y = (char)(j);
            g = g + y;
            message = g;

            x = message.length()-1;             
        }
        System.out.print(g);
        System.out.println();
    }

    public void print(){
        for(int i = 1; i <= 13; i ++){
            System.out.println("Encryption " + i + ":");
            this.code();
        }

    }
    public static void main(String [] args){
        Encrypt e = new Encrypt();
        e.input();
        e.print();

    }
}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111

2 Answers2

2

Two Things :

  • Resetting the variable g after every iteration.
  • Proper placement of message.

 public void code() {
    int x = message.length() - 1;
    boolean enter = true;
    g = "";
    for (int i = 0; i <= x; i++) {
        int j = message.charAt(i);
            if ((j >= 32 && j <= 64) || (j >= 91 && j <= 96)
        || (j >= 123 && j <= 127)) {
            }

        else if ((j >= 65 && j <= 90)) {
            j = j + 2;
            if (j > 90) {
                j = (j - 90) + 64;
                }
        } else if (j >= 97 && j <= 122) {
            j = j + 2;
            if (j > 122) {
                j = (j - 122) + 96;
            }
        }
        if(enter == true){
            System.out.println();
            System.out.print("  ");
            enter = false;
        }
        y = (char) (j);
        g = g + y;
    }
    message = g;
}

Output:

Welcome to Encrypt.java. Please enter a word,phrase, or sentence. 

->    abba
Encrypt.code() message >> abba
Encrypt.code() message >> cddc
Encrypt.code() message >> effe
Encrypt.code() message >> ghhg
Encrypt.code() message >> ijji
Encrypt.code() message >> kllk
Encrypt.code() message >> mnnm
Encrypt.code() message >> oppo
Encrypt.code() message >> qrrq
Encrypt.code() message >> stts
Encrypt.code() message >> uvvu
Encrypt.code() message >> wxxw
Encrypt.code() message >> yzzy
ajc
  • 1,685
  • 14
  • 34
0

Here's your problem

y = (char) (j);
g = g + y;
message = g;

In the first run, g is only one char, and you're making message = g;. This makes message just that one character g. I ran it deleting the message = g and it works fine. I don't know if it's your desired output, but at least it's getting past Encryption 1

Note: You should really learn how to use a debugger. That's how I spotted the problem.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • `print()` is not repeating the `input()` commands. Isn't that what you want to happen: getting repeated user input? If not, I must've misunderstood your question – Paul Samsotha Oct 30 '13 at 01:47
  • This `if` has `else if` components. The first condition simply excludes (does nothing for) some ranges. – PM 77-1 Oct 30 '13 at 01:51
  • @PM77-1 Please post an example of your current output and your desired output. It would be easier to see where the problem could be if we have this information. It's hard to tell from your code and question – Paul Samsotha Oct 30 '13 at 01:53
  • so how do i make message equal to the string g? – user2881409 Oct 30 '13 at 03:18
  • Did you run it and see the output? You see what I mean about maybe not getting your desired output. I assume you wanted to get something in the lines of an encrypted message the same size as the original and not the 13 times the size right? – Paul Samsotha Oct 30 '13 at 03:32
  • Otherwise, I don't see why it' necessary to make `message = g`. I don't see `message` being used anywhere else in your code. – Paul Samsotha Oct 30 '13 at 03:38