Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exception in thread "main" java.util.NoSuchElementException: No line found

843789Sep 28 2009 — edited Sep 29 2009
Thank you fo ryour help in advance !

I am writing a client/server application where the client writes a message that is sent to the server, and the server must print thi smessage on the screen.

I am using a Scanner at the server side to read the inputstream from the client, but i get the following exception:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1471)
at TCPServer.main(TCPServer.java:32)
Java Result: 1

I changed the scanner into a buffered reader, it worked, but i want to know why the scanner doesnt work and how i can change the code to make it work.

The following is my code for both client and server:

Client:

Client:
import java.net.*;  
import java.io.*;
import java.util.*; 
public class Client {

    private static Socket clientSocket;
    private static Scanner inFromServer;
    private static PrintWriter outToServer;
    private static BufferedReader inFromUser;
    private static String messageToServer;
    private static String messageFromServer;
    public static void main(String args[]) {

            try {
                clientSocket = new Socket("localhost", 5000);
                inFromServer = new Scanner(clientSocket.getInputStream());
                outToServer = new PrintWriter(clientSocket.getOutputStream());
                
     InputStreamReader(clientSocket.getInputStream()));


                inFromUser= new BufferedReader(new InputStreamReader(System.in));
                messageToServer=inFromUser.readLine();
                System.out.println(messageToServer);
                
                outToServer.println(+'\n'+messageToServer+ '\n');
                clientSocket.close();
               
            } catch (IOException e) {
                System.err.println("IO error" + e);
            }

        }


}
****************************************************
Server:
import java.net.*;
import java.io.*;
import java.util.*;

public class TCPServer {
    
    
    private static Scanner inFromClient;
    private static PrintWriter outToClient;
    private static String message="hi";
    public static void main(String args[])
    {
            try
            {
                ServerSocket serv = new ServerSocket(5000);
                while(true)
                {
                   
                        Socket clientSocket=serv.accept();

                    inFromClient = new Scanner(clientSocket.getInputStream());
                    outToClient = new PrintWriter(clientSocket.getOutputStream());
                    message= inFromClient.nextLine();
                    System.out.println("received this message from client:"+message);
                    outToClient.println(message+"\n");
                    
                    
                }
            }
            catch(IOException e)
            {

            }

    }

}
Thanks

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Oct 27 2009
Added on Sep 28 2009
2 comments
1,354 views