Reputation: 33
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.
Upvotes: 3
Views: 1014
Reputation: 6739
I believe there is a easier way to solve your issue, and you can find your answer in ASCII table
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
Upvotes: 0
Reputation: 3669
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);
}
}
Upvotes: 0
Reputation: 310
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.
Upvotes: 1
Reputation: 875
I'm guessing what you're attempting to accomplish is a Caesar Cypher. Take a look at this post: Java Int & ASCII Question
Upvotes: 0