【问题标题】:What is the error in java code?java代码中的错误是什么?
【发布时间】:2015-02-01 09:41:20
【问题描述】:

代码包含4个方法 Dir 是构造函数 addDetails 是获取公司详细信息, 该特定公司的联系电话和评级 并将其分别存储在数组 compName、compNumber 和 Rate
分别是String数组、Int数组和String数组。

方法 getDetails 将名称作为输入并打印详细信息 该特定公司的输出

类似地 getContact 获取公司名称并提供联系电话

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

【问题讨论】:

  • compName.length() 应该是 compName.length 因为它是一个数组
  • 你问“什么是错误”。编译器错误消息告诉您。不用在这里问。只需阅读编译器的错误消息。如果您不明白,请发布。
  • 错误在 .length() 方法中
  • 顺便说一句,除了这些错误之外,您还有一个隐藏的name2==compName[i]

标签: java string


【解决方案1】:

这一行:

if(name2==compName[i])

不会像您认为的那样有效。不要将字符串与== 进行比较,而是使用equals()

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

见:How do I compare strings in Java?

【讨论】:

    【解决方案2】:

    首先,length() 方法用于找出字符串的长度,而不是数组的长度。

    你应该使用length 不带括号来找出数组的长度,像这样 - compName.length

    那么方法getContact应该是String,像这样-

    public String getContact() ...
    

    编辑

    另外,正如二支铅笔所指出的,name1==compName[i] 应该是name1.equals(compName[i]),因为它是一个字符串比较。

    【讨论】:

      【解决方案3】:

      当你比较字符串时,你应该使用 equals() 方法而不是 ==。

      更多信息请点击链接。

      Compare String

      【讨论】:

        【解决方案4】:

        你这样声明数组

        字符串名称[] = 新字符串[5788];

        但必须是

        字符串[] 名称 = 新字符串[5788];

        【讨论】:

        • 我不认为这是问题:)
        • 第二种方式是在 Java 中声明数组的首选方式,但第一种方式也可以。
        猜你喜欢
        • 1970-01-01
        • 2017-04-03
        • 2011-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-23
        相关资源
        最近更新 更多