1

I know it must be obvious but can't find the answer.

What should I do to get the right characteres such as ", ', etc...?

This is the method I am using,

public void read(Context context) {

        InputStream inputStream = context.getResources().openRawResource(
                R.raw.puzzles);
        Scanner scanner = new Scanner(inputStream);
        scanner.useDelimiter(PATTERN);
        for (int i = 0; i < 70; i++) {
            ids[i] = scanner.next();
            titles[i] = scanner.next();
            questions[i] = scanner.next();
            answers[i] = scanner.next();
        }

    }

Thanks

eskalera
  • 1,072
  • 2
  • 21
  • 36

3 Answers3

1

After a few days of research I finally found the answer.

First of all I created a BufferedReader from the InputStream for character reading instead of raw byte reading.

Most importantly I instantiated the InputStream with the constructor with the charSetName.

If you go to 'Save as' in your NotePad you can choose your Encoding.

Finally I found here the supported encodings.

The default one in NotePad seems to be ANSI, which doesn't seem to be supported so I changed it and save my .txt file as UTF8.

Here is the code:

public void read(Context context) {

        InputStream inputStream = context.getResources().openRawResource(
                R.raw.puzzles);

        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(
                    inputStream, "UTF8"));
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }

        Puzzle p = null;

        Scanner scanner = new Scanner(bufferedReader);
        scanner.useDelimiter(TOKEN);
        // Skips the first strange char
        scanner.next();
        while (scanner.hasNext()) {

            p = new Puzzle();
            p.setId(scanner.next());
            p.setTitle(scanner.next());
            p.setQuestion(scanner.next());
            p.setAnswer(scanner.next());
            puzzles.add(p);
        }

    }
eskalera
  • 1,072
  • 2
  • 21
  • 36
0

The Scanner object seems to recognize only letters and numbers by default... Try an approach without using the scanner like the answer to this question: Android read text raw resource file

And use String.indexOf(), or TextUtils.StringSplitter, or anything similar to read each line and parse the true strings between your tokens.

You might want to use a while loop to read data until the end of the file rather than a fixed number (70 in your case) for future puzzle files that may have more or less data.

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Thanks @Sam, I'll definately go for an approach reading characters insted of bytes as said on your link, but still don't understand why I can't use Scanner. Where did you read that it only recognizes letters and numbers? Regarding the loop, you are absolutely right. I just didn't know how to do it with Scanner class. thx – eskalera May 11 '12 at 18:27
  • Honestly, your code and the Scanner documentation didn't give any clear reason why you are not reading symbols, so I assumed that you weren't making it up, agreed with you, and provided a viable alternative. :) I didn't feel like researching the why in depth, when I have used the other approach myself. – Sam May 11 '12 at 19:04
  • Still can't get the right symbols with the BufferedReader readLine() approach. Any suggestions? – eskalera May 14 '12 at 11:54
0

Try using an instance of Scanner where you pass in a character set, i.e. new Scanner (InputStream src, String charsetName)

HTH

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Hi @Devunwired, I have tried with utf-8 but still get the same result. What charset should I be trying with? – eskalera May 11 '12 at 18:02