Problems with changing characters in strings

So the question asked is: change the characters of the string with 3 characters in front of them, so let's say the string "AB cd" will be changed to: "DE fg". I don't know how to program, but I tried my best and came up with the following:

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 am 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.

+3


source to share


4 answers


I believe there is an easier way to solve your problem and you can find your answer at ASCII table

enter image description here

As you can see, words have some number associated with 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 the 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

      

0


source


In addition to the helpful link provided by dmcqu314, answer some thoughts about your code and the errors you get.

Error on line 20

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

      

As @Jama Jurayevich said, you should really use a different variable than "i" for inner loops. For example, use "k". It helps a little - not much due to other bugs.

Error on line 21

char b = Lowercase.indexOf(i);

      

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



Error on line 23

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

      

Here you are trying to assign an int value to the string method. It is impossible at all. Strings are final objects in Java. And there is no way to assign a method to a method. If you want to add a char string to a string, you can do it like this (example):

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

      

The ellipse is for other encodings of your choice.

I hope these tips will help you to overcome some confusion a bit.

+1


source


I am assuming you are trying to accomplish this Caesar Cipher. Take a look at this post: Java Int and ASCII Question

0


source


Here's one solution. Compare this with the current code to see how to fix the errors.

I also made it move to the front of the array for letters at the end of the alphabet. IE: Entering 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);
    }
}

      

0


source







All Articles