-1

Here I am reading file word by word and manipulating List view with these word. Problem here is First name and Last name are appearing in different rows. e.g. Name = "John Clerk" then I am getting "John" in first row and "Clerk" in second row of List view. They must be in single row and so forth for other data. What should I make changes to work it properly? My code...

String myData = "";
String strLine;
String listName = "" ;
FileOutputStream fos;
FileInputStream fstream;
DataInputStream in;
String[] SavedFiles;
BufferedReader br;


public void readFile(String file) throws IOException
    {

        fstream = openFileInput(file);
        Scanner scanFile = new Scanner(new DataInputStream(fstream));
        ArrayList<String> words = new ArrayList<String>();


        String theWord, theWord1, theWord2;     
        while (scanFile.hasNext())
        {
            theWord = scanFile.next();
            words.add(theWord);

        }
        Toast.makeText(getBaseContext(), "" + size, 1000).show();


        adapterFriends = new ArrayAdapter<String>(getBaseContext(), R.layout.text, words);
        lvFinal.setAdapter(adapterFriends);
        adapterFriends.notifyDataSetChanged();

        }
Looking Forward
  • 3,579
  • 8
  • 45
  • 65
  • possible duplicate of [android toast doesn't fit text](http://stackoverflow.com/questions/6888664/android-toast-doesnt-fit-text) – Nir Alfasi Aug 06 '13 at 07:18

2 Answers2

2

Try to use nextLine() instead of next(), as it should return every string between \n chars.

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextLine()

hope that helps

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
Adrián Rodríguez
  • 1,836
  • 15
  • 16
  • @Adrian...not getting output that I want...Instead all strings are apeearing in single row...Means if file has two names name1 = John Clerk and name2 = Steve Rich, then in one row of List view they are appearing as John Clerk Steve Rich... – Looking Forward Aug 06 '13 at 07:16
  • You're not answering the question - the OP asked how to control where the line-break would be placed (in the text that's displayed from `adapterFriends`) - it has nothing with reading the input. – Nir Alfasi Aug 06 '13 at 07:19
  • @alfasin I think it has a lot with reading the input. I mean, if the input is in sepparate lines, my answer is correct; as we don't have any further information of how the structure of the file is, my answer could have been the right one. To be back on topic, could you please give us more details of how's your file structured? Do you have control over it? – Adrián Rodríguez Aug 06 '13 at 07:30
  • In my file, I am saving data from Listview. Means there is one List view with some names. When user selects these names, they should be saved in file. And when I read that file, data should be again appear in second List view. – Looking Forward Aug 06 '13 at 07:35
  • @AdriánRodríguez is correct. The issue is not understanding how ArrayAdapter works. Every item in the ArrayList will be its own row. So, to properly do what is required the data needs to get into the ArrayList in a different format. – Rich Schuler Aug 06 '13 at 07:39
  • In such a case you answer would be helpful indeed - downvote cancelled. – Nir Alfasi Aug 06 '13 at 17:35
1

If I understand correctly what you need, try this:

while (scanFile.hasNext())
{
   String name = scanFile.next();
   if (scanFile.hasNext())
   {
      name = String.format("%s %s", name, scanFile.next());
   } 

   words.add(name);    
}   
Igor
  • 1,835
  • 17
  • 15
  • about to solve. what I am getting here, let me tell you. eg. name1 = John Clerk and name2 = Steve Rich, I am getting in first row John Clerk Steve and in second row only Rich.... – Looking Forward Aug 06 '13 at 07:48
  • Can you print me first 3-4 rows of your file from where your read? I suppose you have bad formatted file. – Igor Aug 06 '13 at 07:55
  • Post your writing to file method. I guess the problem is there. – Adrián Rodríguez Aug 06 '13 at 07:57
  • public void writeToFile(ArrayList datawrite, String file) throws IOException { try { fos = openFileOutput(file, Context.MODE_PRIVATE); for(int temp = 0; temp< datawrite.size(); temp++) { fos.write(datawrite.get(temp).getBytes()); //Toast.makeText(getBaseContext(), datawrite.get(temp).toString(), 1000).show(); } fos.close(); } catch(Exception e) { } } – Looking Forward Aug 06 '13 at 07:58
  • I think the problem is in datawrite ArraList. I suppose it look something like { "John", "Clerk Steve", "Rich"... } or something sumular – Igor Aug 06 '13 at 08:01
  • hey yoy are right icrew...Data are "John Clerk", "Steve Rich", but in file they are stored as John", "Clerk Steve", "Rich" – Looking Forward Aug 06 '13 at 08:08
  • What's need to be done to save these data in their original pattren? – Looking Forward Aug 06 '13 at 08:10
  • Yes, exactly. I suppose datawrite parameter in writeToFile is not correct – Igor Aug 06 '13 at 08:14
  • Also try saving data like correct answer here http://stackoverflow.com/questions/6548157/how-to-write-an-arraylist-into-a-text-file – Igor Aug 06 '13 at 08:19
  • It's sting array. In my file, I am saving data from List view. Means there is one List view with some names. When user selects these names, they should be saved in ArrayList datawrite and write to file. And when I read that file, data should be again appear in second List view. – Looking Forward Aug 06 '13 at 08:19
  • Please debug every part of process (Creating arraylist, writing arrayList to file, etc...). Its impossible for us to guess where can be error – Igor Aug 06 '13 at 08:21
  • finally did it...some modification in my write function and working like a charm..Thanks to all... – Looking Forward Aug 06 '13 at 09:25
  • Hey icrew...What if name has 3 words..eg. Mark J Tailor...When I read file with this data, Mark J is appearing in first line and Tailor is another row...What needs to be done? I tried to count whitespace and wrote logic accordingly, but nothing works. – Looking Forward Aug 07 '13 at 05:32
  • If you don't know if there will be two or three words per row, I think there's no algorythm to detect that. I guess you should use a delimiter such as ; or | between complete names. Something like John Clerk;Steve Rich;Mr Potato;etc. So when you read that back you only need to split that String by that token. – Adrián Rodríguez Aug 07 '13 at 06:04