So f.e. i have a file ala.xs that contains: "sąłs" and i would like to read those symbols from this file and then put them into the table of shorts and then printf them on stdout. I am having some trouble with this becouse I have to read them as single chars and then join this chars into shorts. Here is my code:
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Main {
public static void main(String[] args) throws IOException {
short [] words = new short[10];
for(int i = 0 ; i < 10; i++)
words[i] = 0;
Reader reading = new FileReader("ala.xs");
short character;
int i = 0;
while ((character = (short) reading.read()) != -1) {
words[i] = character;
System.out.println(words[i]);
i++;
}
reading.close();
}
}
I tried to printf them like System.out.println((char)words[i]); but this is just stupid. I had this same task to do in C and in C i just had to printf them as %c and stdout joined them automaticly (that is my theory)
Assuming you are trying to read all the characters of the input file then instead of using the array of
shortdirectly concatenate the characters into a String. Using a StringBuilder is more efficient than concatenating directly to a String because Strings in Java are immutable.It should be something like this: