“方法**对于类型字符串是未定义的”在Eclipse中 [英] "the method ** is undefined for the type string" in Eclipse

查看:971
本文介绍了“方法**对于类型字符串是未定义的”在Eclipse中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在完成一个项目,正在尝试创建一个菜单。菜单本身工作正常,但是当我尝试并链接到方法本身所在的其他java文件时,我得到错误方法名称对于String类型是未定义的。以下是我用来尝试链接到其他java文件中的方法的代码。

  {
if(menuChoice.equals(A))
{
System.out.print(输入电影ID:);
movieID = sc.nextLine();
movieID.borrowMovie();
}
if(menuChoice.equals(a))
{
System.out.print(输入电影ID:);
movieID = sc.nextLine();
movieID.borrowMovie();
}
if(menuChoice.equals(B))
{
System.out.print(输入电影ID:);
movieID = sc.nextLine();
movieID.returnMovie();
}
if(menuChoice.equals(b))
{
System.out.print(输入电影ID:);
movieID = sc.nextLine();
movieID.returnMovie();
}

此外,以下是我尝试的2种方法的代码链接到其他java文件。项目要求有两个独立的类文件,并演示如何一起使用它们,但这部分让我失望了!

  //实现borrowMovie方法
public double borrowMovie(String memberID)
{
if(this.isOnLoan = false)
{
this.isOnLoan =真正;
}
else
{
return Double.NaN;
}
}

//实现returnMovie方法
public double returnMovie(int daysBorrowed)
{
if(this.isOnLoan = false )
{
return Double.NaN;
}
else
{
this.isOnLoan = false;

}
}

任何帮助都将不胜感激:

解决方案

看看你的代码,我想, movieID 一个 String



String 没有方法 borrowMovie() returnMovie(),所以你会从编译器得到一个告诉你的错误。 / p>

而不是 movieID.borrowMovie()你想要的: borrowMovie(movieID)

这是您的 borrowMovie() returnMovie( )方法:

  if(this.isOnLoan = false)

您将 false 的值分配给 this.isOnLoan 这里。这很可能不是你想要的。使用 == 进行比较, = 进行分配。更好的是,要检查 boolean 是否 false ,请使用

  if(!this.isOnLoan)

为什么你的方法返回 double 值?


I am currently completing a project and am trying to create a menu. The menu itself works alright but when I try and link to the other java file that the method itself is in I get the error "the method method name is undefined for the type String". Below is the code that I am using to try and link to the method in the other java file.

        {
        if (menuChoice.equals("A"))
        {
            System.out.print("Enter the Movie ID: ");
            movieID = sc.nextLine();
            movieID.borrowMovie();
        }
        if (menuChoice.equals("a"))
        {
            System.out.print("Enter the Movie ID: ");
            movieID = sc.nextLine();
            movieID.borrowMovie();
        }
        if (menuChoice.equals("B"))
        {
            System.out.print("Enter the Movie ID: ");
            movieID = sc.nextLine();
            movieID.returnMovie();
        }
        if (menuChoice.equals("b"))
        {
            System.out.print("Enter the Movie ID: ");
            movieID = sc.nextLine();
            movieID.returnMovie();
        }

Further to, below is the code of the 2 methods that I am trying to link to in the other java file. It is a requirement of the project to have 2 separate class files and to demonstrate how to use them both together but this part has got me stumped!

// Implement borrowMovie method
public double borrowMovie(String memberID)
{
    if (this.isOnLoan = false)
    {
        this.isOnLoan = true;
    }
    else 
    {
        return Double.NaN;
    }
    }

// Implement returnMovie method
public double returnMovie(int daysBorrowed)
{
    if (this.isOnLoan = false)
    {
        return Double.NaN;
    }
    else
    {
        this.isOnLoan = false;

    }
}

Any help at all will be appreciated :)

解决方案

Looking at your code, I guess that movieID is a String.

Class String does not have methods borrowMovie() and returnMovie(), so you'll get an error from the compiler that tells you so.

Instead of movieID.borrowMovie() you want: borrowMovie(movieID).

Here's another bug in your borrowMovie() and returnMovie() methods:

if (this.isOnLoan = false)

You are assigning the value false to this.isOnLoan here. That is most likely not what you want. Use == for comparison, = for assignment. Better yet, to check if a boolean is false, use !:

if (!this.isOnLoan)

Why are your methods returning double values?

这篇关于“方法**对于类型字符串是未定义的”在Eclipse中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆