1

I have tried to convert a random number into a character.
If I type a number into a variable it works, but if I do this int number = (int)(Math.random()*10); and then to assign it to a char and convert it char c = (char)number; when I print it, it shows nothing without errors like an invisible character.
Could you help me to do this?

MiniCiver
  • 13
  • 5
  • Does this answer your question? [Convert int to char in java](https://stackoverflow.com/questions/17984975/convert-int-to-char-in-java) – Mehdi Feb 17 '20 at 16:20
  • I've already tried this but it doesn't work :'( – MiniCiver Feb 17 '20 at 16:25
  • you tried exactly what? I would use `Character.forDigit(number, 10)` – user85421 Feb 17 '20 at 16:35
  • My problem was that the first characters os the ASCII table were like null characters, but if I start at least from 33 it works correctly. Thank you for your help! – MiniCiver Feb 18 '20 at 11:32
  • @MiniCiver - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 25 '20 at 19:00

4 Answers4

1

Just add '0' to your int.

int number = (int)(Math.random()*10);
char c = (char)number + '0';

Since '0' is ASCII value 48, '1' is 49, etc..., any number from 0 to 9 you sum to it will result in a number whose ASCII value is between between '0' and '9'.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
0

You are using a random from 0 to 9. And when trying to convert that to an integer you can have this possible values from the ASCII table

Dec  Char                         
---------                         
  0  NUL (null)                   
  1  SOH (start of heading)       
  2  STX (start of text)          
  3  ETX (end of text)            
  4  EOT (end of transmission)    
  5  ENQ (enquiry)                
  6  ACK (acknowledge)            
  7  BEL (bell)                   
  8  BS  (backspace)              
  9  TAB (horizontal tab)         
 10  LF  (NL line feed, new line) 

So you need to check the ASCII table for the Char to use

Gatusko
  • 2,503
  • 1
  • 17
  • 25
0

It is happening because you are getting a number from 0 to 9 which corresponds to a non-printable character. Try the following code:

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        int number = new Random().nextInt((126 - 33) + 1) + 33;
        char c = (char) number;
        System.out.println(c);
    }
}

This will print some character from the ASCII range of 33 to 126.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Use an Integer and convert to String.

public class Main {
    public static void main(String[] args) {
        Integer number = new Integer(Math.random() * 10);
        String c = number.toString();
        System.out.println(c);
    }
}
bcr666
  • 2,157
  • 1
  • 12
  • 23