Follow us on Twitter!
It is the path of least resistance that makes rivers and men crooked. - Bj Palmer
Wednesday, April 23, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 21
Guests Online: 19
Members Online: 2

Registered Members: 82885
Newest Member: ConiBE
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

in java...scanner vs bufferedreader

the_unwanted
Member

Your avatar

Posts: 11
Location:
Joined: 03.12.10
Rank:
Newbie
Posted on 21-03-11 05:55
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... Sad
Author

RE: in java...scanner vs bufferedreader

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 21-03-11 07:37
Scanner's nextLine() returns a String that includes the newline character at the end of the line. BufferedReader's readLine() returns a String that does not. That's why a.equals(b) will never return true.


...

Edited by GTADarkDude on 21-03-11 07:38
- - -
Author

RE: in java...scanner vs bufferedreader

the_unwanted
Member

Your avatar

Posts: 11
Location:
Joined: 03.12.10
Rank:
Newbie
Posted on 21-03-11 12:01
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

Edited by the_unwanted on 21-03-11 15:22
Author

RE: in java...scanner vs bufferedreader

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 21-03-11 15:02
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:
Code
import java.io.FileReader;
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);

    }
}






...

Edited by GTADarkDude on 21-03-11 15:02
- - -