[英]String index out of range: 0
在尋找解決方案時,我發現了使用sc.next()而不是sc.nextLine()的建議,但是我不能使用它,因此我需要其他幫助。
我正在開發在線多人文字游戲,需要過濾玩家姓名,但是我一直收到錯誤消息“字符串索引超出范圍: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(name):
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();
}
如果有任何幫助,我將不勝感激
我認為您的問題是這樣的:
if (Utills.profan(name) && (name != null)
&& ((!name.equals("")) || (!name.equals(" "))))
應該是
if (Utills.profan(name) && (name != null)
&& !name.equals("") && !name.equals(" "))
甚至
if (Utills.profan(name) && (name != null)
&& !name.trim().isEmpty())
如果您仍然看不到我的意思,檢查((!name.equals("")) || (!name.equals(" ")))
將始終為true
,因為name
始終不是""
還是不" "
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.