I am trying to go through a string one character at a time and change those characters into their ASCII numbers. I have it working wine except for the spaces in the text turned into ''
(with no space) instead of ' '
(with space).
Scanner in = new Scanner(original_file);
PrintWriter out = new PrintWriter("encoded_message.txt");
text_file = "";
while (in.hasNextLine()) {
text_file += in.nextLine() + "\n";
}
char[] text_file_array = text_file.toCharArray();
for (char c : text_file_array) {
int code = Character.getNumericValue(c); //<-- The problem is here.
out.println(code);
}
in.close();
out.close();
When I run this, the c
becomes ''
which makes code
have a value of -1. I later use code
in an array. How do I make it return ' '
?