在字符串中改变字符有困难。

时间:2023-01-08 22:24:09

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:

因此,问题是:要改变一个字符串的字符,前面有三个字符,所以我们假设字符串是“AB cd”,它将改为:“DE fg”。我不擅长编程,但我已经尽了最大的努力,想出了这个办法:

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[])

Problem1。java:20: error:变量i已经在方法main(String[])中定义为(int i = 0;我< Lowercase.size();我+ +){ ^ Problem1。java:21:错误:可能丢失精度char b =小写。^要求:char发现:int Problem1。java:23:错误:意外类型message.charAt(i)=小写。^要求:Problem1变量发现:价值。java:27: error:变量i已经在方法main(String[])中定义了

any help would be appreciated :) thanks.

如有任何帮助,我们将不胜感激。

4 个解决方案

#1


0  

I believe there is a easier way to solve your issue, and you can find your answer in ASCII table 在字符串中改变字符有困难。

我相信有一个更简单的方法来解决你的问题,你可以在ASCII表中找到答案。

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.

正如你所看到的,单词有一些与之相关的数字,比如大写的A是65,小写的c是99,因此,你可以通过这个数字,使用铸造过程来得到你想要的char。

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

#2


1  

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

除了dmcqu314给出的有用链接之外,还有一些关于代码和出现错误的想法。

Error in line 20

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

正如@Jama Jurayevich所述,您确实应该使用另一个变量而不是“i”来进行内部循环。使用“k”为例。这将会有所帮助,因为其他的错误。

Error in line 21

在第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".

下标。indexof (i)将检索(签名)int类型。将其赋值为char类型(char b)会引发类型转换为无符号整数(即char),从而暗示“可能的精度损失”。

Error in line 23

在第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方法指定一个int值。那是不可能的。字符串是Java中的最终对象。也没有办法给方法赋值。如果要将char追加到string,可以这样做(示例):

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.

希望这些提示能帮助你克服一些困惑。

#3


0  

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

我猜你想要完成的是凯撒密码。看看这篇文章:Java Int & ASCII问题。

#4


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

我也做了,所以它会绕到数组的前面,在字母表的末尾。IE:输入字母‘Z’将输出‘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);
    }
}

#1


0  

I believe there is a easier way to solve your issue, and you can find your answer in ASCII table 在字符串中改变字符有困难。

我相信有一个更简单的方法来解决你的问题,你可以在ASCII表中找到答案。

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.

正如你所看到的,单词有一些与之相关的数字,比如大写的A是65,小写的c是99,因此,你可以通过这个数字,使用铸造过程来得到你想要的char。

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

#2


1  

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

除了dmcqu314给出的有用链接之外,还有一些关于代码和出现错误的想法。

Error in line 20

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

正如@Jama Jurayevich所述,您确实应该使用另一个变量而不是“i”来进行内部循环。使用“k”为例。这将会有所帮助,因为其他的错误。

Error in line 21

在第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".

下标。indexof (i)将检索(签名)int类型。将其赋值为char类型(char b)会引发类型转换为无符号整数(即char),从而暗示“可能的精度损失”。

Error in line 23

在第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方法指定一个int值。那是不可能的。字符串是Java中的最终对象。也没有办法给方法赋值。如果要将char追加到string,可以这样做(示例):

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.

希望这些提示能帮助你克服一些困惑。

#3


0  

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

我猜你想要完成的是凯撒密码。看看这篇文章:Java Int & ASCII问题。

#4


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

我也做了,所以它会绕到数组的前面,在字母表的末尾。IE:输入字母‘Z’将输出‘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);
    }
}