繁体   English   中英

为什么我的代码不断抛出 StringIndexOutOfBoundsException?

[英]Why does my code keep throwing a StringIndexOutOfBoundsException?

我已经尝试解决这个问题有一段时间了,但似乎无法解决。 我正在尝试从用户那里获取电话号码,以便我可以显示它,但是当我获取所有用户信息时,就会发生错误。 任何帮助,将不胜感激。 谢谢你。

这是代码:

import java.util.Scanner;

public class Event 
{
    public static double pricePerGuestHigh = 35.00;
    public static double pricePerGuestLow = 32.00;
    public static final int LARGE_EVENT_MAX = 50;
    public String phone = "";
    public String eventNumber;
    private int guests;
    private double pricePerEvent;

    public void setPhone()
    {
        Scanner input = new Scanner(System.in);
        int count = 0;

        System.out.println("Enter your phone number: ");
        String phone = input.nextLine();
        int len = phone.length();
        for(int i=0; i<1; i++)
        {
            char c = phone.charAt(i);
            if(Character.isDigit(c))
            {
                count++;
                String ss = Character.toString(c);
                phone = phone.concat(ss);
            }
        }
        if(count != 10)
        {
            phone = "0000000000";
        }
    }

    public String getPhone()
    {
        // The error occurs in this method
        String ret = "(" + this.phone.charAt(0) + "" + this.phone.charAt(1)
        + "" + this.phone.charAt(2) + ")" + this.phone.charAt(3) 
        + "" + this.phone.charAt(4) + "" + this.phone.charAt(5)
        + "" + this.phone.charAt(6) + "" + this.phone.charAt(7)
        + "" + this.phone.charAt(8) + "" + this.phone.charAt(9);
        return ret;
    }

    public void setEventNumber()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the event number: ");
        eventNumber = input.nextLine();
    }

    public void setGuests(int guests)
    {
        this.guests=guests;
        if(isLargeEvent())
            pricePerEvent = pricePerGuestHigh;
        else
            pricePerEvent = pricePerGuestLow;
    }

    public int getGuestsCount()
    {
        return guests;
    }

    public boolean isLargeEvent()
    {
        if(guests >= LARGE_EVENT_MAX)
        {
            return true;
        }
        else if(guests < LARGE_EVENT_MAX)
        {
            return false;
        }
        return isLargeEvent();
    }

    public String getEventNumber()
    {
        String ret1 = "Event Number: " + this.eventNumber;
        return ret1;
    }

    public int getGuests(boolean largeEvent)
    {
        return guests;
    }
}

发生错误的代码已用注释标记。

该错误意味着您正试图在不存在的索引处访问phone的字符。

准确地说,您的phone字段从未设置在您的代码中,因此它是一个空字符串。

无论如何,您还应该使用len变量来修复for循环:

int len = phone.length();
for(int i = 0; i < len; i++)
{
    ...
}

通过这样做,您不必担心StringIndexOutOfBoundsException因为现在for仅自动遍历String存在的字符。

每当您尝试访问给定索引处不存在的字符串中的字符时,就会抛出 StringOutOfBoundsException 。

从您提供的代码看来,您似乎正在访问getPhone()方法中的空字符串。

您可以通过首先使用phone.isEmpty()检查字符串是否为空来解决此问题。

public String getPhone() {

    if (phone == null || /*this.*/phone.isEmpty()) {
        // Handle the error accordingly.
        return null; // example
    }
    String ret = "(" + this.phone.charAt(0) + "" + this.phone.charAt(1)
    + "" + this.phone.charAt(2) + ")" + this.phone.charAt(3) 
    + "" + this.phone.charAt(4) + "" + this.phone.charAt(5)
    + "" + this.phone.charAt(6) + "" + this.phone.charAt(7)
    + "" + this.phone.charAt(8) + "" + this.phone.charAt(9);
    return ret;
}

虽然我们正在这样做,但我建议不要使用字符串连接,因为这会产生大量开销。 相反,使用 Java 的字符串格式

这不仅会增加代码的可读性,而且会(如前所述)减少开销,因为 Java 中的字符串是不可变的

为了使您的代码工作,您应该创建新的本地inputPhone (例如inputPhone ),而不是更改 Event 对象的phone inputPhone 您也应该在 for 循环中更改条件。

public void setPhone()
{
    Scanner input = new Scanner(System.in);
    int count = 0;

    System.out.println("Enter your phone number: ");
    String inputPhone = input.nextLine();
    int len = inputPhone.length();
    for(int i=0; i<len; i++)
    {
        char c = inputPhone.charAt(i);
        if(Character.isDigit(c))
        {
            count++;
            String ss = Character.toString(c);
            phone = phone.concat(ss);
        }
    }
    if(count != 10)
    {
        phone = "0000000000";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM