wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "please help" Watch "please help" New topic
Author

please help

nadia fariss
Greenhorn

Joined: Oct 09, 2003
Posts: 3
/*I am tryin to add or compare a suspect to a list of suspect in the server
what I keep gettin is
SuspectClient.java:67: cannot resolve symbol
symbol : variable sock
location: class SuspectClient
ObjectOutputStream oos = new ObjectOutputStream( sock.getOutputStream() );
^
can anyone help me thank you*/

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.util.ArrayList;
public class SuspectClient {
public static void main( String[ ] args ) {
if ( args.length < 1 ) {
System.err.println( "DateClient <IP address of server>" );
return;
}
Socket sock = new Socket( "127.0.0.1", 8951 );
ObjectInputStream ois = new ObjectInputStream( sock.getInputStream() );
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream() );
oos.writeObject("Can I have the date?");
// oos.flush();
String choice = (String) ois.readObject();
do {
choice = JOptionPane.showInputDialog("Enter :\n"
+ " add: to Add a suspect to a list\n"
+ " compare: to Compare a suspect to all suspects\n"
+ " Show all: to Show all the suspect in the list\n"
+ "quit to Quit");
if (choice.equalsIgnoreCase("add"))
doAdd();
/*else if (choice.equalsIgnoreCase("compare"))
doCompare();*/
}while( choice.equalsIgnoreCase("quit") == false);
sock.close();

}
public static void doAdd(){
String name = JOptionPane.showInputDialog( "Enter Suspect name: ");
String sheight = JOptionPane.showInputDialog( "Enter Suspect Height: ");
String sage = JOptionPane.showInputDialog( "Enter Suspect Age: ");
String eyeColor = JOptionPane.showInputDialog( "Enter Suspect Eye color: ");
int height = Integer.parseInt(sheight);
int age = Integer.parseInt(sage);
Suspect s =new Suspect(height,age,eyeColor);
s.setName(name);
//ObjectOutputStream oos = new ObjectOutputStream( sock.getOutputStream() );
oos.writeObject(s);
oos.flush();
//oos.reset();
}
/*public static void doCompare(){
String name = JOptionPane.showInputDialog( "Enter Suspect name: ");
String sheight = JOptionPane.showInputDialog( "Enter Suspect Height: ");
String sage = JOptionPane.showInputDialog( "Enter Suspect Age: ");
String eyeColor = JOptionPane.showInputDialog( "Enter Suspect Eye color: ");
int height = Integer.parseInt(sheight);
int age = Integer.parseInt(sage);
Suspect another = new Suspect(height,age,eyeColor);
another.setName(name);
//ObjectOutputStream oos = new ObjectOutputStream( sock.getOutputStream() );
oos.writeObject(another);*/
//}
*/
}
class Suspect implements Serializable
{
private String name;
private int height;
private int age;
private String eyeColor;
//public Setter
public void setName( String name){
this.name = name;
}
public void setHeight( int height){
this.height = height;
}
public void setAge( int age){
this.age = age;
}
public void setEyeColor( String eyeColor){
this.eyeColor = eyeColor;
}
//public getter
public String getName() {
return name;
}
public int getHeight(){
return height;
}
public int getAge(){
return age;
}
public String getEyeColor() {
return eyeColor;
}
public String toString(){
return "Name: " + name + "\t Height: " + height + "\t Age: " + age + "\t Eye Color: " + eyeColor +"\n" ;
}
public boolean validSuspect (Suspect s){
if( (age == s.age) && (height == s.height) && (eyeColor.equalsIgnoreCase(s.eyeColor)) )
return true;
else
return false;
}
//constructor
public Suspect( int h , int a, String e) {
height=h;
age=a;
eyeColor=e;
}
public Suspect( String n) {
name = n;
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,
Welcome to JavaRanch!
Is line 67 the commented-out line? (I think so).
That line is in the method doAdd(), but the variable sock is a local variable in the method main(). Methods can't see each other's local variables (this is a pretty basic Java question.) If doAdd() needs to use sock, then main() should pass it as an argument to that routine -- i.e.,

OK?


[Jess in Action][AskingGoodQuestions]
nadia fariss
Greenhorn

Joined: Oct 09, 2003
Posts: 3
/*thank you very much for replying so fast
I am a java beginner and I tried to declare the socket in the main but I got this error message.
SuspectClient.java:54: cannot resolve symbol
symbol : variable oos
location: class SuspectClient
oos.writeObject(s);
*/
//here the change that I made
public class SuspectClient {
public static void main( String[ ] args ) {
if ( args.length < 1 ) {
System.err.println( "DateClient <IP address of server>" );
Socket sock = new Socket( "127.0.0.1", 8951 );
ObjectInputStream ois = new ObjectInputStream( sock.getInputStream() );
ObjectOutputStream oos = new ObjectOutputStream( sock.getOutputStream() );
return;
}
//the rest
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, yes, indeed. You've still got that line that defines the "oos" variable in doAdd() commented out, so there's no such variable in that routine. Again, if you want the routines to share an ObjectOutputStream, then pass it around as an argument.
It doesn't make much sense for your to try to write a program at this level without a basic understanding of variables and scopes. Why not sit down and work through the Java Tutorial, especially the first four lessons. You'll be in much better shape to work on this program afterwards.
nadia fariss
Greenhorn

Joined: Oct 09, 2003
Posts: 3
I am still unable to get my code to compile without error
even after addding the code doAdd(sock)
still the same error
"SuspectClient.java:55: cannot resolve symbol
symbol : variable oos
location: class SuspectClient
oos.writeObject(s);
"
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Yes, because as I said, the line that declares it in doAdd() is commented out!
Please, please, don't try to write software without understanding the language. The world is full of too many people trying to do this. Please learn to walk before you try to run! Read and understand the tutorial, and you'll be able to solve this problem yourself, trivially!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please help