• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please help

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*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;
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
nadia fariss
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
reply
    Bookmark Topic Watch Topic
  • New Topic