-
convert int to char
hello everybody!
i want to alter letters like this:
char letter;
int k;
...
letter + k
for example: a+1=b, b+1=c; x+2=z;...
i did it like that:
{
...
String b;
char a,y;
int k = 1;
...
b = readWord(); // input is the letter 'A'
a = b.charAt(0);
y = a + k;
print(y);
...
}
output: 66 ('B' in ASCII) but i want the letter 'B' to be put out. how can i convert the "int" into a "char" again?
later i want to 'crypt' a whole string.
thx and nice greez
ps.: sorry for the sloppy english! :rolleyes:
-
It's quite simple, you just do a cast to a char:
int numericValue = 66;
char c = (char)numericValue;
This 'forces' the value into a character variable.
-
already tried it like this:
String b;
char a,z;
int x;
print("String: ");
b = readWord();
a = wort.charAt(0);
x = a;
z = (char)x;
println(z);
doesn't work :(
whats wrong? :confused:
thx for helping, greez
chris
-
I adjusted your code to get it to compile and it behaved as expected, in this case, printing out 'B'.
Code:
public class Test {
public static void main(String[] args) {
String b;
char a,z;
int x;
System.out.println("String: ");
b = "Beautiful!";
a = b.charAt(0);
x = a;
z = (char)x;
System.out.println(z);
}
}
Run this code. Tell me if this is what you want.
ArchAngel.
-
Hi, out of curiosity I tried to run your code.. with
import java.swing.*; above the code. And then: 'java Test' at the prompt to create java application.
The error encountered it says:
$ /usr/j2se/bin/java test
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Wonder what exactly is the problem?
Is System.exit( 0 ); needed b4 the end of the main to terminate application?
-
I got it! I should have compiled it with javac then run with java Test.
The output is:
String
B
Sorry.