In Java, how do I set each character as an integer? - java

I would like to put the entire alphabet from A-Z into an array of 0 to 25. From then I would like to manipulate it. So, for example if the user input a character and a number, I would be able to move that character by a certain amount on the alphabet.
For example the letter "A" and the number "3" will give me the letter "D" in the end.
How should I implement this? Thank you!

There is no need for an array. In Java, a char is an integral type. So you could use something like
int count = 3;
char out = (char) ('A' + (count % ('Z' - 'A')));
System.out.println(out);
Output is (as requested)
D

You can write directly 'A' + n:
char getChar(int n) {
return 'A' + n;
}
If the input is not necessarily correct you can add your conditions.

You could use the ASCII value of the letters.
A=65
B=66....
Take the char you need to know the value and do ('A' - 'A' + 1) +3
Replace the first A by any letter you need the value of

Related

Get next lexicographic character

Is there a method of java characters that will return the next character in lexicographical order?
char x = 'a'.next();
'a'.next() would return b. 'b'.next() would return c. etc.
char x = 'a';
System.out.println(++x);
Output: b
I hope this is enough as a hint.
Basically, just increase the ASCII code of char variable to get the next char.
And yes, ASCII code is basically just an integer. (Try doing System.out.println((int) 'a'); and you will get output as 97, which is the ASCII value for char a). Similarly, every char has some ASCII value.

How to get a character from a number in Java

Hi I want the opposite function of getNumericValue
int i = Character.getNumericValue('A');
if('A' == Character.someFunction(i)){
System.out.println("hooray");
}
I have tried "Character.forDigit" but this seems to be completely wrong.
Am new to java so please help.
The opposite is Character.forDigit
if(Character.forDigit(Character.getNumericValue('b'), Character.MAX_RADIX) == 'b') {
// true!
}
if(Character.forDigit(Character.getNumericValue('B'), Character.MAX_RADIX) == 'b') {
// true!
}
if(Character.getNumericValue('B') == Character.getNumericValue('b')) {
// true!
}
if((int)('B') == (int)'b') {
// false
}
Although given your question I think your looking for the actual ASCII char code for the letter.
Read this Java Character literals value with getNumericValue() post to see more information about Character.getNumericValue
To convert between char and int you can use typecasting. For example:
char myChar = (char) 65;
System.out.println(myChar);
will result in A. Hope that helps!
Character.getNumericValue('A') convert 'A' into unicode representation of the char. Probably you don't want to use that.
The letters A-Z in their uppercase ('\u0041' through '\u005A'),
lowercase ('\u0061' through '\u007A'), and full width variant
('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have
numeric values from 10 through 35. This is independent of the Unicode
specification, which does not assign numeric values to these char
values.

Casting int to char in Java

I'm looking for a straightforward answer and can't seem to find one.
I'm just trying to see if the following is valid. I want to take the integer 7 and turn it into the character '7'. Is this allowed:
int digit = 7;
char code = (char) digit;
Thank you in advance for your help!
This conversion is allowed, but the result won't be what you expect, because char 7 is the bell character whereas '7' is 55 (0x37). Because the numeric characters are in order, starting with '0' at 48 (0x30), just add '0', then cast the result as a char.
char code = (char) (digit + '0');
You may also take a look at the Unicode characters, of which the printable ASCII characters are the same codes.
'7' is Unicode code point U+0037.
Since it is a code point in the Basic Multiligual Plane, and since char is a UTF-16 code unit and that there is a one-to-one mapping between Unicode code points in this plane and UTF-16 code units, you can rely on this:
(char) ('0' + digit)
Do NOT think of '7' as ASCII 55 because that prevents a good understanding of char... For more details, see here.
Nope. The char '7' can be retrieved from int 7 in these ways:
int digit = 7;
char code = Integer.toString(digit).charAt(0);
code = Character.forDigit(digit, 10);
If digit is between 0 and 9:
int digit = 7;
char code = (char)(((int)'0')+digit);

Java-Function to print the variable with a particular ASCII code

I have to write a Java program for cryptography which takes a number from the user and adds( or subtracts the number if it is negative) from the ASCII code of the character. For example if the number taken is 2, and the given string is "ABCZ" it should return "CDEB".
I've figured out most of it, but I'm not sure how to return the character which has a given ASCII code. So if the ASCII is 65,how do I return "A"?
I'm only in class 10,so please keep it as simple as possible. Thanks in advance.
All you need is to cast the number to a character.
int yourInt = 65;
char ch = (char) yourInt;
System.out.println(yourInt);
System.out.println(ch);
// Output:
// 65
// A

JAVA: storing input into array

I need to write a program where the program would generate random letter and i would need to store this random character into an array
char[] arrayRandom = new char[10];
for (int i = 0; i < 8; i++) {
randomNumLet = (generator.nextInt(20) + 1);
System.out.print(arrayRandomLetter[randomNumLet] + " ");
arrayRandomLetter[randomNumLet] = arrayRandom[i];
}
is there anything wrong with my code?
because when i run this and printed the array i get boxes for all the values in the array and there are some letter that this line of code cannot print
System.out.print(arrayRandomLetter[randomNumLet] + " ");
Thanks
You're assigning an element of arrayRandomLetter a value from arrayRandom. As you never initialize arrayRandom, its values are all 0. 0 is not the value of a printable character, hence the boxes.
An easy way to pick a random character is like this:
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char randomChar = chars.charAt(random.nextInt(chars.length()));
You are trying to print arrayRandomLetter before it is assigned.
I'm not going to give you the answer, but I will give you a hint:
(char)('A' + 1) is 'B'
#fastcodejava's answer explains why you are seeing "boxes" -- rendering the ASCII NUL character.
#Mark Peters is also correct, but that's not the simplest solution.

Categories

Resources