简体   繁体   中英

Compare string within an array of objects

i am new. Hope someone can help me.I want to compare a string from user input with a course title variable which is also a string type.The "course title" variable it's part of the state of a number of objects that are inserted in an array of objects.

Here is the code:

    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++;
           {

here the console error:

line   43: 

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

//Here is all the classes

I am italian, thus sorry if i don't translate properly.BTW here is the ManagementExams Class:

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");
        }
    }

Here the class Exam:

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;
    }


}

Both class build and compile correctly,but when i create an exam with the coursetitle like "math" and then i try to search it,with the string input "math" the program jump to the println method "no exam was found".

search is an int which cannot be dereferenced (ie you cannot use . with it). It seems like using == may work if it is an integer, but perhaps getCourseTitle returns a string. In that case, convert the integer to a string.

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

//PROBLEM SOLVED ;)

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);
                            }


    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM