1 2 Previous Next 27 Replies Latest reply: Nov 18, 2008 4:00 AM by masijade RSS

    How do I use string instead of System.in

    807589
      How do I use the string instead of a System.in

      my code is like this.

      DataInputStream console = null

      console = new DataInputStream(System.in);

      I want to send a String instead of the System.in

      Can anyone help me out please.

      Cheers, San
        • 1. Re: How do I use string instead of System.in
          799716
          the api will tell you if that is possible.
          [http://java.sun.com/javase/6/docs/api/java/io/DataInputStream.html]
          • 2. Re: How do I use string instead of System.in
            807589
            phdk wrote:
            the api will tell you if that is possible.
            [http://java.sun.com/javase/6/docs/api/java/io/DataInputStream.html]
            This does not really help the OP since the problem is how to convert the String to an InputStream. Since the OP would need to be storing binary data in a String (a no-no) and having looked at the OP's previous posts I am not convinced he needs a DataInputStream. I suspect the OP is just trying to parse numbers in a String and hopes that DataInputStream will do it for him.
            • 3. Re: How do I use string instead of System.in
              masijade
              htats wrote:
              How do I use the string instead of a System.in

              my code is like this.

              DataInputStream console = null

              console = new DataInputStream(System.in);

              I want to send a String instead of the System.in

              Can anyone help me out please.

              Cheers, San
              console = new DataInputStream(new ByteArrayInputStream(stringVar.getBytes()));
              • 4. Re: How do I use string instead of System.in
                masijade
                sabre150 wrote:
                phdk wrote:
                the api will tell you if that is possible.
                [http://java.sun.com/javase/6/docs/api/java/io/DataInputStream.html]
                This does not really help the OP since the problem is how to convert the String to an InputStream. Since the OP would need to be storing binary data in a String (a no-no) and having looked at the OP's previous posts I am not convinced he needs a DataInputStream. I suspect the OP is just trying to parse numbers in a String and hopes that DataInputStream will do it for him.
                Probably all true, but, I just gave him what he asked for. You know what they say "Be careful what you ask for, you just might get it!"

                ;-)
                • 5. Re: How do I use string instead of System.in
                  807589
                  well I just want to get a string from say String connect = "yes";

                  instead of inputing it from the user from the System.in console

                  so that way I can just send a string over instead of fetching it from the user.

                  cheers,

                  San
                  • 6. Re: How do I use string instead of System.in
                    masijade
                    htats wrote:
                    well I just want to get a string from say String connect = "yes";

                    instead of inputing it from the user from the System.in console

                    so that way I can just send a string over instead of fetching it from the user.

                    cheers,

                    San
                    Well, it might help to now in what context you mean "so that way I can just send a string over".

                    What exactly is it you are trying to do?

                    Edit: mulitpost/continuation of [this thread|http://forums.sun.com/thread.jspa?threadID=5348573&tstart=0].

                    @OP please keep the conversation there.

                    Edit Again: Oops. Nevermind, keep it here. The other thread is now closed.
                    • 7. Re: How do I use string instead of System.in
                      masijade
                      What are you attempting to use the "DataInputStream" for? If it is for socket communication (as the other thread seemed to indicate) why don't you post that socket code and we can probably give you a better way to do this. The DataInputStream bit is probably completely unneccessary, and nothing but an additional source of problems.
                      • 8. Re: How do I use string instead of System.in
                        807589
                        I want to use the String connect = "yes" and send that over to the client
                        instead of getting the input from the System.in

                        correct me if I'm wrong but will the System.in get the input form the user on the console?

                        But I can't since it will only construct with the InputStream as the parameter in

                        DataInputStream(InputStream) as in DataInputStream(System.in);
                        • 9. Re: How do I use string instead of System.in
                          masijade
                          And like I said, WHAT THE BLANK DO YOU WANT TO USE THE DataInputStream FOR!?

                          What you are saying right now is completely incomprehensible, because we have no idea what you want to use the DataInputStream for. What exactly are you trying to accomplish (after this step).

                          If you are simply going to keep repeating what you have already said, then see my earlier post with the ByteArrayInputStream and just go away.
                          • 10. Re: How do I use string instead of System.in
                            807589
                            Client.java

                            public void start() throws IOException
                            {       
                            console = new DataInputStream(System.in);//new ByteArrayInputStream(stringVer.getBytes()));
                            streamOut = new DataOutputStream(socket.getOutputStream());
                            if (thread == null)
                            {  client = new ChatClientThread(this, socket);
                            thread = new Thread(this);
                            thread.start();
                            }
                            }


                            ClientThread.java

                            private DataInputStream streamIn = null;


                            ....


                            public void open()
                            {  try
                            {  streamIn  = new DataInputStream(socket.getInputStream());
                            }
                            catch(IOException ioe)
                            {  System.out.println("Error getting input stream: " + ioe);
                            client.stop();
                            }
                            ...


                            public void run()
                            {  while (true)
                            {  try
                            {  client.handle(streamIn.readUTF());
                            }
                            catch(IOException ioe)
                            {  System.out.println("Listening error: " + ioe.getMessage());
                            client.stop();
                            }
                            }
                            }


                            I'm trying to write the String inputed from the System.in to the client
                            using DataInputStream
                            • 11. Re: How do I use string instead of System.in
                              masijade
                              Okay, I see where you are defining "console", but neither were you are declaring it, nor where you are using it. So I still have no idea how you are using the DataInputStream.

                              In any case, it all seems to be bad design. It seems as though the same classes that do the transmits, also does the reads, and so on and so forth, with no clear "separation of purpose" (to put it one way), and that your problem arises from this.

                              One way to do it would be to have a class that sends what it gets from a queue, another class that places items on the queue. That way you can one class that can read from system.in and place things on the queue, and another class that gets things to send from another source, and places them on the queue, and so on and so forth. With that separation you no longer need to even attempt to create a DataInputStream from a String. That would not have served your purpose anyway, because as soon as it sent a message it would stop the client as the InputStream would be empty causing an EOF exception (if I remember right), which is an extension of IOException, which would close the client.
                              • 12. Re: How do I use string instead of System.in
                                807589
                                import java.net.*;
                                import java.io.*;
                                import javax.swing.*;

                                public class ChatClient extends JFrame implements Runnable
                                {  private Socket socket              = null;
                                private Thread thread = null;
                                private DataInputStream console = null;
                                private DataOutputStream streamOut = null;
                                private ChatClientThread client = null;
                                private String prodID = "CUSCarissa_Calton25242526";

                                public ChatClient(String serverName, int serverPort)
                                {  System.out.println("Establishing connection. Please wait ...");
                                try
                                {  socket = new Socket(serverName, serverPort);
                                System.out.println("Connected: " + socket);
                                start();
                                }
                                catch(UnknownHostException uhe)
                                {  System.out.println("Host unknown: " + uhe.getMessage()); }
                                catch(IOException ioe)
                                {  System.out.println("Unexpected exception: " + ioe.getMessage()); }
                                }
                                public void run()
                                {  while (thread != null)
                                {  try
                                {  streamOut.writeUTF(prodID + " " + console.readLine()+" "+InetAddress.getLocalHost().getHostName()+ " " + InetAddress.getLocalHost().getHostAddress());
                                streamOut.flush();
                                }
                                catch(IOException ioe)
                                {  System.out.println("Sending error: " + ioe.getMessage());
                                stop();
                                }
                                }
                                }
                                public void handle(String msg)
                                {        int aSuc = 0;     
                                char[] aChar = msg.toCharArray();
                                boolean msg0 = false;

                                if (msg.equals(".bye"))
                                {  System.out.println("Good bye. Press RETURN to exit ...");
                                stop();
                                }
                                     
                                if (msg.equals("connect"))
                                {  System.out.println("Connected!");
                                //start WONCUS1 here
                                }
                                     

                                for(int a=0;a<msg.length();a++)
                                {
                                     if(aChar[a]=='m'&&aChar[a+1]=='s'&&aChar[a+2]=='g')
                                     {
                                          msg0 = true;
                                          aSuc = a+2;
                                          aSuc++;
                                          break;
                                     }
                                     else
                                     {
                                          aSuc = 0;
                                     }
                                }
                                char [] newy = msg.toCharArray();
                                String mStr = msg.substring(0,2);      
                                     
                                if(msg0==true)
                                {
                                     if(newy[0]=='0'||newy[0]=='1'||newy[0]=='2'||
                                     newy[0]=='3'||newy[0]=='4'||newy[0]=='5'||newy[0]=='6'||
                                     newy[0]=='7'||newy[0]=='8'||newy[0]=='9')
                                     {
                                          JOptionPane.showMessageDialog(null,msg.substring(aSuc,msg.length()));      msg0=false;
                                     }
                                }
                                if(msg.equals("pass"))System.out.println(msg);
                                     

                                }
                                public void start() throws IOException
                                {  console   = new DataInputStream(System.in);
                                streamOut = new DataOutputStream(socket.getOutputStream());
                                if (thread == null)
                                {  client = new ChatClientThread(this, socket);
                                thread = new Thread(this);
                                thread.start();
                                }
                                }
                                public void stop()
                                {  if (thread != null)
                                {  thread.stop(); 
                                thread = null;
                                }
                                try
                                {  if (console   != null)  console.close();
                                if (streamOut != null) streamOut.close();
                                if (socket != null) socket.close();
                                }
                                catch(IOException ioe)
                                {  System.out.println("Error closing ..."); }
                                client.close();
                                client.stop();
                                }
                                public static void main(String args[])
                                {  ChatClient client = null;
                                client = new ChatClient("localhost", 444);
                                }
                                }

                                /*
                                Today I meet Cyana at the very student present in the morning at Epping Station this morning today is the 4th Nov 2008. Around about 8am.
                                She was sitting facing East with her legs crossed when two of her friends. She had a little sprial binded a4 book and she was studing with her
                                friends with their own books. She seems to be not wanting to study though. But I saw her she had a little thick glasses like Karina I think but she looked
                                so good. I loved everything about her her hair her glasses and her legs they were so good looking. But the special things was that I was attracted to her like hell
                                man. You gotta to know how I feel man. I loved her when I remember her sitting there.
                                */


                                /////////////////////////////////////////////











                                import java.net.*;
                                import java.io.*;

                                public class ChatClientThread extends Thread
                                {  private Socket           socket   = null;
                                private ChatClient client = null;
                                private DataInputStream streamIn = null;

                                public ChatClientThread(ChatClient client, Socket socket)
                                {  client   = _client;
                                socket = _socket;
                                open();
                                start();
                                }
                                public void open()
                                {  try
                                {  streamIn  = new DataInputStream(socket.getInputStream());
                                }
                                catch(IOException ioe)
                                {  System.out.println("Error getting input stream: " + ioe);
                                client.stop();
                                }
                                }
                                public void close()
                                {  try
                                {  if (streamIn != null) streamIn.close();
                                }
                                catch(IOException ioe)
                                {  System.out.println("Error closing input stream: " + ioe);
                                }
                                }
                                public void run()
                                {  while (true)
                                {  try
                                {  client.handle(streamIn.readUTF());
                                }
                                catch(IOException ioe)
                                {  System.out.println("Listening error: " + ioe.getMessage());
                                client.stop();
                                }
                                }
                                }
                                }


                                ///////////////////////////





                                import java.net.*;
                                import java.io.*;

                                public class ChatServerThread extends Thread
                                {  private ChatServer       server    = null;
                                private Socket socket = null;
                                private int ID = -1;
                                private DataInputStream streamIn = null;
                                private DataOutputStream streamOut = null;

                                public ChatServerThread(ChatServer server, Socket socket)
                                {  super();
                                server = _server;
                                socket = _socket;
                                ID = socket.getPort();
                                }
                                public void send(String msg)
                                {   try
                                {  streamOut.writeUTF(msg);
                                streamOut.flush();
                                }
                                catch(IOException ioe)
                                {  System.out.println(ID + " ERROR sending: " + ioe.getMessage());
                                server.remove(ID);
                                stop();
                                }
                                }
                                public int getID()
                                {  return ID;
                                }
                                public void run()
                                {  System.out.println("Server Thread " + ID + " running.");
                                while (true)
                                {  try
                                {  server.handle(ID, streamIn.readUTF());
                                }
                                catch(IOException ioe)
                                {  System.out.println(ID + " ERROR reading: " + ioe.getMessage());
                                server.remove(ID);
                                stop();
                                }
                                }
                                }
                                public void open() throws IOException
                                {  streamIn = new DataInputStream(new
                                BufferedInputStream(socket.getInputStream()));
                                streamOut = new DataOutputStream(new
                                BufferedOutputStream(socket.getOutputStream()));
                                }
                                public void close() throws IOException
                                {  if (socket != null)    socket.close();
                                if (streamIn != null) streamIn.close();
                                if (streamOut != null) streamOut.close();
                                }
                                }


                                /////////////////////////////////////













                                //hit you from the side and in the middle and make you wanna ride away.
                                //

                                import java.net.*;
                                import java.io.*;

                                public class ChatServer implements Runnable
                                {  private ChatServerThread clients[] = new ChatServerThread[50];
                                private ServerSocket server = null;
                                private Thread thread = null;
                                private int clientCount = 0;

                                public ChatServer(int port)

                                {
                                String Wisdom = "Wisdom did this to 911 0-pp";
                                try
                                {  System.out.println("Initialising San's Server  ..."+"\n"+"Binding to port " + port + ", please wait  ...");
                                server = new ServerSocket(port);
                                System.out.println("Server started: " + server);
                                start(); }
                                catch(IOException ioe)
                                {  System.out.println("Can not bind to port " + port + ": " + ioe.getMessage()); }
                                }
                                public void run()
                                {  while (thread != null)
                                {  try
                                {  System.out.println("Waiting for a client ...");
                                addThread(server.accept()); }
                                catch(IOException ioe)
                                {  System.out.println("Server accept error: " + ioe); stop(); }
                                }
                                }
                                public void start() {
                                     int wisdom = 1;

                                     if(thread == null)
                                     {
                                          thread = new Thread(this);thread.start();
                                     }

                                               }
                                public void stop() {
                                if(thread !=null)
                                {     //client = new ChatClientThread(this, socket);
                                     thread.stop();thread =null;
                                }
                                }
                                private int findClient(int ID)
                                {  for (int i = 0; i < clientCount; i++)
                                if (clients.getID() == ID)
                                return i;
                                return -1;
                                }
                                public synchronized void handle(int ID, String input)
                                {  if (input.equals(".bye"))
                                {  clients[findClient(ID)].send(".bye");
                                remove(ID); }
                                else
                                for (int i = 0; i < clientCount; i++)
                                clients[i].send(ID + ": " + input);
                                }
                                public synchronized void remove(int ID)
                                {  int pos = findClient(ID);
                                if (pos >= 0)
                                {  ChatServerThread toTerminate = clients[pos];
                                System.out.println("Removing client thread " + ID + " at " + pos);
                                if (pos < clientCount-1)
                                for (int i = pos+1; i < clientCount; i++)
                                clients[i-1] = clients[i];
                                clientCount--;
                                try
                                {  toTerminate.close(); }
                                catch(IOException ioe)
                                {  System.out.println("Error closing thread: " + ioe); }
                                toTerminate.stop(); }
                                }
                                private void addThread(Socket socket)
                                {  if (clientCount < clients.length)
                                {  System.out.println("Client accepted: " + socket);
                                clients[clientCount] = new ChatServerThread(this, socket);
                                try
                                {  clients[clientCount].open();
                                clients[clientCount].start();
                                clientCount++; }
                                catch(IOException ioe)
                                {  System.out.println("Error opening thread: " + ioe); } }
                                else
                                System.out.println("Client refused: maximum " + clients.length + " reached.");
                                }
                                public static void main(String args[]) {     ChatServer server = new ChatServer(Integer.parseInt(args[0]));}
                                }






                                ///////////////////////////////


                                And that's the code to clarify your image of it all.

                                And also can I use a file to replace the System.in since it is also a InputStream the file I meant and then I could just write the text I wanted in the file for sending over the socket?

                                Cheer,
                                San
                                • 13. Re: How do I use string instead of System.in
                                  PhHein
                                  Please use the code tags when posting code. There is a code button above the message editor that inserts the tags for you.
                                  • 14. Re: How do I use string instead of System.in
                                    masijade
                                    You can give DataInputStream any InputStream you want, but that (and the mess you just posted) doesn't change anything I said in my last post.
                                    1 2 Previous Next