0

I am inputing k and choice (Integers) in Main class. And then trying to input title_name in count class. But ,I am not able to input title_name.

//Main class

    package com.iiitd.ap.lab6;

import java.io.IOException;
import java.util.Scanner;

public class Main {
    static int k;
    static int option;



    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        k=in.nextInt();
        option=in.nextInt();
        in.close();

        System.out.println(k+" "+option);

        count t=new count(k,option);
        t.count_print();


    }

}

//count class

package com.iiitd.ap.lab6;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class count {
    static int k;
    static int option;

    count(int k, int option)
    {
        count.k= k;
        count.option= option;

    }



    int  decide_file_1() throws IOException
    {

        String title_name="";
        Scanner in=new Scanner(System.in);
        title_name=in.next();
        System.out.println("ttt");
            in.close();

        for(int i=1;i<=20;++i){
        FileInputStream fs= new FileInputStream("/home/tarun/Downloads/Lab6/Papers/paper"+i+".txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fs));
        System.out.println(title_name+" "+br.readLine());
            if(title_name.equals(br.readLine()))
                return i;               

        }

        return 0;

    }





    int count_print() throws IOException
    {
        if(option==1)
        {   
            System.out.printf("decide_file=%d",decide_file_1());

        }
        return 0;

    }




}

Any kind of help will be greatly appreciated.

7
  • It it related somewhere to concurrent access of input stream/resource? anyone having the exact explanation? Commented Sep 30, 2015 at 13:57
  • What do you mean "But ,I am not able to input title_name." ?are you getting any error ? Commented Sep 30, 2015 at 13:59
  • You have closed the input stream in your main... Don't do that.. Commented Sep 30, 2015 at 13:59
  • @Codebender, he instantiates a new Scanner in the count class, so it's not that... Commented Sep 30, 2015 at 14:01
  • @Tgsmith61591, instantiating a new Scanner wont help... The underlying stream is already closed... You cannot reopen a closed stream... Commented Sep 30, 2015 at 14:01

2 Answers 2

2

You are closing the Standard input stream (System.in) by calling in.close().

From the Scanner#close() javadoc,

If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked. If this scanner is already closed then invoking this method will have no effect.

This means that it will invoke the input stream's close() method, there by closing the stream and disconnecting the connection between your read console and the java program.

So, you cannot use System.in stream anymore in the program (doesn't matter if you are using a new Scanner() object or not).

Sign up to request clarification or add additional context in comments.

Comments

0

You are reading the line twice:

System.out.println(title_name+" "+br.readLine());
        if(title_name.equals(br.readLine()))
            return i;  

Store the value before printing and comparing. Once you read the line, the reader advances. So, the second readLine() call is getting the next line in the stream.

String title = br.readLine();
System.out.println(title_name+" "+title);
            if(title_name.equals(title))
                return i;  

3 Comments

Actually I have written the System.out.println() line , just to check is there something in title_name or not. But it is empty.
I see. Thanks for the clarification.
I might also suggest placing your Scanner inside a try-with-resources block which makes it easier to view the scope of your stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.