2

here's a code about depth first search in graphs. who knows why bufferedReader class were used in this code? and why nextInt function not used instead? what is its privilege? is it for speeding up processing? Thanks :)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Graph
{
int g[][];
int v,e;
int visited[];
void createGraph()throws IOException
{
    int a,b;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("\n Enter Number Of Vertices = ");
    v=Integer.parseInt(br.readLine());
    System.out.print("\n Enter Number Of Edges = ");
    e=Integer.parseInt(br.readLine());  
    g=new int[v+1][v+1];
    for(int i=1;i<=e;i++)
    {
        System.out.print("\n Enter Edge Infomation ");
        System.out.print("\n From =");
        a=Integer.parseInt(br.readLine());
        System.out.print("\n To =");
        b=Integer.parseInt(br.readLine());  
        g[a][b]=g[b][a]=1;
    }
}
void callDFS()
{
    visited = new int[v+1];
    dfs(1);
}   
void dfs(int k)
{
    System.out.print(k + "\t");
    visited[k]=1;
    for(int i=1;i<=v;i++)
    {
        if(g[k][i] !=0 && visited[i]!=1)
        dfs(i);
    }
}
}
class DFS
{
public static void main(String args[])throws IOException
{
    Graph g = new Graph();
    g.createGraph();
    g.callDFS();
}
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
Shr723
  • 23
  • 1
  • 5
  • 2
    So that invalid integers are detected? – fge Jan 12 '13 at 11:07
  • 1
    Why are you asking here? Why not ask the person who wrote it? All you'll get here is guesswork, as for instance maybe the author didn't know about Scanner, or didn't have it available at the time, or didn't care, or preferred BufferedReader for any of a number of possible reasons such as @fge's. Not constructive. – user207421 Jan 12 '13 at 11:10
  • @EJP you can treat this as "what are the benefits of BufferedReader over Scanner – John Dvorak Jan 12 '13 at 11:11
  • @JanDvorak I We could all guess about what the OP really means. I prefer to deal with the text as written. He asked why. – user207421 Jan 12 '13 at 11:14
  • It looks like the code is designed for reading a lot of lines from System.in in one go. Like you copy and paste the content of a file to the console. Buffering the input stream reader always increases performance. – Andreas Dolk Jan 12 '13 at 11:16
  • cuz i cant find who write it :) – Shr723 Jan 12 '13 at 11:19

3 Answers3

2

Probably, when this code was written, the Scanner class didn't exist (in fact java 1.4 did not have the Scanner class), or maybe who written this code simply preferred using BufferedReader's readLine method instead of using Scanner.nextLine() method, i can't see other explainations about your question

BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • hi Harlandarka you mean it may works with Scanner's methods,too? – Shr723 Jan 12 '13 at 11:15
  • @Shr723 Scanner would work too (except it accepts spaces as delimiters by default as well, while the former requires newlines). – John Dvorak Jan 12 '13 at 11:16
  • I don't mean it may works with scanner methods, i mean it **will absolutely** work with Scanner.nextLine method, because it take user input from line start to line end (\r, \n or both) – BackSlash Jan 12 '13 at 11:19
  • @JanDvorak Scanner accepts spaces instead of newlines **only if** you use the Scanner.next method, if you use Scanner.nextLine it accepts the newline delimiter – BackSlash Jan 12 '13 at 11:23
2

It's an issue of how you intend to use the stream. A buffered reader exists for simple and threaded applications. This is due to scanner's lack of thread safety.

I think you'll get more on this from this question Scanner vs. BufferedReader

Community
  • 1
  • 1
korefn
  • 955
  • 6
  • 17
0

BufferedReader is simpler (which makes it slightly more efficient) but it is also a clearer choice showing you intend to do is to use the functionality BufferdReader provides. i.e readLine() is the main one.

In short, if you have BufferedReader you know it is just going to read lines. If you use Scanner it implies you may or many not be reading something more complicated.

BTW:

  Integer.parseInt(br.readLine())

and

  scanner.nextInt();

are not the same although the distinction is usually lost on noob developers. For this reason I prefer the first example. The difference is how new lines are handled.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130