Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 修剪对象的最后4个字符_Java_String_Oop_Object_Substring - Fatal编程技术网

Java 修剪对象的最后4个字符

Java 修剪对象的最后4个字符,java,string,oop,object,substring,Java,String,Oop,Object,Substring,我想修剪显示在第3列(房间类型)和第4列(用餐类型)中的对象的最后4个字符。在底部,我提供了输出的样本。在第3列和第4列中,您可以清楚地看到最后4个字符是括号中的price,这是我想说的 public void showAll() { String name =""; String ID=""; Object roomItem; Object mealItem; int roomIn; in

我想修剪显示在第3列
(房间类型)
和第4列(
用餐类型)
中的对象的最后4个字符。在底部,我提供了输出的样本。在第3列和第4列中,您可以清楚地看到最后4个字符是括号中的price,这是我想说的

public void showAll()

    {
        String name ="";
        String ID="";
        Object roomItem;
        Object mealItem;
        int roomIn;
        int meal;
        int days=0;
        double tprice=0;

        display.setText("");
        display.append("ID  Customer Name   RoomType    MealType    Days    TotalCharge($)");
        display.append("\n ---------------------------------");

        for (int i = 0; i < myList.size(); i++)
           {
        Customer c = myList.get(i);

        ID = c.getID();
        name = c.getName();
        roomIn = c.getRoomIndex();                  // Get the room index stored in Linked list
        roomItem = roomTypeCombo.getItemAt(roomIn); // Get the item stored on that index.
        meal = c.getMealIndex();                    // Get the Meal index stored in Linked list
        mealItem = mealCombo.getItemAt(meal);       // Get the item stored on that index.
        days = c.getDaysIndex();
        tprice = c.getTotalPrice();
        display.append("\n"+ID+"    "+name+"        "+roomItem+"    "+mealItem+"    "+days+ "   "+tprice);
            }
        display.append("\n \n Total "+myList.size()+" Entrie(s) !");

    } // end of function
我怎样才能跳过
房间类型
用餐类型
的最后4个字符?

您可以使用:


在发布此类问题之前,首先应该阅读JavaStringAPI:

然后,您可以使用类似子字符串的方法

public String substring(int beginIndex,
               int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

     "hamburger".substring(4, 8) returns "urge"
     "smiles".substring(1, 5) returns "mile"


Parameters:
    beginIndex - the beginning index, inclusive.
    endIndex - the ending index, exclusive.
Returns:
    the specified substring.
Throws:
    IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

你看过JavaAPI了吗?有一个为
字符串定义的方法:
子字符串(…)
。我将把参数留给你去弄清楚——读一读并不是那么难。@Thomas:我试过substring(),但RoomItem和MealItem被声明为对象而不是字符串。因此,当我试图使用子字符串时,它显示“找不到符号”错误。@Ravi那么,
roomItem
的确切类型是什么?它不能是
对象
,但必须是子类型。如果它
s
String`那么。。。你听说过强制转换吗?@Thomas:是的,我知道,但是当我将roomItem定义为字符串时,它没有存储我的结果并得到错误,然后我搜索了它,得到了我需要定义为Object的结果。根据Javadoc,最好看到
pricey.length()-4
不会产生负值,因为这将导致异常。@npinti-正确点。通用的字符串缩短方法应该进行检查,或者在方法文档中指定传递小于4个字符的字符串将引发异常。但是my roomItem被声明为Object而不是string。根据Javadoc,最好看到
pricey.length()-4
不会产生负值,因为这将导致异常。当然,在尝试获取字符串的子字符串之前,您必须检查字符串的长度。不幸的是,此代码将返回最后四个字符,而不是最后四个字符的全部内容。忘记了0。接得好,@TedHopp!但是我的roomItem变量声明为Object而不是String。子字符串和长度是字符串函数。
String word = "Breakfast($10)".substring(0, "Breakfast($10)".length() - 4);
String pricey = "Breakfast($10)";
String yummy = pricey.substring(0, pricey.length() - 4);
public String substring(int beginIndex,
               int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

     "hamburger".substring(4, 8) returns "urge"
     "smiles".substring(1, 5) returns "mile"


Parameters:
    beginIndex - the beginning index, inclusive.
    endIndex - the ending index, exclusive.
Returns:
    the specified substring.
Throws:
    IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.