0

This is what i got

public class CharacterArithmeticExperiment {
  public static void main(String[] args) {
    char start = '1';
    for(int i = 1; i < 500;i++){
      System.out.printf("%c", start + i);
    }
  }
}

output:

23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~????????????????????????????????������������������������������������������������������������������������������������������������??????????????????????????????????????????????????????????????????????????????????��????????????��??????????????????????�????��???????????????????�??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

That's the problem, do i need something else to properly display this, or whats up with this, I haven't learned too much on characters, but i know darn well there's more characters than that, and i really want to see them any help is greatly appreciated.

Jacob Burgess
  • 55
  • 1
  • 7
  • 5
    The characterset o your comand line is not `UTF-8`. It is not a problem with your code – Jens Sep 10 '18 at 05:53
  • refer also to a [ascii table](https://www.asciitable.com/) - you are already starting at ascii 50 – Scary Wombat Sep 10 '18 at 05:54
  • Characters up to 128 are identical in all charsets. The 128 to 256 differ from charset to charset. Chars above 256 are only available in Unicode/utf8 – iPirat Sep 10 '18 at 05:56
  • you are using which IDE or console – Pramod Yadav Sep 10 '18 at 06:18
  • 1
    @iPirat That is blatantly incorrect statement. For example EBCDIC is completely different from ASCII-derived character sets. Also, Unicode and UTF-8 are different things. Unicode is a set of characters, UTF-8 is a way to represent said characters in data transmit. – Torben Sep 10 '18 at 06:47
  • Possible duplicate of [How can I iterate through the unicode codepoints of a Java String?](https://stackoverflow.com/questions/1527856/how-can-i-iterate-through-the-unicode-codepoints-of-a-java-string) – Torben Sep 10 '18 at 06:48
  • Also https://stackoverflow.com/questions/26729378/how-to-write-unicode-value-to-a-file-using-java – Torben Sep 10 '18 at 06:48
  • 1
    To learn more about the characters, visit https://unicode-table.com/en/. Note: not all characters can be displayed (End-Of-Text, Bell, Backspace, Device-Control-Four, ...). – Peter Walser Sep 10 '18 at 07:38
  • Echoing @Jens, the problem is how the text is flowing from your program to your screen. Please [edit] your question to describe that part. Your code will work except in the range `'\uD800'` to `'\uDFFF'` because those `char` values need to be properly paired to form a character (would be in a `String`). – Tom Blodget Sep 10 '18 at 11:51

1 Answers1

0

The char datatype in java is based on the ascii values. Every character, symbol and integer is associated with a ascii value. In your code:

public class CharacterArithmeticExperiment {
public static void main(String[] args) {
char start = '1';
for(int i = 1; i < 500;i++){
  System.out.printf("%c", start + i);
}
}
}

Here the ascii value of '1' is 49. In for loop while adding i to start the variable start gets typecasted and becomes 49. So for the first iteration the value of start+i = 50 and the character associated with the ascii value 50 is '2'. So the values get printed accordingly for the next iterations too. So the working code for printing characters could be according to the ascii values as follows :

public class CharacterArithmeticExperiment {
public static void main(String[] args) {
char start = 'a';
for(int i = 0; i < 26 ;i++){
  System.out.printf("%c", start + i);
}
}
}

OUTPUT : abcdefghijklmnopqrstuvwxyz

for refering to ascii value table click here

  • 1
    Replace ASCII with [UTF-16](https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html) and this would be correct. UTF-16 is one of several encoding for the [Unicode](http://www.unicode.org/charts/nameslist/index.html) character set. (If you ever were to write code that uses ASCII, you'd know which specification requires it. For example, [RFC 7230](https://tools.ietf.org/html/rfc7230). Otherwise, references to ASCII are typically erroneous.) – Tom Blodget Sep 10 '18 at 11:41