![](/img/trans.png)
[英]StringIndexOutOfBoundsException in my MapReduce code
[英]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.