0

我正在努力在 java 中创建一个 PalindromeServer,但是我不断收到找不到行的错误,这似乎来自我的 PrintWriter,但我不知道为什么。有人可以解释导致 no line found 错误的原因吗?

我的代码

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 测试

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" );
}

错误

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)
4

1 回答 1

0

scan有“找不到行” (NoSuchElementException),因为你正在关闭你的client. 循环完成迭代后等待关闭客户端

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

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

   ...
}

client.close();
于 2013-11-14T02:44:22.250 回答