Reading a file using scanner, how to get to the file

Put your problem here if it does not fit any of the other categories.

Reading a file using scanner, how to get to the file

Postby milerk » Sun Jun 10, 2012 5:11 pm

Hello i got a quite annoying problem
i'd like to read text files similar to this

Code: Select all
KEY
0 1 2 3 4 5 6 7
KEYEND


i tried to use scanner class, because it can return results as strings, decimals, whatever

Code: Select all
   public static void LoadStuff(String Name) {
      Scanner reader = null;
      try {
         reader = new Scanner(new File(Name));
      } catch (Exception e) {
         Log.d("damn", "FAIL");
      }
         if(reader != null)
            Load(reader);
   }

Code: Select all
private static void Load(Scanner reader) {
      while (reader.hasNext()) {
         String result = reader.next();
         if (result == "KEY") {   // may be  result.equalsignorecase
            while (result != "KEYEND") {
               int index = reader.nextInt();
               Log.d("Index", String.valueOf(index));
            }
         }
      }
            reader.close();
   }

i cant do above, cause scanner cant find the file, parsing like "file.txt" doesnt work, tried also with path
like this "res/data/file.txt" also doesnt work
where should i put the file and how to get the directory to make it work
thanks
milerk
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Jun 10, 2012 5:04 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 17 guests