8 Replies - 2758 Views - Last Post: 08 April 2012 - 08:52 PM

#1 XxDnGxX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-March 11

Reading from a text file into a array(Android).

Posted 07 April 2012 - 01:36 PM

 

File file = new File("test.txt");
try {
    BufferedReader in = new BufferedReader(new FileReader(fileToRead));
    String str;
    while ((str = in.readLine()) != null) {

    	  for (int i = 0; i < 35; i++){
  	  		  char[] x = new char[34];
  	  		  str = in.readLine();
  	  		  x = str.toCharArray();
  	  		  for (int f = 0; f < 34; f++){
  	  			  map[f][i] = x[f];
  	  		  }
    }
    in.close();
}} catch (IOException e) {
}

   


I am trying to read in a text file in android. I am going to send the array to a switch statement but the switch statement only uses the default so I am guessing that my file is not reading in. Any ideas what I am doing wrong, also where would I need to put the text file in order to read it? Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Reading from a text file into a array(Android).

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Reading from a text file into a array(Android).

Posted 07 April 2012 - 05:27 PM

I'm not an Android guy... But I can read C.

what do you mean "I am guessing that my file is not reading in"?
Why would you be guessing? Your IDE must have some sort of debugging capability, right? There must be a way of placing a breakpoint and looking at the values of the variables.

Personally I wouldn't try to write any kind of program for any purpose until I learned how to debug and how to use the development environment. If you don't do that then you find yourself in this situation of stumbling around in the dark guessing.

In the C# area where I do most of my posting I tell this to a LOT of people: Learn to debug first. 3 hours spent on debugging tutorials will save you 300 hours of confusion on your first project alone.

I did a google for "Android debugging tutorials" and there seem to be many.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Reading from a text file into a array(Android).

Posted 07 April 2012 - 05:38 PM

your sample code has some inconsistencies that make me wonder if this even runs when you try it.
How can this run when in line 01 you use the variable "file" but in line 03 you use "fileToRead"?

01 File file = new File("test.txt");
02 try {
03 BufferedReader in = new BufferedReader(new FileReader(fileToRead));



I'm worried you are throwing away half of your file data. You read a line in on line 05 and do nothing with it, then read in again on line 09. So while str is set to every line, it is only evaluating every other line of the file.

I suspect this is backwards from your intent. Its certainly backwards from how most coders fill an array.
12 map[f][i] = x[f];
The outter loop is i and would normally be the first dimension of the array.
Like this you are filling it virtically before horizontally.

f loop
map[0][0]
map[1][0]
map[2][0]
...
map[34][0]


i increments
map[1][1]
map[2][1]
map[3][1]
...
map[34][1]


In line 08 you are initializing your array to a size of 34 but looping for 35 iterations (0-34)
So the first time you tried to run this it should have failed with an index out of bounds error.
Was This Post Helpful? 0
  • +
  • -

#4 XxDnGxX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-March 11

Re: Reading from a text file into a array(Android).

Posted 07 April 2012 - 06:11 PM

It was actually a mistype on top I put in something I had commented out, it is String fileToRead = "maze.txt"; not file, and it is not failing at all, I have something similar to this in a java applet and the program runs good, I am trying to put this in android. for map i have it set to char map[][] = new char[34][35]; it doesn't seem to me that it would go out of bounds unless i'm missing something.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Reading from a text file into a array(Android).

Posted 07 April 2012 - 07:09 PM

when looking at such small parts of code its hard to say.
We don't see the initialization of the map[][] array. I just assumed it was square.
Was This Post Helpful? 0
  • +
  • -

#6 XxDnGxX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-March 11

Re: Reading from a text file into a array(Android).

Posted 07 April 2012 - 07:33 PM

Yea sorry I should have included that. I'm just not for sure if this is how you are supposed to read in a file through android using the BufferedReader in = new BufferedReader(new FileReader(fileToRead)), its seems pretty different from just java in eclipse.
Was This Post Helpful? 0
  • +
  • -

#7 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,009
  • Joined: 13-March 09

Re: Reading from a text file into a array(Android).

Posted 08 April 2012 - 09:33 AM

This is a more correct way of doing this.

  ArrayList<String> list = new ArrayList<String>();
  try {
    InputStream instream = openFileInput("test.txt");
    if (instream) {
      BufferedReader buffreader = new BufferedReader(new InputStreamReader(instream));
 
      String line;
      while (( line = buffreader.readLine())) {
         list.add(line);
      }
 
    }
    instream.close();
  } catch (java.io.FileNotFoundException e) {
  }


This is just written from the top of my head, but i think this will work.
If not, i'll check it later.

This post has been edited by EndLessMind: 08 April 2012 - 09:34 AM

Was This Post Helpful? 2
  • +
  • -

#8 XxDnGxX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-March 11

Re: Reading from a text file into a array(Android).

Posted 08 April 2012 - 08:38 PM

ahh so then i would stick that in my activity i assume since its openfileinput, but how would i call that from my view? I have the file in a function of the activity class, and from the view class I was thinking something like ((Activity)getContext()).readText(); with readText being the name of my function. Does this sound anywhere right to you?
Was This Post Helpful? 0
  • +
  • -

#9 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,009
  • Joined: 13-March 09

Re: Reading from a text file into a array(Android).

Posted 08 April 2012 - 08:52 PM

You can put the ArrayList inside the Class, can call in from a void.
Like:
ArrayList<String> list = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    ReadFile("test.txt");

]

public void ReadFile(String filename){
try {
  InputStream instream = openFileInput(filename);
  if (instream) {
    BufferedReader buffreader = new BufferedReader(new InputStreamReader(instream));

    String line;
    while (( line = buffreader.readLine())) {
       list.add(line);
    }

  }
  instream.close();

  // Do what you want with the Array
} catch (java.io.FileNotFoundException e) {
}


}


or in a function

ArrayList<String> list = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    list = ReadFile("test.txt");

]

public ArrayList<String> ReadFile(String filename){
try {
ArrayList<String> array = new ArrayList<String>()
  InputStream instream = openFileInput(filename);
  if (instream) {
    BufferedReader buffreader = new BufferedReader(new InputStreamReader(instream));

    String line;
    while (( line = buffreader.readLine())) {
       list.add(line);
    }

  }
  instream.close();

  return array;
} catch (java.io.FileNotFoundException e) {
}


}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1