0
在尋找解決方案時,我發現了一個使用sc.next()而不是sc.nextLine()的建議,但我無法使用它,因此我需要額外的幫助。字符串索引超出範圍:0
我正在開發一個在線多人遊戲文本遊戲,需要過濾玩家的名字,但我不斷收到錯誤「字符串索引超出範圍:0」我已經嘗試了幾個小時來解決這個問題,一直沒能找到解決方案。 導致錯誤的功能是這樣的:
public static Player charCreate(Player player) {
int points = 60;
int i = 0;
boolean cont = true;
int str = 0;
int dex = 0;
int end = 0;
int INT = 0;
int lck = 0;
int cha = 0;
Output.slowDiscription("You stand there and think about all"
+ " that has transpired, who are you realy?");
System.out.print("What is your Name? ");
boolean bool = false;
String temp;
String name = null; //name is forced to be declared by while loop
do {
name = sc.nextLine();
System.out.println(name);
if (Utills.profan(name) && (name != null)
&& ((!name.equals("")) || (!name.equals(" ")))) {
bool = true;
player.setName(Utills.filter(name)); //Error is here
}
else {
bool = false;
}
} while (bool == false);
player.Height = getUsrHeight();
System.out.print("Please select your stats. ");
do {
System.out.println("Points Remaining: " + points);
switch (i) {
case 0:
System.out.println();
System.out.println("Please Enter the number of points to alot to "
+ "Strength.\n Min:1 Max:18");
str = sc.nextInt();
if ((str >= 1) && (str <= 18) && (str <= points) &&
((points - str) >= 5)) {
points -= str;
i++;
}
break;
case 1:
System.out.println();
System.out.println("Please Enter the number of points to alot to "
+ "Dexterity.\n Min:1 Max:18");
dex = sc.nextInt();
if ((dex >= 1) && (dex <= 18) && (dex <= points) &&
((points - dex) >= 4)) {
points -= dex;
i++;
}
break;
case 2:
System.out.println();
System.out.println("Please Enter the number of points to alot to "
+ "Endurance.\n Min:1 Max:18");
end = sc.nextInt();
if ((end >= 1) && (end <= 18) && (end <= points) &&
((points - end) >= 3)) {
points -= end;
i++;
}
break;
case 3:
System.out.println();
System.out.println("Please Enter the number of points to alot to "
+ "Inteligence.\n Min:1 Max:18");
INT = sc.nextInt();
if ((INT >= 1) && (INT <= 18) && (INT <= points) &&
((points - INT) >= 2)) {
points -= INT;
i++;
}
break;
case 4:
System.out.println();
System.out.println("Please Enter the number of points to alot to "
+ "Luck.\n Min:1 Max:18");
lck = sc.nextInt();
if ((lck >= 1) && (lck <= 18) && (lck <= points) &&
((points - lck) >= 1)) {
points -= lck;
i++;
}
break;
case 5:
System.out.println();
System.out.println("Please Enter the number of points to alot to "
+ "Charisma.\n Min:1 Max:18");
cha = sc.nextInt();
if ((cha >= 1) && (cha <= 18) && (cha <= points)) {
points -= cha;
i++;
}
break;
case 6:
int[] stats = {str, dex, end, INT, lck, cha};
player.setStats(stats);
cont = false;
break;
}
}while (cont);
return player;
}
的錯誤在這裏來自Utills.filter(名稱):
public static String filter(String name) {
//Variables
String[] Name = name.toLowerCase().split(" ");
StringBuilder sb = new StringBuilder();
StringBuilder endStr = new StringBuilder();
char temp;
int i = 0;
//Sorting
for(String w: Name) {
sb.append(w);
temp = Character.toUpperCase(sb.charAt(0)); //And Error is here
sb.setCharAt(0, temp);
if(i >= 1) {
endStr.append(" " + sb.toString());
}
else {
endStr.append(sb);
}
i++;
empty(sb);
}
return endStr.toString();
}
我會greatfull任何幫助
錯誤消息應該告訴你錯誤在哪一行。請讓我們知道它是什麼(和_show_我是哪一行;不要告訴我們行號,因爲我們將無法確定哪一行是最好的,最好是編輯你的問題,並添加一個對您的代碼發表評論,說明錯誤消息指的是哪一行)。 – ajb
'name'的價值是什麼?如果錯誤在'filter()'中,那麼其他代碼對你的問題沒有任何意義,只是噪聲。相關部分是導致錯誤的值和發生錯誤的行。 – Andreas
不知道錯誤在哪一行,很難說清楚。但我的猜測是'w'可能是一個空字符串。 – ajb