The following code prints A
to the console:
char ch = (char)65.25;
System.out.println(ch);
However I don't understand why the next piece of code doesn't print the ASCII character?
int rand = (int)currentSeconds % 26;
System.out.println(" the number is " rand);
char randN = (char)rand;
System.out.println(randN);
Below follows the full code:
System.out.println(System.currentTimeMillis());
System.out.println(" using current second to generate random Uppercase letter");
long totalsecs = System.currentTimeMillis()/1000;
long currentSeconds = totalsecs % 60;
long totalMinute = totalsecs/60;
long currentMinute = totalMinute%60;
long totalHours = totalMinute/60;
long currentHours = totalHours%24;
System.out.println("current time is " currentHours ":" currentMinute ":" currentSeconds);
int rand = (int)currentSeconds % 26;
System.out.println(" the number is " rand);
char randN = (char)rand;
System.out.println(randN);
CodePudding user response:
There are many versions of the println
method, with different parameter types. In other words, if s
is a String
, i
is an int
and c
is a char
, then System.out.println(i)
is actually calling a different method from System.out.println(c)
, which is calling a different method from System.out.println(s)
. It's the compiler's job to figure out which method you're trying to call, and it takes into account the type of the expression that you pass in. So
System.out.println(rand)
calls theint
version of the method, which prints the number.System.out.println(randN)
calls thechar
version of the method, which prints the character, rather than its numeric equivalent.System.out.println("the number is " rand)
calls theString
version of the method, which prints aString
, and in this case, theString
is made by concatenating the number to the end of aString
literal.
If you're trying to print a character value in the range A
to Z
in place of a number from 0 to 25, then writing char randN = (char) rand;
isn't enough - that won't actually convert the number to the range you want, because the numeric values of the characters from A
to Z
are not actually 0 to 25.
You need to write char randN = 'A' rand;
to effect the desired conversion.
CodePudding user response:
tl;dr
Character.toString
(
"A".codePointAt( 0 )
ThreadLocalRandom.current().nextInt( 0 , 27 )
)
Or:
Character.toString
(
ThreadLocalRandom.current().nextInt( 65 , ( 65 27 ) )
)
Start with A
As others pointed out, to get a random character from A-Z, you need to add 0-25 to a starting number, that number being the code for A
which is 65.
Avoid char
Another problem with your code is its use of the char
. The char
type in Java is legacy, essentially broken. As a 16-bit value, that type cannot represent most characters.
While your particular case of A-Z works with char
, using char
is a bad habit, and an unnecessary habit.
Code points
Instead learn to use Unicode code point integer numbers. Unicode is a superset of US-ASCII, so code points 0-127 represent the same characters in both.
int codePointForA = "A".codePointAt( 0 ) ; // 65
int addend = ThreadLocalRandom.current().nextInt( 0 , 27 ) ; // ( inclusive , exclusive )
int randomCodePoint = ( codePointForA addend ) ;
String randomCharacter = Character.toString( randomCodePoint ) ;
See code run live at IdeOne.com.
Z