比较对象数组中的字符串

时间:2014-01-02 19:09:43

标签: java arrays string

我是新人。希望有人可以帮助我。我想比较用户输入的字符串和课程标题变量,这也是一个字符串类型。“课程标题”变量它是插入数组的一些对象的状态的一部分对象。

以下是代码:

    if (selection.equals("yes")){
        System.out.print("");
        System.out.print("Enter the day of exam you want to search: ");
        int search= ScannerUtility.readInt();
        int i = 0;

line 43    while( i<a.length && search.equals(a[i].getCourseTitle()) ){
           i++;
           {

这里是控制台错误:

line   43: 

error: int cannot be dereferenced while( i<a.length && search.equals(a[i].getCourseTitle() ) ){

//Here is all the classes

我是意大利人,很抱歉,如果我没有正确翻译。请注意这里是ManagementExams课程:

import java.util.*;
    public class ManagementExams{
        public static void main(String[] args){
            Scanner sc= new Scanner(System.in);
            System.out.println("MANAGEMENT EXAMS");
            System.out.print("Indicate how many exams do you want to insert: ");
            int dim = ScannerUtility.readInt();
            int nexams = 1;
            Exam [] a = new Exam[dim];
                for(int i=0; i<a.length; i++){
                    System.out.print("Insert the name of the exam " + nexams +":");
                    String coursetitle = sc.nextLine();
                    System.out.print("Insert the day of the exam: ");
                    int day = ScannerUtility.readInt();
                    System.out.print("Insert the month of the exam: ");
                    String month = sc.nextLine();
                    System.out.print("Insert the year of the exam: ");
                    int year = ScannerUtility.readInt();
                    System.out.print("Insert the hour of the exam: ");
                    int hourexam = ScannerUtility.readInt();
                    Exam b = new Exam(coursetitle,day,month,year,hourexam);
                    a[i] = b;
                    nexams++;
                }
                    System.out.print("The number of exams inserted are': " + (nexams-1)+ "\n\n");
                    System.out.println("Print Exams: \n");
                        for(int i=0; i<a.length; i++)
                            System.out.println(a[i]);


                            System.out.print("Do you want to change the day of certain exams?: ");

                            String selection = sc.nextLine();

                                if (selection.equals("yes")){
                                    System.out.print("");
                                    System.out.print("Insert the name of the exam you want to search: ");
                                    String search= ScannerUtility.readString();
                                    int i = 0;

                                        while( i<a.length && search.equals(a[i].getCourseTitle()) )
                                            i++;


                                            if(i<a.length){
                                                System.out.println("I found the following exam: \n" + a[i]);
                                                System.out.print("Change the day: ");
                                                int day= ScannerUtility.readInt();
                                                a[i].setDay(day);
                                            }
                                            else{
                                                System.out.println("No exam was found ");
                                                System.exit(0);
                                            }

                                }

                                else if(selection.equals("no"))
                                    System.exit(0);

                                for(int i=0; i<a.length; i++)
                                System.out.print("Mold the update data of the exams: \n" + a[i] +"\n");
        }
    }

这里的课程考试:

public class Exam{

    private String coursetitle;
    private int day;
    private String month;
    private int year;
    private int hourexam;


    //CONSTRUCTOR

    public Exam(String coursetitle,int day,String month,int year,int hourexam){
        this.coursetitle=coursetitle;
        this.day=day;
        this.month=month;
        this.year=year;
        this.hourexam=hourexam;
    }




    //SET METHODS
    public void setCourseTitle(String a){
        this.coursetitle=a;
    }
    public void setDay(int a){
        this.day=a;
    }

    public void setMonth(String a){
        this.month=a;
    }

    public void setYear(int a){
        this.year=a;
    }

    public void setHourExam(int a){
            this.hourexam=a;
    }

    //GET METHODS
    public String getCourseTitle(){
        return this.coursetitle;
    }
    public int getDay(){
        return this.day;
    }
    public String getMonth(){
        return this.month;
    }
    public int getYear(){
        return this.year;
    }
    public int getHourExam(){
        return this.hourexam;
    }

    //TOSTRING

    public String toString(){
        return coursetitle +"\n" + day + "\n" + month + "\n" + year +"\n" + hourexam;
    }


}

这两个类都正确构建和编译,但是当我使用像“math”这样的coursetitle创建一个考试然后我尝试搜索它时,使用字符串输入“math”,程序跳转到println方法“没有找到考试”

2 个答案:

答案 0 :(得分:2)

searchint,无法解除引用(即您无法使用.)。如果它是一个整数,似乎使用==可能会起作用,但是getCourseTitle可能会返回一个字符串。在这种情况下,将整数转换为字符串。

while (i < a.length && Integer.toString(search).equals // etc.

答案 1 :(得分:0)

//问题已解决;)

import java.util.*;
public class ManagementExams{
    public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        System.out.println("MANAGEMENT EXAMS \n");
        System.out.print("Indicate how many exams do you want to insert: ");
        int dim = ScannerUtility.readInt();
        int nexams = 1;
        Exam [] a = new Exam[dim];
            for(int i=0; i<a.length; i++){
                System.out.println("");
                System.out.print("Insert the name of the exam " + nexams +":");
                String coursetitle = sc.nextLine();
                System.out.print("Insert the day of the exam: ");
                int day = ScannerUtility.readInt();
                System.out.print("Insert the month of the exam: ");
                String month = sc.nextLine();
                System.out.print("Insert the year of the exam: ");
                int year = ScannerUtility.readInt();
                System.out.print("Insert the hour of the exam: ");
                int hourexam = ScannerUtility.readInt();
                Exam b = new Exam(coursetitle,day,month,year,hourexam);
                a[i] = b;
                nexams++;
            }
                System.out.print("The number of exams inserted are: " + (nexams-1)+ "\n\n");
                System.out.println("Print Exams: \n");
                    for(int i=0; i<a.length; i++){
                        System.out.println(a[i].toString());
                    }

                        System.out.print("Do you want to change the day of certain exams?: ");

                        String selection = sc.nextLine();
                            if (selection.equals("yes")){
                                System.out.print("");
                                System.out.print("Insert the name of the exam you want to search: ");
                                String search = ScannerUtility.readString();
                                boolean var = false;

                                    for(int i=0; i < a.length; i++){

                                        if(search.equals(a[i].getCourseTitle()) ){
                                            var = true;
                                            System.out.println("I found the following exam: \n" + a[i].toString() );
                                            System.out.print("Change the day: ");
                                            int newday = ScannerUtility.readInt();
                                            a[i].setDay(newday);
                                        }
                                    }
                                    if(var == false ){
                                        System.out.println("No exam was found ");
                                        System.exit(0);
                                    }

                                    System.out.println("");
                                    for(int j=0; j < a.length; j++){
                                        System.out.println("Exams updated: \n" + a[j].toString() + "\n");
                                    }

                                    System.exit(0);

                            }else if(selection.equals("no")){
                                System.exit(0);
                            }


    }
}