Arraylist和显示

时间:2015-11-16 03:07:57

标签: java arraylist

这是我的课程:

prev_word = 0
print_variable = 0

for line in sys.stdin:
        line = line.strip()       #strip out carriage return
        key_value = line.split('\t')   #split line, into key and value, returns a list   

        #note: for simple debugging use print statements, ie:  
        curr_word = key_value[0]         #key is first item in list, indexed by 0
        value_in = key_value[1]         #value is 2nd item

    if (value_in == "ABC"):
        print_variable = 1
    else:
        value_in = int(value_in)
        prev_word += value_in
    if (print_variable == 1):
        print( '%s\t%s' % (curr_word, prev_word) )
        print_variable = 0
    prev_word = curr_word

以下是我的主要内容:

package classregisterapp;

import java.util.ArrayList;

public class ClassRegister 
{
   private String[] studentNames;
   private String tutGrp;

   public ClassRegister(String[] studentNames, String tutGrp) 
   {
      this.studentNames = studentNames;
      this.tutGrp = tutGrp;
   }

   public String getTutGrp()
   {
      return tutGrp;
   }

   public void searchStudent(String name)
   {
      if (name == tutGrp)
      {
         System.out.println("Student found");
      }
      else
      {
         System.out.println("Student not found");
      }
   }
}

1)使用由学生组成的教程组(“Ron”,“Mandy”,“Joshua”)和教程组名称“T09”创建一个ClassRegister对象。

2)读取用户的姓名并查明学生是否在教程组中。如果找到学生,则显示消息“找到学生”。否则,显示消息“未找到学生”。

当我做package classregisterapp; import java.util.ArrayList; import java.util.Scanner; public class ClassRegisterApp { public static void main(String[] args) { ArrayList<ClassRegister> classList = new ArrayList<>(); ClassRegister cr1= new ClassRegister ("Ron", "T09"); ClassRegister cr2 = new ClassRegister ("Mandy", "T09"); ClassRegister cr3 = new ClassRegister ("Joshua", "T09"); classList.add(cr1); classList.add(cr2); classList.add(cr3); Scanner sc = new Scanner(System.in); System.out.println("Enter student name: "); } } ...时,“Ron”用红色加下划线,第12到14行,表示“String无法转换为String []”。我想知道错误是什么,并且不确定如何要求用户检查学生是否在课堂上,或者我是否采取了正确的方式?

1 个答案:

答案 0 :(得分:0)

class ClassRegister构造函数接受一个字符串数组作为第一个参数,并且只传入一个字符串。例如:

ClassRegister cr1= new ClassRegister ("Ron", "T09");
                                        ^