0

I want to have a char, add number to it and get another char(a+2=c)

    int leng,leng2;
    String word = "";
    String key = "";
    String enc ="";
    char x;
    char z;
    char tost;
    System.out.println("gimme word");
    word=in.next();
    System.out.println("gimme key");
    key=in.next();
    leng=word.length();
    leng2=key.length();

    for(int i=1;i<=leng;i++)
    {
        z=word.charAt(i-1);
        x=key.charAt(i-1);
        int plus=z + x;
        tost=(char)plus;
        enc=enc+tost;
        System.out.println(enc);
        System.out.println(tost);
        System.out.println((char)z);
        System.out.println((char)x);
        System.out.println(plus);
        System.out.println((char)plus);
    }

I want it to print c and in my code I do it with charAt because I have a full string and I tried to search for many solutions, and tried myself many things, they all didnt work sadly.

edit: full code is on, as requested the way of char plus doesnt work and says it is an error

Hacktivator
  • 63
  • 3
  • 7
  • Java strings are counted sequences of Unicode/UTF-16 code units, one or two of which encode a codepoint. While it makes some sense to offset a codepoint, the result isn't necessarily associated with a character. I'd guess that you want to operate on limited range like the [Basic Latin](http://www.unicode.org/charts/PDF/U0000.pdf) letters and wrap around if +2 is overrange. – Tom Blodget Apr 05 '15 at 18:45

4 Answers4

1

If you want to do this for the whole string then you may use a for loop -

String r= "abc"; 
int x=2;

for(i=0; i<r.length(); i++){
  char z= r.charAt(0);  
  int plus=z+x; 
  System.out.println((char)plus);
}  

Look at the type casting at the System.out.println(). Since plus is an int you have to explicitly cast it to a char.

Razib
  • 10,965
  • 11
  • 53
  • 80
0

You need char plus=z+x; not plus=r+x;.

That would add 2 to the character a, resulting in the character c.

You can print it with :

System.out.println(plus);
Eran
  • 387,369
  • 54
  • 702
  • 768
0

Your code

String r= "abc";  
int x=2;
char z= r.charAt(0);  
plus=r+x                                       // wrong do int plus=z+x;  
String plus2=(String)String.valueOf(plus);     //not required
System.out.println(plus2);                     // plus not defined

Corrected Code

String r= "abc";  
int x=2;
char z= r.charAt(0);  
int plus=z+x;  
System.out.println((char)plus);

output

c

Demo

After your edit

change only one line

int plus=z + Character.getNumericValue(x);

Demo

singhakash
  • 7,891
  • 6
  • 31
  • 65
  • `z=word.charAt(i-1); //z is char x=key.charAt(i-1); //x is char plus=z + x; //plus is int tost=(char)plus; // tost is char enc=enc+tost; //enc is String System.out.println(enc); System.out.println(tost); System.out.println((char)z); System.out.println((char)x);` this is the original code – Hacktivator Apr 05 '15 at 17:57
0

It shouldn't even compile. You haven't declared the variable plus. Also you want to add 2 to the character at index zero of r which is stored in z. So you'll have to add x to z. Do something like this instead

String r = "abc";  
int x = 2;
char z = r.charAt(0);
char plus = z + x;
System.out.println(plus);
mushfek0001
  • 3,845
  • 1
  • 21
  • 20
  • `z=word.charAt(i-1); //z is char x=key.charAt(i-1); //x is char plus=z + x; //plus is int tost=(char)plus; // tost is char enc=enc+tost; //enc is String System.out.println(enc); System.out.println(tost); System.out.println((char)z); System.out.println((char)x);` this is the original code – Hacktivator Apr 05 '15 at 17:53