Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble changing characters in strings

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.

like image 509
Finn Avatar asked Dec 04 '25 02:12

Finn


1 Answers

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.

like image 145
codefan-BK Avatar answered Dec 06 '25 15:12

codefan-BK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!