3

So the question asked is: To change the characters of a string with 3 characters ahead of them so lets say the string is "AB cd" it would be changed to: "DE fg". I am not good at programing but I have tried my best and come up with this:

import java.util.*;

public class encrypt{

    public static void main(String[] args){

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter a message to encrypt: ");
        String message = reader.nextLine();

        List<Character> Lowercase = Arrays.asList('a','b','c','d','e','f','g','h','i','j',
  'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');

        List<Character> Uppercase = Arrays.asList('A','B','C','D','E','F','G','H','I','J',
  'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

        for ( int i = 0; i < message.length(); i++ ) {  
            char c = message.charAt( i );

            if( c == ' '){
                continue;
            }
            else if (c != ' '){
                for ( int i = 0; i < Lowercase.size(); i++ ) {
                    char b = Lowercase.indexOf(i);

                    if(c == b){
                        message.charAt(i)=Lowercase.indexOf(i+3);
                    }
                }
            }

            for ( int i = 0; i < Uppercase.size(); i++ ) {
                char j = Uppercase.indexOf(i);

                if(c == j){
                    message.charAt(i)=Uppercase.indexOf(i+3);
                }
            }
        }
    }               
}

I have been getting errors like :

Problem1.java:20: error: variable i is already defined in method main(String[]) for ( int i = 0; i < Lowercase.size(); i++ ) { ^ Problem1.java:21: error: possible loss of precision char b = Lowercase.indexOf(i); ^ required: char found: int Problem1.java:23: error: unexpected type message.charAt(i)=Lowercase.indexOf(i+3); ^ required: variable found: value Problem1.java:27: error: variable i is already defined in method main(String[])

any help would be appreciated :) thanks.

Jama A.
  • 15,680
  • 10
  • 55
  • 88
Finn
  • 33
  • 4
  • you should use j or other variable name for your inner loop index – Jama A. Sep 07 '14 at 23:20
  • 1
    There are too many questions here... I recommend removing code until you get *one* compiler error, and ask a specific question about it. – Chris Martin Sep 07 '14 at 23:20
  • @ChrisMartin my question is mostly how would I go about like comparing the characters from the user input string to the lower/upper case character, and then replacing the user inputstring with the character from the lower/upper array – Finn Sep 07 '14 at 23:25
  • @JamaJurayevich i have tried that but still didnt help anything :\ – Finn Sep 07 '14 at 23:25
  • @Finn you have lot of issues and misconceptions. if you like I can help you to solve this with better approach – Kick Buttowski Sep 07 '14 at 23:50
  • @KickButtowski yes please i am still very new to java and would really appreciate some help i dont know if there is private messaging on this site i just signed up for help. – Finn Sep 07 '14 at 23:54

4 Answers4

1

Besides the helpful link presented with dmcqu314's answer here some thoughts on your code and the occurring errors.

Error in line 20

for ( int i = 0; i < message.length(); i++ ) {

As @Jama Jurayevich stated, you really should use another variable than 'i' for the inner loops. Use 'k' for instance. That will help a bit - not a lot because of the other errors.

Error in line 21

char b = Lowercase.indexOf(i);

Lowercase.indexOf(i) will retrieve a (signed) int type. Assigning this to a char type (char b) provokes a type cast to something like an unsigned int (namely the char) - thus the hint of "possible loss of precision".

Error in line 23

message.charAt(i)=Lowercase.indexOf(i+3);

Here you are trying to assign an int value to string method. That's not possible at all. Strings are final objects in Java. And there is no way to assign something to a method. If you want to append a char to string, you can do it this way (example):

String newString = new String();
...
newString = newString + 'a'

The ellipse is for other codings of your choice.

Hope these hints will help you a little to fight some confusions.

codefan-BK
  • 310
  • 2
  • 9
0

I'm guessing what you're attempting to accomplish is a Caesar Cypher. Take a look at this post: Java Int & ASCII Question

Community
  • 1
  • 1
dmcqu314
  • 875
  • 11
  • 29
0

Here is one solution. Compare that to your current code to see how to correct your errors.

I also made it so it would loop around to the front of the array for the letters at the end of the alphabet. IE: Inputting the letter 'Z' will output 'C'.

import java.util.*;

class encrypt
{
    public static void main (String[] args) throws java.lang.Exception
    {
        char c,b,j;

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter a message to encrypt: ");
        String message = reader.nextLine();

        char[] messageArray = message.toCharArray();

        List<Character> Lowercase = Arrays.asList('a','b','c','d','e','f','g','h','i','j',
        'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');

        List<Character> Uppercase = Arrays.asList('A','B','C','D','E','F','G','H','I','J',
        'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

        for (int i = 0; i < message.length(); i++ ) {  
            c = messageArray[i];
            if (c != ' '){
                for (int x = 0; x < Lowercase.size(); x++ ) {
                    b = (char)(Lowercase.get(x));
                    if(c == b){
                        int n = (x+3)%Lowercase.size();
                        messageArray[i]=Lowercase.get(n);
                    }
                }
                for (int y = 0; y < Uppercase.size(); y++ ) {
                    j = (char)(Uppercase.get(y));
                    if(c == j){
                        int m = (y+3)%Lowercase.size();
                        messageArray[i]=Uppercase.get(m);
                    }
                }
            }
        }
        System.out.println(messageArray);
    }
}
Daniel Congrove
  • 3,519
  • 2
  • 35
  • 59
  • thank you so much! I am comparing this with mine i am trying to see what you did differently, because i really want to understand java. – Finn Sep 08 '14 at 00:57
  • Glad to help. If you have any questions or if you don't understand what certain lines of code mean, just let me know and I can help to explain them. – Daniel Congrove Sep 08 '14 at 01:17
0

I believe there is a easier way to solve your issue, and you can find your answer in ASCII table enter image description here

As you see, words have some number related to them like capital A is 65 and small c is 99, as a result, you can go through the number and use casting processes to get char you want.

read more casting from string to int

I think you should try this

Code:

        String s = "eh az";
        for (int i = 0; i < s.length(); i++) {
            int a = s.charAt(i);
            if (a >= 97 && a <= 119) {
                System.out.print((char) (s.charAt(i) + 3));
            } else if (a >= 120 && a <= 122) {
                a -= 23;
                System.out.print((char) a);
            } else if (a == 32) {
                System.out.print(" ");
            }
        }

output:

hk dc
Community
  • 1
  • 1
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
  • 1
    That's fine but use a Unicode table instead of an ASCII table. Java's Character holds one UTF-16 code unit, one or two of which encode a Unicode codepoint. – Tom Blodget Sep 08 '14 at 04:05
  • @TomBlodget that is good point, yet I did not want to make life more complicated for the OP cuz as you see, the op is novice. Sometimes making life easier is necessary to make a point and make the concept more understanding for the OP who is novice :) – Kick Buttowski Sep 08 '14 at 04:32
  • But so much can go wrong when programmers think ASCII is used everywhere when it is used almost nowhere. I don't think they should start out by learning ASCII. – Tom Blodget Sep 08 '14 at 12:35