in java...scanner vs bufferedreader
there are two files file1 contains scrambled words file2 contains unscrambled words we have to find the corresponding unscrambled word (from file2)for each scrambled word in file 1 i used scanner to read strings from file1 and sorted each string and stored in arraylist1 and used bufferedreader to read strings from file2 and stored in arraylist2 and stored corresponding sorted string in arraylist2buf
now when im comparing each string in arraylist1 to every string in arraylist2buf no string is matching …though there are matching strings (when im comparing strings …. its slways returing a non-zero value indicating both strings are not equal)
but when im using bufferedreader to read data from file1 (instead of scanner ) im getting correct answer
my question is why im not getting output when im using scanner(for reading file1)
note: ->i used character stream for both scanner and bufferedreader (FileReader) ->here arraylist1,arraylist2,arraylist2buf are arrayLists of type <string> ->if a string in arraylist1 matches a string in arraylist2buf then index of string in arraylist2buf is obtained ->then string at arraylist2(index) is fetched .. above two steps is repeated for every string in arraylist1… which gives required o/p
sorry for my bad english… :(
Scanner sn=new Scanner(new FileReader("file1.txt")); sn.useDelimiter("\n"); String dummy1; while(sn.hasNext()) { dummy1=sn.next(); char c[]=dummy1.toCharArray[]; Arrays.sort(c); String dump1=new String(c); arraylist1.add(dump1); }
FileReader fr = new FileReader("file2.txt"); BufferedReader br=new BufferedReader(fr); String dummy2; while((dummy2=br.readLine())!=null) { arraylist2.add(dummy2); char c1[]=dummy2.toCharArray[]; Arrays.sort{c1); String dump2=new String(c1); arraylist2.add(dump2); } int i=0; Iterator<String> itr =arraylist1.iterator(); while(itr.hasNext()) { i=arraylist2buf.indexOf(itr.next); if(i!=-1) { String result=arraylist2.get(i); System.out.println(result); } }
thats the main part of programme
file1 and file2 has contents in the following structure
word1 word3 uhuhu something anything etc…
i didnt use nextLine in scanner..but i used next which returns next String in file
does it still return string with newline char at its end??
edit:this is not original source code ..i just presented this code to better understand the problem…just a rough work
Dude seriously, that code is awful. It's full of syntax errors! And for God's sake, use the [ code ] tags.
Anyway, I noticed my previous post was wrong. It's the opposite. nextLine() returns "blah" while next() returns "blah\n".
Try it:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
Scanner sc = null;
try {
sc = new Scanner(new FileReader("test.txt"));
} catch(Exception e) {
e.printStackTrace(); System.exit(1);
}
sc.useDelimiter("\n");
ArrayList<String> list = new ArrayList<String>();
while(sc.hasNext())
list.add(sc.nextLine());
Collections.sort(list);
System.out.println("I just read " + list.size() + " lines..:");
for(String s : list)
System.out.println(s.length() + " " + s);
}
}