2

Here's my code:

import java.util.Scanner;

class Graph{
boolean [][]array;
int N;
Graph (){
    array = new boolean [1001][1001];
    N=0;
}

void read_graph() {
    Scanner sc = new Scanner(System.in);
    N = sc.nextInt();
    sc.nextLine();

    String str;
    String []substr;

    for (int K=1; K<=N; K++){
        str = sc.nextLine();
        substr = str.split(" ");
        for (int I=0; I<substr.length; I++)
            System.out.println(substr[0]+" "+substr[I]);
    }   
}

void query(){
Scanner sc = new Scanner(System.in);
    int P, Q;
    int counter = 0;
    boolean flag = true;
    while (flag){
    counter++;
    P = sc.nextInt();
    Q = sc.nextInt();
    sc.nextLine();
    if ( P == Q && P == 0 )
        flag =false;
    else {
        if (Q == 1)
            System.out.println("DFS done");
        else
            System.out.println("Bfs done");      
                  }
    }
    }       
}
class demo{
public static void main( String [] args ){
    Graph G= new Graph();
    Scanner sc = new Scanner(System.in);
    int numGraphs = sc.nextInt();
    while (numGraphs>0){
        G.read_graph();
        G.query();
        numGraphs--;
        }
    }
}

Here's the Input data:

1
6
1 2 3 4
2 2 3 6
3 2 1 2
4 1 1
5 0
6 1 2
5 1
1 0
1 0
0 0

When I give this input data with keyboard it works fine but when I saved this input to file and redirected this as input(in linux using '<'), it throws error message.

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:855)
at java.util.Scanner.next(Scanner.java:1478)
at java.util.Scanner.nextInt(Scanner.java:2108)
at java.util.Scanner.nextInt(Scanner.java:2067)
at Graph.read_graph(b.java:13)
at demo.main(b.java:56)

Help me in pointing out the mistake.

Bharat Kul Ratan
  • 985
  • 2
  • 12
  • 24

3 Answers3

4

Don't create a Scanner object in every method. Pass the first Scanner object you have created around.

Here is a list of changes that should fix the issue:

--- demo-old.java   2012-01-25 23:12:54.000000000 +0530
+++ demo.java   2012-01-25 23:13:45.000000000 +0530
@@ -10,4 +10,3 @@

-void read_graph() {
-    Scanner sc = new Scanner(System.in);
+void read_graph(Scanner sc) {
     N = sc.nextInt();
@@ -26,4 +25,3 @@

-void query(){
-Scanner sc = new Scanner(System.in);
+void query(Scanner sc){
     int P, Q;
@@ -53,4 +51,4 @@
     while (numGraphs>0){
-        G.read_graph();
-        G.query();
+        G.read_graph(sc);
+        G.query(sc);
         numGraphs--;
Susam Pal
  • 32,765
  • 12
  • 81
  • 103
  • thanks, your trick worked but I still didn't get that why redirecting input from file didn't worked but input by keyboard is fine? – Bharat Kul Ratan Jan 25 '12 at 18:02
  • I believe it has something to do with the way Scanner maintains the buffer internally. In fact input by keyboard doesn't work for me when I copy paste the entire input in one shot. It works for me only if I type the input one by one. Perhaps when you make the entire input in one shot, the first Scanner object you create gets the entire input in its buffer and then the other two Scanner objects get empty buffers. – Susam Pal Jan 25 '12 at 18:06
  • @SusamPal - I think you're right. that would explain the different behavior. – Kelly S. French Jan 25 '12 at 21:05
1

Why are you creating 3 scanners? It is possible that it is choking in the lines

1) P = sc.nextInt();
2) Q = sc.nextInt();

because the input with only 1 int is being read in line 1 and then line 2 is trying to scan the nextInt() for an empty line.

I have no idea why this would work when inputing by hand, unless the input is in a different order.

Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
0

You should not use < to redirect input. You need to use scanner class to read from file.

File file = new File("data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
//logic
}
RanRag
  • 48,359
  • 38
  • 114
  • 167
  • 1
    There is nothing wrong about using `<` to redirect input. Redirection and piping are common ways to provide input to a program that reads from stdin. A Scanner(System.in) object doesn't care whether the input comes to it from keyboard, redirection or piping as long as it gets the input on standard input. – Susam Pal Jan 26 '12 at 07:44