Need help on coding

Hi everyone!

currently i am doing a match making project (networking). my project is almost done, the only problem left is on the clientView side which when either button clicked, it will hang the program if char is input on the age text field. i am only managed to show a "not funny" msg when the input is in negative. hope any kind soul can help mi on the coding which will give an error msg and will not hang the program if a char or float is input on the text field. thks in advance:)

ServerModel.java

import java.net.*;

import java.io.*;

import java.util.*;

public class ServerModel

{

public static void main(String[] args)

{

new ServerModel();

}

ServerView sv = new ServerView();

public ServerModel()

{

try

{

ServerSocket ss = new ServerSocket(8000);

sv.jtaLog.append("Server Started .. waiting for Client\n");

System.out.println("Server started .. waiting for Client");

int clientNo = 1;

while(true)

{

Socket connectToClient = ss.accept();

// Print the connected client number on the console

System.out.println("Start thread for client " + clientNo);

//Create a new thread for connection

HandleClient thread = new HandleClient(connectToClient);

sv.jtaLog.append("Client "+clientNo+" connected!!!\n");

//Start thread

thread.start();

// Increment clientNo

clientNo++;

}

}

catch(IOException e){

sv.jtaLog.append("Server started already! \n"+e);

}

}

class HandleClient extends Thread

{

private Socket connectToClient;

public HandleClient(Socket socket)

{

connectToClient = socket;

}

//Run the thread

public void run()

{

try

{

BufferedReader brFromClient = new BufferedReader(new InputStreamReader(connectToClient.getInputStream()));

PrintWriter pwToClient = new PrintWriter(connectToClient.getOutputStream(),true);

while(true)

{

String btn = brFromClient.readLine();// read which button clicked

if(btn.equals("Register"))

{

// recieve 4 name, age , gender, country from client

String name = brFromClient.readLine();

int age = Integer.parseInt(brFromClient.readLine());//READ LINE FROM CLIENT N ASSIGN TO NAME

String gender = brFromClient.readLine();

String country = brFromClient.readLine();

if (age<=0)

{

pwToClient.println("Not funny");

}

else {

sv.jtaLog.append("New customer registered!!\n"+name+"\n"+age+"\n"+gender+"\n"+country+"\n");

PrintWriter bw = new PrintWriter(new FileWriter("Candidate.txt", true)); //write into text file

//String success = name+"#"+age+"#"+gender+"#"+country+;

bw.println(name+"#"+age+"#"+gender+"#"+country);//write on candidate.txt

//BufferedReader brFile = new BufferedReader(new FileReader("Candidate.txt"));

//String msg0;

//while((msg0= brFile.readLine())!=null)

//bw.println(success);

bw.close();

pwToClient.println("Registered successfully!!");

}

}// end if

else {

int countFound=0;

BufferedReader brFile = new BufferedReader(new FileReader("Candidate.txt"));//READ FROM .TXT

int age = Integer.parseInt(brFromClient.readLine());//READ LINE FROM CLIENT N ASSIGN TO NAME

String gender = brFromClient.readLine();

String country = brFromClient.readLine();

String msg1;//ASSIGN TO MSG

while((msg1= brFile.readLine())!=null)

{

pwToClient.println("Continue Search");

StringTokenizer st = new StringTokenizer(msg1,"#");

String nameT=st.nextToken();//BREAK N ASSIGN

int ageT= Integer.parseInt(st.nextToken());

String genderT=st.nextToken();

String countryT=st.nextToken();

if((age == ageT) && gender.equals(genderT) && country.equals(countryT))

{

pwToClient.println("Found");

pwToClient.println(nameT);

pwToClient.println(ageT);

pwToClient.println(genderT);

pwToClient.println(countryT);

countFound++;

sv.jtaLog.append("Customer searching for...\n"+age+"\n"+gender+"\n"+country+"\n");

}

else

pwToClient.println("Not Found Yet");

}// end while

pwToClient.println("End Search");

if (countFound==0)

pwToClient.println("Record Not Found");

else

pwToClient.println("Total Records Found: "+countFound);

}// end else

}// end while

}catch (NumberFormatException e) {}

catch (IOException ex) {}

}

}

}

clientView.java

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class ClientView extends JApplet

{

private JLabel jlblName = new JLabel("Name: (For Register only)");

private JLabel jlblAge = new JLabel("Age: ");

private JLabel jlblGender = new JLabel("Gender: ");

private JLabel jlblCountry = new JLabel("Country: ");

private JLabel jlbl = new JLabel("Online Match Make", JLabel.CENTER);

private JComboBox jcboGender = new JComboBox();

private JComboBox jcboCountry = new JComboBox();

private JTextField tfName = new JTextField(6);

private JTextField tfAge = new JTextField(2);

private JTextArea jta = new JTextArea(10,5);

private JButton btnSubmit = new JButton ("Submit");

private JButton btnRegister = new JButton ("Register");

private JButton btnClear = new JButton ("Clear");

private ClientModel cm=new ClientModel();

private String s1 ="F";

private String s2 ="Singapore";

public void init()

{

setLayout(new GridLayout(4,1,5,5));

JPanel jp = new JPanel();

jp.setLayout(new GridLayout(1,1));

jp.add(jlbl);

JPanel jp1 = new JPanel();

//add(new JLabel("Online Match Make", JLabel.CENTER),BorderLayout.NORTH);

//jp1.add(new JLabel("Online Match Make", JLabel.CENTER),BorderLayout.NORTH);

jp1.setLayout(new GridLayout(4,2,5,5));

jp1.add(jlblName);

jp1.add(tfName);

tfName.setEditable(false);

tfName.setText("");

jp1.add(jlblAge);

jp1.add(tfAge);

jp1.add(jlblGender);

jp1.add(jcboGender);

jcboGender.addItem("F");

jcboGender.addItem("M");

jp1.add(jlblCountry);

jp1.add(jcboCountry);

jcboCountry.addItem("Singapore");

jcboCountry.addItem("Malaysia");

jcboCountry.addItem("Hong Kong");

JPanel jp2 = new JPanel();

jp2.setLayout(new GridLayout(1,3,20,20));

jp2.add(btnSubmit);

jp2.add(btnRegister);

jp2.add(btnClear);

JPanel jp3 = new JPanel();

jp3.setLayout(new GridLayout(1,1,10,10));

jp3.add(new JScrollPane(jta));

//jp1.add(jta);

jlblName.setForeground(Color.RED);

jlblAge.setForeground(Color.BLUE);

jlblGender.setForeground(Color.BLUE);

jlblCountry.setForeground(Color.BLUE);

add(jp);

add(jp1);

add(jp2);

add(jp3);

btnSubmit.addActionListener(new Submit());

btnRegister.addActionListener(new Register());

btnClear.addActionListener(new Clear());

}

class Submit implements ActionListener

{

public void actionPerformed(ActionEvent e)

{// tells the server Submit button is clicked

String result="";String endOfSearch="";

cm.sendToServer("Submit");

// sends age, gender, country to server

cm.sendToServer(tfAge.getText());

cm.sendToServer(s1);

cm.sendToServer(s2);

s1= (String)jcboGender.getSelectedItem();

s2= (String)jcboCountry.getSelectedItem();

// gets result from server

while (true){

endOfSearch= cm.getFromServer(); //pwToClient.println("Continue Search");

if (endOfSearch.equals("End Search")) break;

result= cm.getFromServer();

if (result.equals("Found"))

{

String name = cm.getFromServer();

String age= cm.getFromServer();

String gender= cm.getFromServer();

String country= cm.getFromServer();

jta.append("Found"+"\n");

jta.append("Name: "+name+"\n");

jta.append("Age: "+age+"\n");

jta.append("Gender: "+gender+"\n");

jta.append("Country: "+country+"\n");

}

}//end while

jta.append(cm.getFromServer()+"\n");

}

}//end of submit

public class Clear implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

/*Clearing TextFields*/

tfName.setText("");

tfAge.setText("");

jta.setText("");

}

}

public class Register implements ActionListener

{

public void actionPerformed(ActionEvent e)

{// tells the server Register button is clicked

jlblName.setForeground(Color.RED);

jlblAge.setForeground(Color.RED);

jlblGender.setForeground(Color.RED);

jlblCountry.setForeground(Color.RED);

if(tfName.getText().equals(""))

{

tfName.setEditable(true);

tfAge.setText("");

jta.setText("");

jta.append("Pls enter both Name & Age\n");

}

else

{

if (tfAge.getText().equals(""))

{

jta.append("Pls enter age\n");

}

else{

cm.sendToServer("Register");

// sends name, age, gender, country to server

cm.sendToServer(tfName.getText());

cm.sendToServer(tfAge.getText());

s1= (String)jcboGender.getSelectedItem();

s2= (String)jcboCountry.getSelectedItem();

cm.sendToServer(s1);

cm.sendToServer(s2);

String msg = cm.getFromServer();

if (msg.equals("Not funny"))

{

jta.append("Not funny");

}

else{

jta.setText("");

jta.append("Registered successfully!!");

tfName.setText("");

tfName.setEditable(false);

jlblName.setForeground(Color.RED);

jlblAge.setForeground(Color.BLUE);

jlblGender.setForeground(Color.BLUE);

jlblCountry.setForeground(Color.BLUE);

}

}

}

}

}

}

Message was edited by:

aces1799

[10234 byte] By [aces1799a] at [2007-11-26 17:40:40]
# 1
What did you learn the last time you tried to just basically say "Do my work and thinking for me" here? http://forum.java.sun.com/thread.jspa?threadID=785948
warnerjaa at 2007-7-9 0:08:51 > top of Java-index,Java Essentials,Training...
# 2
jus shut up if u dont wanna help. i only asked for the part which i do not know, not the whole program,OK?!! hope at least some guide from anyone here, i am in the process of learning not listening to your nonsense!
aces1799a at 2007-7-9 0:08:51 > top of Java-index,Java Essentials,Training...
# 3
The pile of unformatted junk you posted won't be read by anyone so essentially you are requesting people to do it all for you.
jwentinga at 2007-7-9 0:08:51 > top of Java-index,Java Essentials,Training...
# 4

> jus shut up if u dont wanna help. i only asked for

> the part which i do not know, not the whole

> program,OK?!! hope at least some guide from anyone

> here, i am in the process of learning not listening

> to your nonsense!

Yes sir. Problem for you is, that everyone else is probably shutting up too.

Um, good luck with that sir.

P.S. Will it be another 12 hours or so before you come back with another snappy retort?

warnerjaa at 2007-7-9 0:08:51 > top of Java-index,Java Essentials,Training...