一段时间以来,我一直在尝试解决此问题,但似乎还是无法解决。我正在尝试从用户那里获取电话号码,以便可以显示它,但是当我获得所有用户的信息时,就会发生错误。任何帮助,将不胜感激。谢谢。
代码如下:
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;
}
}
发生错误的代码已用注释标记。
答案 0 :(得分:3)
该错误表示您正在尝试在不存在的索引处访问phone
的字符 。
准确地说,您的phone
字段从未在代码内设置,因此它是一个空字符串。
无论如何,您还应该使用for
变量来修复len
循环:
int len = phone.length();
for(int i = 0; i < len; i++)
{
...
}
这样做,您不必担心StringIndexOutOfBoundsException
,因为现在for
仅自动遍历String
中存在的字符。
答案 1 :(得分:2)
每当您尝试访问给定索引中不存在的字符串中的字符时,都会抛出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的string formatting。
这不仅会提高代码的可读性,而且(如前所述)会减少开销,因为Java中的字符串为immutable。
答案 2 :(得分:0)
要使代码正常工作,您应该创建新的本地变量(例如inputPhone
),然后更改Event对象的phone
变量。另外,您应该在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";
}
}