-5

The code contains 4 methods Dir is the constructor addDetails is to get company Details , contact number and ratings for that particular company and store it in arrays compName,compNumber and Rate respectively
which are String array,Int array and String array respectively.

Method getDetails takes a name as input and prints details of that particular company as output

Similarly getContact takes company name and gives contact number

public class Dir extends UnicastRemoteObject implements DirInterface {

    Scanner sc=new Scanner(System.in);
    public String compName[]=new String[50];
    public int compNumber[]=new int[50];
    public String Rate[]=new String[50];

    public static int k=0;

    public Dir() throws RemoteException {
        super();
    }

    public void addDetails() {
    System.out.println("Input  company Details to add");
    compName[k]=sc.nextLine();

    System.out.println("input contact number of company");
    compNumber[k]=sc.nextInt();

    System.out.println("enter out of 5 for rating");
    Rate[k]=sc.next();

    k++;
}

public String getDetails(){
    System.out.println("Input company name to get its details");
    String name2=sc.next();

    for(int i=0;i<=(compName.length());i++){ 
        if(name2==compName[i])
            return compName[i]+" "+compNumber[i]+" "+Rate[i]+" rating";
    } 
}

public int getContact(){
    System.out.println("Enter name of the company to get contact number");
    String name1=sc.nextLine();
    for(int i=0;i<=compName.length();i++){
        if(name1==compName[i])
            return compNumber[i];
    } 
}
}

Errors:error ';' expected Dir() throws  RemoteException
cannot find symbol for(int i=0;i<=compName.length();i++)
symbol:method.length

error:incompatible types
location:return compNumber
required int found String

************SOLVED ERRORS OF THE ABOVE CODE*************



public class Client{
public static void main(String args[]){
try{
DirInterface st=(DirInterface)Naming.lookup("rmi://"+args[0]+"/AddService");   

st.addDetails();

String det=st.getDetails();
System.out.println(det);

String xx=st.getContact();
System.out.println(xx);

}catch(Exception e){

System.out.println(e);
}

}


ERRORS:In Client class
Illegal start of type try{
expected ';'

Identifier expected 
st.addDetails();
sopln(det);

catch}(Exception e){}
class,interface or enum expected
André Schild
  • 4,592
  • 5
  • 28
  • 42

4 Answers4

1

Firstly, the method length() is used to find out the length of a String, not of an array.

You should use length without brackets to find out the length of array, like this - compName.length

Then the method getContact should be String, like this -

public String getContact() ...

EDIT

Also, as pointed out by Chief Two Pencil, name1==compName[i] should be name1.equals(compName[i]) as it's a String comparison.

Confuse
  • 5,646
  • 7
  • 36
  • 58
1

This line:

if(name2==compName[i])

will not work like you think it works. Do not compare strings with ==, use equals() instead:

if(name2.equals(compName[i]))

See: How do I compare strings in Java?

Community
  • 1
  • 1
Jesper
  • 202,709
  • 46
  • 318
  • 350
0

When you are comparing the string you should use equals() method instead of ==.

For More Information follow the link.

Compare String

Jaydeep
  • 1
  • 4
-3

You declared the arrays like this

String name[] = new String[5788];

but is has to be

String[] name = new String[5788];

lukas81298
  • 51
  • 1
  • 1
  • 6