错误无法仅通过在线编译器找到符号

时间:2018-05-07 12:36:46

标签: java

我提交代码时收到此错误。只有当我在我的课程所需的在线编译器上提交我的代码时才会发生这种情况,但是,当我通过InteliJ运行我的代码时,它会正确编译。

  

Main.java:335:错误:找不到符号           while(!(TeamMember.contains(“Stop”))){                              ^     符号:方法包含(String)     location:班级成员   1错误   <

我的课程如下:

主:

package com.company;

 import java.util.Scanner;
 import java.util.ArrayList;

public class Main {

public static void main(String[] args) {
    String name = "";
    String id = "";

    ArrayList<TeamMember> list = new ArrayList<>();

    while (!(TeamMember.contains("Stop"))) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter the name: ");
        name = scan.nextLine();

        System.out.println("Please enter the id: ");
        id = scan.nextLine();

        list.add(new TeamMember(name.toLowerCase(), id));

        System.out.println(Main.selectionSort(list));


    }
    int size = list.size();
    for (int j = size; j > (list.size() + 2); j--) {
        list.remove(j);
    }

}

public static ArrayList<TeamMember> selectionSort(ArrayList<TeamMember> list) {
    TeamMember[] teamArray = new TeamMember[list.size()];

    for (int i = 0; i < list.size(); i++) {
        teamArray[i] = list.get(i);
    }

    for (int i = 0; i < teamArray.length - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < teamArray.length; j++) {
            if (teamArray[j].compareTo(teamArray[minIndex]) < 0) {
                minIndex = j;
            }
        }
        TeamMember holder = teamArray[i];
        teamArray[i] = teamArray[minIndex];
        teamArray[minIndex] = holder;
    }

    for (int i = 0; i < list.size(); i++) {
        list.set(i, teamArray[i]);
    }

    return list;
}

}

TeamMember:

package com.company;

import java.util.ArrayList;

class TeamMember{
private String fullName;
private String idString;

public TeamMember(String name, String id){
    fullName = "";
    name = name.toLowerCase();
    String [] charName = new String[name.length()];

    for(int i = 0; i < charName.length; i++){
        charName[i] = Character.toString(name.charAt(i));
        if(i == 0){
            charName[0] = charName[0].toUpperCase();
        }
    }

    for(int i = 0; i < charName.length - 1; i++){
        if(charName[i].equals(" ") && !charName[i + 1].equals(" ")){
            charName[i + 1] = charName[i + 1].toUpperCase();
        }
    }
    for(int i = 0; i < charName.length; i++){
        fullName = fullName + charName[i];
    }

    idString = id;
}

public static boolean contains(String stop) {
return false;
}

public String getID(){
    return idString;
}

public String toString(){
    return fullName;
}

public int compareTo(TeamMember other){
    if(idString.compareTo(other.getID()) < 0){
        return -1;
    }
    else if(idString.compareTo(other.getID()) > 0){
        return 1;
    }
    else{
        return 0;
    }
}

}

1 个答案:

答案 0 :(得分:0)

您的TeamMember课程无法访问课程Main。尝试使用此代码,因为它应该在同一个文件中使用类。

import java.util.Scanner;
import java.util.ArrayList;

public class Main 
{

public static void main(String[] args) {
    String name = "";
    String id = "";

    ArrayList<TeamMember> list = new ArrayList<>();

    while (!(TeamMember.contains("Stop"))) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter the name: ");
        name = scan.nextLine();

        System.out.println("Please enter the id: ");
        id = scan.nextLine();

        list.add(new TeamMember(name.toLowerCase(), id));

        System.out.println(Main.selectionSort(list));


    }
    int size = list.size();
    for (int j = size; j > (list.size() + 2); j--) {
        list.remove(j);
    }

}

public static ArrayList<TeamMember> selectionSort(ArrayList<TeamMember> list) {
    TeamMember[] teamArray = new TeamMember[list.size()];

    for (int i = 0; i < list.size(); i++) {
        teamArray[i] = list.get(i);
    }

    for (int i = 0; i < teamArray.length - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < teamArray.length; j++) {
            if (teamArray[j].compareTo(teamArray[minIndex]) < 0) {
                minIndex = j;
            }
        }
        TeamMember holder = teamArray[i];
        teamArray[i] = teamArray[minIndex];
        teamArray[minIndex] = holder;
    }

    for (int i = 0; i < list.size(); i++) {
        list.set(i, teamArray[i]);
    }

    return list;
}

}

class TeamMember{
private String fullName;
private String idString;

public TeamMember(String name, String id){
    fullName = "";
    name = name.toLowerCase();
    String [] charName = new String[name.length()];

    for(int i = 0; i < charName.length; i++){
        charName[i] = Character.toString(name.charAt(i));
        if(i == 0){
            charName[0] = charName[0].toUpperCase();
        }
    }

    for(int i = 0; i < charName.length - 1; i++){
        if(charName[i].equals(" ") && !charName[i + 1].equals(" ")){
            charName[i + 1] = charName[i + 1].toUpperCase();
        }
    }
    for(int i = 0; i < charName.length; i++){
        fullName = fullName + charName[i];
    }

    idString = id;
}

public static boolean contains(String stop) {
return false;
}

public String getID(){
    return idString;
}

public String toString(){
    return fullName;
}

public int compareTo(TeamMember other){
    if(idString.compareTo(other.getID()) < 0){
        return -1;
    }
    else if(idString.compareTo(other.getID()) > 0){
        return 1;
    }
    else{
        return 0;
    }
}
}