NoSuchElementException: строка не найдена?
Я работаю над созданием PalindromeServer в Java, однако я продолжаю получать сообщение об отсутствии строки, которое, похоже, исходит от моего PrintWriter, но я не могу понять, почему. Может ли кто-нибудь объяснить, что является причиной отсутствия строки?
мой код
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)
1 ответ
Решение
scan
"не найдено ни одной строки" (NoSuchElementException)
потому что вы закрываете client
, Подождите, чтобы закрыть ваш клиент после завершения цикла
while(scan.hasNextLine()) {
...
if(in.equals(out)){
writer.print("YES");
}
else{
writer.print("NO");
}
...
}
client.close();