Search code examples
javajava.util.scannerinputstream

I am getting this no line found exception in server inputstream scanner


I am building a small server-client programme and i am using scanner to scan inputstream. but it is getting no line found exception evrytime. i don't know what to do. please help... there is a line that server's output stream is passing to client.

here is a stack trace.

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at Client.main(Client.java:26)

here is a code..

public class Client
{
    public static void main(String[] args) throws IOException
    {
        final int SBAP_PORT = 100;
        try (Socket s = new Socket("localhost", SBAP_PORT))
        {
            InputStream instream = s.getInputStream();
            OutputStream outstream = s.getOutputStream();
            Scanner in = new Scanner(instream);
            PrintWriter out = new PrintWriter(outstream);

            Scanner scanner = new Scanner(System.in);
            String command = scanner.nextLine(); // client interact with server through here.

            while(!command.equalsIgnoreCase("QUIT")) {
                out.print(command+"\n");
                out.flush();
                String response = in.nextLine();  // i am getting error here.
                System.out.println(response);
                command = scanner.nextLine();
            }
            command = "QUIT";
            System.out.println(command);
            out.print(command);
            out.flush();
        }
    }
}

this is how server sends response to client.

if(command.equalsIgnoreCase("track"))
        {
            data = new ServerData(phrase);
            Thread t = new Thread(data);
            t.start();
            usr.addPortfolio(data.getTicker(), data.getPrice());
            out.print("OK!");
            out.flush();
        }

"out" is socket outputstream.


Solution

  • This is likely because the server response does not have any line breaks. I would rewrite the way you process the data coming back from the socket by just reading it byte by byte instead.

    public class Client
    {
    public static void main(String[] args) throws IOException
    {
        final int SBAP_PORT = 8080;
        try (Socket s = new Socket("localhost", SBAP_PORT))
        {
            InputStream instream = s.getInputStream();
            OutputStream outstream = s.getOutputStream();
    
            //Scanner in = new Scanner(instream);
            PrintWriter out = new PrintWriter(outstream);
    
            Scanner scanner = new Scanner(System.in);
            String command = scanner.nextLine();
            System.out.println("Command: " + command); 
    
    
            while(!command.equalsIgnoreCase("QUIT")) {
                out.print(command+"\n");
                out.flush();
                //String response = in.nextLine();  // i am getting error here.
                int i = 0;
                char c;    
                while(( i = instream.read())!=-1) {
    
                    // converts integer to character
                    c = (char)i;
    
                    // prints character
                    System.out.print(c);
                }
    
    
    
                command = scanner.nextLine();
            }
            command = "QUIT";
            System.out.println(command);
            out.print(command);
            out.flush();
        }
    }
    }