Question

I am Working to make a PalindromeServer in java, however I keep getting a no line found error, that seems to be coming from my PrintWriter but I cant figure out why. Could someone explain what is causing the no line found error?

my code

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class PalindromeServer {

public static void main(String[] args){
    try{
    int port = 5150;
    ServerSocket ssocket = new ServerSocket(port);


    Socket client = ssocket.accept();

    OutputStream outStream = client.getOutputStream();
    Scanner scan = new Scanner(client.getInputStream());
    PrintWriter writer = new PrintWriter(outStream, true);

    while(scan.hasNextLine()){
        String in = scan.nextLine();
        String out = "";
        if(in.equals("SHUT DOWN")){
            ssocket.close();
        }
        in.replaceAll("\\s+","");
        int length = in.length();

          for ( int i = length - 1 ; i >= 0 ; i-- ){  
             out = out + in.charAt(i);
          }
          in.toLowerCase();
          out.toLowerCase();

          if(in.equals(out)){
              writer.print("YES");
              client.close();
          }
          else{
              writer.print("NO");
              client.close();
          }
    }

}catch (IOException e) {}

}
}

Junit Tests

import org.junit.runners.MethodSorters;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PalindromeServerTest {
@BeforeClass
public static void startServer() {
    // run server
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            PalindromeServer.main( null );
        }
    });
    thread.start();
}
@Before
public void waitTwoSecondsBetweenTests() {
    try {
        Thread.sleep( 2000 );
    } 
    catch (InterruptedException e) {
        e.printStackTrace();
    }
}

@Test(timeout=3000)
public void testServerWithNonPalindrome() {
    try {
        // run client
        Socket      socket  = new Socket( "localhost", 5150 );
        Scanner     scanner = new Scanner    ( socket.getInputStream() );
        PrintWriter writer  = new PrintWriter( socket.getOutputStream(), true );

        writer.println( "Not in Here" );

        String result = scanner.nextLine();
        assertEquals( "Incorrect result", "NO", result );

        socket.close();
    }
    catch (IOException e) {
        e.printStackTrace();
        fail( "Error opening client socket" );
    }
}

@Test(timeout=3000)
public void testServerWith1LetterPalindrome() {
    try {
        // run client
        Socket      socket  = new Socket( "localhost", 5150 );
        Scanner     scanner = new Scanner    ( socket.getInputStream() );
        PrintWriter writer  = new PrintWriter( socket.getOutputStream(), true );

        writer.println( "Z" );

        String result = scanner.nextLine();
        assertEquals( "Incorrect result", "YES", result );

        socket.close();
    }
    catch (IOException e) {
        e.printStackTrace();
        fail( "Error opening client socket" );
    }
}

@Test(timeout=3000)
public void testServerWithPalindrome() throws IOException {
    try {
        // run client
        Socket      socket  = new Socket( "localhost", 5150 );
        Scanner     scanner = new Scanner    ( socket.getInputStream() );
        PrintWriter writer  = new PrintWriter( socket.getOutputStream(), true );

        writer.println( "Kayak" );

        String result = scanner.nextLine();
        assertEquals( "Incorrect result", "YES", result );

        socket.close();
    }
    catch (IOException e) {
        e.printStackTrace();
        fail( "Error opening client socket" );
    }
}

@Test(timeout=3000)
public void testServerWithPerfectPalindrome() throws IOException {
    try {
        // run client
        Socket      socket  = new Socket( "localhost", 5150 );
        Scanner     scanner = new Scanner    ( socket.getInputStream() );
        PrintWriter writer  = new PrintWriter( socket.getOutputStream(), true );

        writer.println( "Able was I ere I saw Elba" );

        String result = scanner.nextLine();
        assertEquals( "Incorrect result", "YES", result );

        socket.close();
    }
    catch (IOException e) {
        e.printStackTrace();
        fail( "Error opening client socket" );
    }
}

@Test(timeout=3000)
public void testServerWithCapitalizedPalindrome() throws IOException {
    try {
        // run client
        Socket      socket  = new Socket( "localhost", 5150 );
        Scanner     scanner = new Scanner    ( socket.getInputStream() );
        PrintWriter writer  = new PrintWriter( socket.getOutputStream(), true );

        writer.println( "I prefer PI" );

        String result = scanner.nextLine();
        assertEquals( "Incorrect result", "YES", result );

        socket.close();
    }
    catch (IOException e) {
        e.printStackTrace();
        fail( "Error opening client socket" );
    }
}

@Test(timeout=3000)
public void testServerWithNonSense() throws IOException {
    try {
        // run client
        Socket      socket  = new Socket( "localhost", 5150 );
        Scanner     scanner = new Scanner    ( socket.getInputStream() );
        PrintWriter writer  = new PrintWriter( socket.getOutputStream(), true );

        writer.println( "I want my mojo back!" );

        String result = scanner.nextLine();
        assertEquals( "Incorrect result", "NO", result );

        socket.close();
    }
    catch (IOException e) {
        e.printStackTrace();
        fail( "Error opening client socket" );
    }
}

@Test(expected = IOException.class)
public void test_RunsLast_ServerShutsDown() throws IOException {
    try {
        // running client #1...shuts down server
        Socket      socket  = new Socket( "localhost", 5150 );
        PrintWriter writer  = new PrintWriter( socket.getOutputStream(), true );

        writer.println( "SHUT DOWN" );

        socket.close();
    }
    catch (IOException e) {
        e.printStackTrace();
        fail( "Error opening client socket" );
    }
    waitTwoSecondsBetweenTests();

    // running client #2...should throw exception (server should have stopped)
    new Socket( "localhost", 5150 );
    fail( "Socket should not connect after server was shut down" );
}

Error

java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at PalindromeServerTest.testServerWith1LetterPalindrome(PalindromeServerTest.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:74)
Was it helpful?

Solution

scan has "no line found" (NoSuchElementException) because you are closing your client. Wait to close your client after the loop is done iterating

while(scan.hasNextLine()) {
   ...

   if(in.equals(out)){
       writer.print("YES");
   }
   else{
       writer.print("NO");
   }

   ...
}

client.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top