This question already has answers here:
How do I compare strings in Java?

(23个答案)


5年前关闭。




我创建了一个Singleton类,该类是Boat的列表。我正在为MainActivity的操作栏创建一个选项,该选项栏将基于用户注册船只的输入来显示有关船只的信息。如果在列表中找到注册,则应将有关该特定船的注册的所有信息附加到TextView上。但是,我一直收到错误消息。我认为原因是在我的for循环语句或其主体中的某个位置,但是我不太确定。这是我的代码的样子(我还将在我的主要代码下面提供列表,BoatList(又名我的单身人士类)和常规类Boat):

public void showBoat(View view)
{
    //Declare references and variables
    EditText et;
    TextView tv;
    ImageView im;
    BoatList boat_list;
    String boatRegist;

    //Set references
    et = (EditText) findViewById(R.id.edit_boat_regist);

    tv = (TextView) findViewById(R.id.text_main);

    im = (ImageView) findViewById(R.id.image_area);

    boat_list = BoatList.getInstance();

    //Get user input
    boatRegist = et.getText().toString();

    //Search through the list and try to find the boat by registration

    int i;
    for(i = 0; i < boat_list.size(); i++)
    {
        if(boatRegist == boat_list.get(i).getRegi())
        {
            tv.append(boat_list.get(i).getMake() + "\n" + boat_list.get(i).getRegi() +
                    "\n" + boat_list.get(i).getYear() + "\n" + boat_list.get(i).getLength()
                    + "\n" + boat_list.get(i).getBeam() + "\n" + boat_list.get(i).getFuel() +
                    "\n" + boat_list.get(i).getPrice() + "\n" + boat_list.get(i).getAge()
                    + "\n");
        }
        else
            Toast.makeText(ShowBoatActivity.this, "Error: The registration is invalid!",
                    Toast.LENGTH_SHORT).show();
    }

    //Open the picture from the URL address
    /*
    ImageDownloader idl = new ImageDownloader();
    idl.download(boat_list.get(i).getPicURL(), im);*/

}


以下是我的Singleton课程:

import java.util.LinkedList;


public final class BoatList extends LinkedList<Boat>
{

private static BoatList instance = null;


private BoatList()
{
    // Exists only to defeat additional instantiations.
}

//Define method
public static BoatList getInstance()
{
    if(instance == null)
        instance = new BoatList();

    return instance;
}

} //end BoatList


最后,这是我的普通班,船:

public class Boat {

//Declare data members
private String make;
private String registeration;
private int year;
private int length;
private int beam;
private String fuel;
private double price;
private String picURL;
private int age;
private double priceLT;

//Define the methods
public String getMake()
{
    return make;
}

public void setMake(String m)
{
    make = m;
}

public String getRegi()
{
    return registeration;
}

public void setRegi(String r)
{
    registeration = r;
}

public int getYear()
{
    return year;
}

public void setYear(int y)
{
    year = y;
}

 public int getLength()
 {
    return length;
 }

 public void setLength(int L)
 {
    length = L;
 }

 public int getBeam()
 {
     return beam;
 }

 public void setBeam(int b)
 {
    beam = b;
 }

public String getFuel()
{
    return fuel;
}

public void setFuel(String f)
{
    fuel = f;
}

public double getPrice()
{
    return price;
}

public void setPrice(double p)
{
    price = p;
}

public String getPicURL()
{
    return picURL;
}

public void setPicURL(String url)
{
    picURL = url;
}

public int getAge()
   {

    int currentYear = 2015;

    //Calculate the age of the boat
    age = currentYear - year;

    return age;
}

public double getLuxuryTax()
{
    //Declare variables
    double newPrice;
    double luxTax = .15;

    //Calculate the price with luxury tax if the first condition
    //is found to be True.
    if (length >= 50 && age <= 10)
    {
        newPrice = price * luxTax;
        priceLT = price + newPrice;

        if (priceLT == 0)
        {
            priceLT = (Double) null;
            return priceLT;
        }
    }

    return priceLT;
}

}

最佳答案

您正在使用==来比较注册值,应该使用.equalsIgnoreCase(str) reference来确保如果用户使用小写字母而不是大写字母来对用户输入进行正比较。

关于java - 为什么我得到的 list 输出不正确? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29199138/

10-13 09:13