Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi i need some help on my client/server lottery prog

Status
Not open for further replies.

GlenA

Programmer
Apr 21, 2003
24
GB
Hi, i've been helping my younger brother to write this pretty simple client/server lottery program in Java for his Uni's coursework,, but since i dont program in java, i got a little problem.... Basically i've got my client done and only got a little problem with my server. The client should get user input for 6 numbers, then store them in a string and pass to the server through a socket connection, a server then generates 6 random numbers, compares them with recie3ved string and sends the result back to client. Well i get user input, store the files in the string, pass them to the server, grnerate random numbers, but the problem i have is that i cant compare the 2. I could just compare 2 strings but that wouldn't make sence for the lottery application. What i think i need is to extract integers from the string that was sent from the client and compare each integer with random numbers.

This is the user input from the client. The server recieves the string "guesses":

for (int i=1;i<=6;i++) {
String input = JOptionPane.showInputDialog(message);

int guess = Integer.parseInt(input);

guesses += guess + &quot; &quot;;

}

Now here's the servers bit, it generates numbers and then i need to compare them, but i dont know how... Please help.

for (int i=1;i<=6;i++) {

//////Here i need to convert recieved &quot;guesses&quot; string into integers//////////

int number = randomInteger(0,99);

count += ((whatever we gonna call the extracted inegers) == number) ? 1 : 0;

numbers += number + &quot; &quot;;


}


Thank you
 
You can use StringTokenizer to break the guesses string up into individual strings.

After that, check out Integer.parseInt() to make ints.
 
Thanks alot for your reply.

You see i dont code in Java at all and i wrote most of this prog following the book. Could you please clear it up for me.

So i pass the string &quot;guesses&quot; which holds 6 integers to the server the server generates 6 random numbers and before it puts the numbers into the string i want it to campare the recieved numbers with the newly generated.

So in this loop i need to get the integers out of the string &quot;guesses&quot;. Is it possible to do using the same principal as int guesses = Integer.parseInt(input); but instead of getting values from the input get them out of the string?

for (int i=1;i<=6;i++) {

some conversion here

int number = randomInteger(0,99);

count += ((whatever we gonna call the extracted inegers) == number) ? 1 : 0;

numbers += number + &quot; &quot;;


}


Thank you
 
Yes, you have the right idea. The StringTokenizer will chop up the long string of guesses and feed them to you one guess at a time, each one a String. Then you just convert each String to an int:

Code:
   StringTokenizer st = new StringTokenizer(guesses);
   while (st.hasMoreTokens())
   {
      String singleNumber = st.nextToken();
      int guess = Integer.parseInt(singleNumber);
      // You can now compare guess to whatever...
   }

Hope this helps.
 
Thank a lot!!! Great Help!!! All Works now!!! Thanks!!!
 
Opps, not all of it. Sorry for being so dumb and wasting your time but i still have one last question... After compilation i get the following error:

--------------------Configuration: java <Default>--------------------
C:\Program Files\Xinox Software\JCreator Pro\MyProjects\bbb\TCPServer.java:62: cannot resolve symbol
symbol : variable guess
location: class TCPServer
count += (guess == number) ? 1 : 0;
^
C:\Program Files\Xinox Software\JCreator Pro\MyProjects\bbb\TCPServer.java:65: cannot resolve symbol
symbol : variable guess
location: class TCPServer
somenum += guess + &quot; &quot;;
^
2 errors

Process completed.

---------------------------------------------------

Here's the code of the whole server side, can you pleas point me to the error, i've tried a lot of different things but it just refuses to work...


Thank you again.

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.StringTokenizer;

class TCPServer {

public static int randomInteger(int lowerLimit,int upperLimit) {
return(lowerLimit+(int)((upperLimit-lowerLimit)*Math.random()));
}

public static void displayMessage(String message) {
JOptionPane.showMessageDialog(null,message,&quot;Results&quot;,JOptionPane.INFORMATION_MESSAGE);
}

public static void main(String argv[]) throws Exception
{

String somenum = &quot;&quot;;
String clientNumbers;
String numbers = &quot;&quot;;
String message = &quot;hi\n&quot;;
message += &quot;Enter a number(Between 0 and 99)&quot;;
int count=0;
displayMessage(message);
ServerSocket welcomeSocket = new ServerSocket(6789);

while(true) {

Socket connectionSocket = welcomeSocket.accept();

BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());

clientNumbers = inFromClient.readLine();



for (int i=1;i<=6;i++) {





StringTokenizer st = new StringTokenizer(clientNumbers);

while (st.hasMoreTokens())

{
String singleNums = st.nextToken();

int guess = Integer.parseInt(singleNums);

}

int number = randomInteger(0,99);


count += (guess == number) ? 1 : 0;

numbers += number + &quot; &quot;;
somenum += guess + &quot; &quot;;


}

displayMessage(message);

System.out.println(&quot;some shit here&quot; + (count == 0));

message += (count == 0) ? &quot;, dummy!&quot; : &quot;!&quot;;



System.out.println(&quot;FROM CLIENT: &quot; + clientNumbers + somenum +numbers);


outToClient.writeBytes(numbers +'\n');



}
}
}


 
The problem is that you defined
Code:
guess
in your while loop :

Code:
 while (st.hasMoreTokens())
   
   {
      String singleNums = st.nextToken();
       
     //int guess is only know inside here
     int guess = Integer.parseInt(singleNums);
       
         }

What this means is that
Code:
guess
is only know inside the brackets.

try defining guess in you main...something like this:

Code:
class TCPServer {

public static int randomInteger(int lowerLimit,int upperLimit) {
      return(lowerLimit+(int)((upperLimit-lowerLimit)*Math.random()));
   }

 public static void displayMessage(String message) {
      JOptionPane.showMessageDialog(null,message,&quot;Results&quot;,JOptionPane.INFORMATION_MESSAGE);
   }

    public static void main(String argv[]) throws Exception
    {

        String somenum = &quot;&quot;;
      String clientNumbers;
      String numbers = &quot;&quot;;
       String message = &quot;hi\n&quot;;
       message += &quot;Enter a number(Between 0 and 99)&quot;;
      int count=0;
     displayMessage(message);
      ServerSocket welcomeSocket = new ServerSocket(6789);
      int guess = 0;

      while(true) {

                   Socket connectionSocket = welcomeSocket.accept();

           BufferedReader inFromClient =
             new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

           DataOutputStream  outToClient =
             new DataOutputStream(connectionSocket.getOutputStream());

           clientNumbers = inFromClient.readLine();



          for (int i=1;i<=6;i++) {





                   StringTokenizer st = new StringTokenizer(clientNumbers);

   while (st.hasMoreTokens())

   {
      String singleNums = st.nextToken();

     guess = Integer.parseInt(singleNums);

         }

        int number = randomInteger(0,99);


         count += (guess == number) ? 1 : 0;

         numbers += number + &quot; &quot;;
         somenum += guess + &quot; &quot;;


      }

 displayMessage(message);

     System.out.println(&quot;some shit here&quot; + (count == 0));

      message += (count == 0) ? &quot;, dummy!&quot; : &quot;!&quot;;



          System.out.println(&quot;FROM CLIENT: &quot; + clientNumbers + somenum +numbers);


           outToClient.writeBytes(numbers +'\n');



        }
    }
}
[code]

Hope this helps.
 
Great Help you guys!!!!!!!!



Thank you very much!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top