0

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".

2
  • 1
    Can we see the definition of a? Commented Jan 2, 2014 at 19:12
  • 1
    search is a primitive type, you can't access to fields or methods.. Commented Jan 2, 2014 at 19:20

2 Answers 2

3

search is an int which cannot be dereferenced (i.e. 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.
Sign up to request clarification or add additional context in comments.

6 Comments

Well spotted. It might be the the problem is some semantic bug as does not makes much sense to compare "exam days" with "course titles" We need the full code @ggianino
i have updated the first post ;)..one thing what is that "Integer" that you used in the example?
@ggianino Integer is the int wrapper class java.lang.Integer. He is using it to access the static method toString therein implemented. ("" + search).equals would be as effective.
Why should i convert an int value to a string value and viceversa? The problem it's located here: while( i<a.length && search.equals(a[i].getCourseTitle()) ).How can i compare the string value of user input with the coursetitle var in the Exam object?
@ggianino Why are you using ScannerUtility.readInt then? That reads in an int...
|
0

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


    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.