java - How do I modify this Client class to read input from an array of Strings (rather than from standard input)? -


i have following code, in java client class ( it's knockknock client/server pair ) :

   try {       kksocket = new socket("localhost", 4444);       out = new printwriter(kksocket.getoutputstream(), true);       in =  kkjokes[0]; //new bufferedreader(new inputstreamreader(kksocket.getinputstream()));       // in2 = new bufferedreader(new inputstreamreader(kksocket.getinputstream()));     } 

i want have automatically read string array (that contains predefined jokes ) , :

       string kkjokes[] =  {"who's there?", "turnip who?",             "y", "who's there?", "who's there?", "blah" };         /* more cod */        try {           kksocket = new socket("localhost", 4444);           out = new printwriter(kksocket.getoutputstream(), true);         (int = 0; i< kkjokes.length; i++) {           in =  new bufferedreader( new inputstreamreader( kkjokes[i] )) ;         }       } 

but doesn't work, gives :

knockknockclientredux.java:33: non-static variable kkjokes cannot referenced static context       (int = 0; i< kkjokes.length; i++) {                          ^ knockknockclientredux.java:34: non-static variable kkjokes cannot referenced static context       in =  new bufferedreader( new inputstreamreader( kkjokes[i] )) ; //new buf feredreader(new inputstreamreader(kksocket.getinputstream()));                                                        ^ knockknockclientredux.java:34: cannot find symbol symbol  : constructor inputstreamreader(java.lang.string) location: class java.io.inputstreamreader       in =  new bufferedreader( new inputstreamreader( kkjokes[i] )) ; //new buf feredreader(new inputstreamreader(kksocket.getinputstream())); 

edit: wouldn't easier read text file? maybe i'll list knocknock joke lines vertically in text file. way, seems easier swap in filereader kksocket.getinputstream() code

the compiler message tells all. try

static string kkjokes[] =  {"who's there?", "turnip who?",         "y", "who's there?", "who's there?", "blah" }; 

the second problem can solved with:

new inputstreamreader( new bytearrayinputstream(kkjokes[i].getbytes() )  

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -