我正在嘗試編寫一些代碼來驗證用戶的行條目。我需要確保用戶輸入了「:」,因爲這是行分隔符,但是我收到錯誤。包含的數據類型錯誤
這是錯誤:
The method contains(CharSequence) in the type String is not applicable for the arguments (char)
有沒有什麼辦法可以解決這個問題?
import java.util.Scanner;
import java.lang.*;
public class REQ2
{
public static void main (String[] args)
{
String playername;
String line;
String[] list = new String[100];
int count = 0;
int score;
int time;
int totalScore =0;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your name");
playername = sc.nextLine();
if(playername.equals(""))
{
System.out.println("Player name was not entered please try again");
System.exit(0);
}
System.out.println("Please enter your game achivements (Game name:score:time played) E.g. Minecraft:14:2332");
while (count < 100){
line = sc.nextLine();
if(line.equals("quit")){
break;
}
if(!(line.contains(':'))){ //error on this line
System.out.println("Please enter achivements with the proper \":\" sepration\n");
}
list[count]=line;
System.out.println("list[count]" + list[count]);
count++;
for (int i=0; i<count; i++){
line=list[i];
String[] elements =line.split(":");
if (line.length() !=3){
System.out.println("Error please try again, Please enter in the following format:\nGame name:score:timeplayed");
break;
}
score = Integer.parseInt(elements[1].trim());
time=Integer.parseInt(elements[2].trim());
}
}
}
}
一個'char'不是'CharSequence',所以你不能傳遞一箇中,而是傳遞一個'String' :'if(!line.contains(「:」)){' – azurefrog