Arnold Pettersson
Arnold Pettersson

Reputation: 27

Problems reading a text file in android java programming

I have some problems in reading text files.
I am trying to implement a method that can read from a text file.

The text file will be used in game.
Any help will be appreciated

I want to use this method for responding to the button I have in the layout.

The method will send an argument to createQuestions for reading a text file by line.

I want to make every line as questions and answers.

When I try it fails, and gives this error:
http://www.imageno.com/thumbs/20150416/0fx08sldrwxn.jpg

public void levelOne(View v)throws IOException{
Button buttond = (Button) findViewById(R.id.buttonOne);
mQuestions = new ArrayList<Question>();
createQuestions("/raw/hogskoleprovetOne");

   }

  public void createQuestions(String hogskoleprovet) throws IOException{
    new InputStreamReader(getAssets().open("hogskoleprovet"));

    InputStreamReader input = new InputStreamReader(getAssets().open("hogskoleprovet"));
    Scanner sc = new Scanner (input);
    //Lokala variabler för att användas i while loopen, för att man använder när man läser av

    String question;
    String answer;
    String answerOne;
    String answerTwo;
    String answerThree;
    String answerFour;

    while(sc.hasNext() == true){
        question = sc.nextLine();
        answer = sc.nextLine();
        answerOne = sc.nextLine();
        answerTwo = sc.nextLine();
        answerThree = sc.nextLine();
        answerFour = sc.nextLine();
        Question q = new Question (question, answer, answerOne, answerTwo, answerThree, answerFour);
        mQuestions.add(q);
    }

Upvotes: 0

Views: 42

Answers (1)

JP Moresmau
JP Moresmau

Reputation: 7403

You have this code:

public void createQuestions(String hogskoleprovet) throws IOException{
  InputStreamReader input = new InputStreamReader(getAssets().open("hogskoleprovet"));

Which means that whatever String you're passing as a parameter, you're not using it, you're reading the file "hogskoleprovet". I think what you mean is:

public void createQuestions(String hogskoleprovet) throws IOException{
  InputStreamReader input = new InputStreamReader(getAssets().open(hogskoleprovet));

Upvotes: 1

Related Questions