Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2008
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Java boolean method problem

    I have a java boolean method that finds out whether a record in an ArrayList collection exists or not, but when I try calling that method it seems to think that it isn't a boolean type.

    Code:
    System.out.println("Student ID >>");
       studID = keyboardMain.nextInt();
       UndergradMember u = new UndergradMember();
                    
       if(!loanExistsUndergrad(studID,membersList)){
          u.deleteUndergrad((ArrayList) membersList, studID);
       }
    
    public boolean loanExistsUndergrad(int studID, ArrayList <UndergradMember> undergradList){
       for (int i = 0; i < undergradList.size(); i++)
       {
          if(undergradList.get(i).returnStudID() == this.studID)
          {
             return true;
          }
       }       
       return false;
    }
    The error is that it returns an incompatible type "loanExistsUndergrad" instead of "boolean".

    I don't know why it doesn't think it's a boolean, can you help me understand why it's not working properly?

  2. #2
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post
    try using == false instead of the ! my only guess!

    also just noticed, is this a copy & paste from your real code?

    the method loanExistsUndergrad should be in the class not within the method. ie:
    Code:
    public void doSomething(){
    System.out.println("Student ID >>");
       studID = keyboardMain.nextInt();
       UndergradMember u = new UndergradMember();
                    
       if(!loanExistsUndergrad(studID,membersList)){
          u.deleteUndergrad((ArrayList) membersList, studID);
       }
    }
    //Notice it is now outside the above code!
    public boolean loanExistsUndergrad(int studID, ArrayList <UndergradMember> undergradList){
       for (int i = 0; i < undergradList.size(); i++)
       {
          if(undergradList.get(i).returnStudID() == this.studID)
          {
             return true;
          }
       }       
       return false;
    }
    Last edited by iainmackay85; 11-29-2008 at 12:30 AM.

  3. #3
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    The compilation error should also report which line number generated the error. Could you please update with exactly which line this error goes with?

  4. #4
    New Coder
    Join Date
    Nov 2008
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry if my previous code was confusing as I did a cut and paste job from bits and pieces of my code. Here is the Library class.

    Member is the superclass of LecturerMember and UndergradMember.

    Code:
    import java.io.*;
    import java.util.*;
    public class Library implements Serializable{
       public static ArrayList <Member> membersList = new ArrayList <Member>();
       public static ArrayList <Books> booksList = new ArrayList <Books>();
       public static ArrayList <Loans> loansList = new ArrayList <Loans>();   
       Scanner keyboardMain = new Scanner(System.in);
       private int menuChoice;
       private int studID;
       private String lectID;
       private int catalogueNo;
       
       public void addMember(){
          menuChoice = 0;
          String firstName;
          String surname;
          do{ 
             System.out.println("\n(1) Add a Student Member");
             System.out.println("(2) Add a Lecturer Member");
             System.out.println("(3) Return to Members Menu");
             System.out.println("Please Select a Choice between 1 and 3 >>");
             menuChoice = keyboardMain.nextInt();
          
             switch (menuChoice) {
                 case 1:
                    System.out.println("Student ID >>");
                    studID= keyboardMain.nextInt();
                    System.out.println("First Name >>");
                    firstName = keyboardMain.next();
                    System.out.println("Surname >>");
                    surname = keyboardMain.next();
                    membersList.add(new UndergradMember(studID,firstName,surname)); 
                    break;
                 case 2:
                    System.out.println("Lecturer ID >>");
                    lectID = keyboardMain.next();
                    System.out.println("First Name >>");
                    firstName = keyboardMain.next();
                    System.out.println("Surname >>");
                    surname = keyboardMain.next();
                    membersList.add(new LecturerMember(lectID,firstName,surname));
                    break;
                 case 3:
                    break;                  
                 default:
                    System.out.println("Invalid option, Please try again");
             }
          System.out.println("Size of membersList:= " + membersList.size());
          } while (menuChoice != 3);     
       }
     
       public void deleteMember(){   
          menuChoice = 0;
          do{
             System.out.println("\n(1) Delete a Student Member");
             System.out.println("(2) Delete a Lecturer Member");
             System.out.println("(3) Return to Members Menu");
             System.out.println("Please Select a Choice between 1 and 3 >>");          
             menuChoice = keyboardMain.nextInt();
    
             switch (menuChoice) {
                 case 1:
                    System.out.println("Student ID >>");
                    studID = keyboardMain.nextInt();
                    UndergradMember u = new UndergradMember();
                    if(loanExistsUndergrad(studID,membersList == false)){
                       u.deleteUndergrad((ArrayList) membersList, studID);
                    }
                    break;
                 case 2:
                    System.out.println("Lecturer ID >>");
                    lectID = keyboardMain.next();
                    LecturerMember l = new LecturerMember();
                    if(loanExistsLecturer(lectID,membersList == false)){                
                       l.deleteLecturer((ArrayList) membersList, lectID);
                    }
                    break;
                 case 3:
                    break;                  
                 default:
                    System.out.println("Invalid option, Please try again");
             } 
          } while (menuChoice != 3);         
       }
       
       public void findMember(){
          menuChoice = 0;
          do{
             System.out.println("\n(1) Find a Student Member");
             System.out.println("(2) Find a Lecturer Member");
             System.out.println("(3) Return to Members Menu");
             System.out.println("Please Select a Choice between 1 and 3 >>");
             Member m = new Member();
             menuChoice = keyboardMain.nextInt();
             switch (menuChoice) {
                 case 1:
                    UndergradMember u = new UndergradMember();
                    System.out.println("Student ID >>");
                    studID = keyboardMain.nextInt();                
                    u.undergradSearch((ArrayList) membersList, studID);
                    break;
                 case 2:
                    System.out.println("Lecturer ID >>");
                    lectID = keyboardMain.next();
                    m.lecturerSearch((ArrayList) membersList, lectID);
                    break;
                 case 3:
                    break;                  
                 default:
                    System.out.println("Invalid option, Please try again");
             } 
          } while (menuChoice != 3);          
       }
       
       public void addBook(){
          int noOfCopies;
          String bookTitle;
          String bookAuthor;
          System.out.println("Book Catalogue No >>");
          catalogueNo = keyboardMain.nextInt();           
          System.out.println("\nBook Title >>");
          bookTitle = keyboardMain.next();
          System.out.println("Book Author >>");
          bookAuthor = keyboardMain.next();     
          System.out.println("Number of Copies in Library >>");
          noOfCopies = keyboardMain.nextInt();
          booksList.add(new Books(catalogueNo, bookTitle, bookAuthor,noOfCopies));
     
       }
          
       public void deleteBook(){
          menuChoice = 0;
          do{
             System.out.println("\n(1) Delete a Standard Book");
             System.out.println("(2) Delete a Reference Book");
             System.out.println("(3) Return to Books Menu");
             System.out.println("Please Select a Choice between 1 and 3 >>");          
             menuChoice = keyboardMain.nextInt();
             
             switch (menuChoice) {
                 case 1:
                    System.out.println("Catalogue No >>");
                    catalogueNo = keyboardMain.nextInt();
                    Books b = new Books();
                    b.deleteBook((ArrayList) booksList, catalogueNo);
                    break;
                 case 2:
                    System.out.println("Catalogue ID >>");
                    lectID = keyboardMain.next();
                    Reference r = new Reference();
                    r.deleteReference((ArrayList) booksList, catalogueNo);
                    break;
                 case 3:
                    break;                  
                 default:
                    System.out.println("Invalid option, Please try again");
             } 
          } while (menuChoice != 3);        
           
       }
    
       public void findBook(){
          menuChoice = 0;
          do{
             System.out.println("\n(1) Find a Student Member");
             System.out.println("(2) Find a Reference Book");
             System.out.println("(3) Return to Books Menu");
             System.out.println("Please Select a Choice between 1 and 3 >>");          
             menuChoice = keyboardMain.nextInt();
             switch (menuChoice) {
                 case 1:
                    System.out.println("Catalogue No >>");
                    catalogueNo = keyboardMain.nextInt();
                    Books b = new Books();
                    b.bookSearch((ArrayList) booksList, catalogueNo);
                    break;
                 case 2:
                    System.out.println("Catalogue No >>");
                    catalogueNo = keyboardMain.nextInt();
                    Reference r = new Reference();
                    r.referenceSearch((ArrayList) booksList, catalogueNo);
                    break;
                 case 3:
                    break;                  
                 default:
                    System.out.println("Invalid option, Please try again");
             } 
          } while (menuChoice != 3);          
       }   
       
       public void loanBookOut(){
          menuChoice = 0;
             
          do{
             System.out.println("\n(1) Student Member");
             System.out.println("(2) Lecturer Member");
             System.out.println("(3) Return to Members Menu");
             System.out.println("Please Select a Choice between 1 and 3 >>");           
             menuChoice = keyboardMain.nextInt();
             
             switch (menuChoice) {
                 case 1:
                    System.out.println("Student ID >>");
                    studID = keyboardMain.nextInt();
                    System.out.println("Book Catalogue No. >>");
                    catalogueNo = keyboardMain.nextInt();
                
                    loansList.add(new Loans(studID,catalogueNo));
                    break;
                 case 2:
                    System.out.println("Lecturer ID >>");
                    lectID = keyboardMain.next();
                    loansList.add(new Loans(lectID,catalogueNo));
                    System.out.println("Book Catalogue No. >>");
                    catalogueNo = keyboardMain.nextInt();
                    break;
                 case 3:
                    break;                  
                 default:
                    System.out.println("Invalid option, Please try again");
             } 
          } while (menuChoice != 3);         
       }
       
       public void bookReturned(){         
          menuChoice = 0;
      
          do{
             System.out.println("\n(1) Student Member");
             System.out.println("(2) Lecturer Member");
             System.out.println("(3) Return to Members Menu");
             System.out.println("Please Select a Choice between 1 and 3 >>");           
             menuChoice = keyboardMain.nextInt();
             
             switch (menuChoice) {
                 case 1:
                    System.out.println("Student ID >>");
                    studID = keyboardMain.nextInt();
                    System.out.println("Book Catalogue No. >>");
                    catalogueNo = keyboardMain.nextInt();
                
                    break;
                 case 2:
                    System.out.println("Lecturer ID >>");
                    lectID = keyboardMain.next();
                    System.out.println("Book Catalogue No. >>");
                    catalogueNo = keyboardMain.nextInt();
                    break;
                 case 3:
                    break;    
                 default:
                    System.out.println("Invalid option, Please try again");
             } 
          } while (menuChoice != 3);              
       }
       
       public boolean loanExistsUndergrad(int studID, ArrayList <UndergradMember> undergradList){
           boolean b = false;
           for (int i = 0; i < undergradList.size(); i++)
           {
              if(undergradList.get(i).returnID() == studID)
              {
                 b = true;
              }
           }       
           return b;
       }
       
       public boolean loanExistsLecturer(String lectID, ArrayList <LecturerMember> lecturerList){
           boolean b = false;
           for (int i = 0; i < lecturerList.size(); i++)
           {
              if(lecturerList.get(i).returnID().equalsIgnoreCase(lectID))
              {
                 b = true;
              }
           }       
           return b;   
       }
       
       public boolean loanExistsBook(int catalogueNo, ArrayList <Books> bookList){
           boolean b = false;
           for (int i = 0; i < bookList.size(); i++)
           {
              if(bookList.get(i).returnCatalogueNo() == catalogueNo)
              {
                 b = true;
              }
           }       
           return b; 
       }
       
       public void saveDatabase() {
            // Write out Array to disk
           try {
               FileOutputStream save = new FileOutputStream("Saves");
               ObjectOutputStream out = new ObjectOutputStream(save);
               out.writeObject(membersList);
               out.writeObject(booksList);
               out.writeObject(loansList);
               out.flush();
               out.close();
           } catch (IOException e) {
               System.out.println(e);
           }
       }
    
        public static void loadDatabase() {
            //Read Array in from disk.
    
            try {
                try {
                    FileInputStream load = new FileInputStream("Saves");
                    ObjectInputStream in = new ObjectInputStream(load);
                    membersList = (ArrayList) (in.readObject());
                    booksList = (ArrayList) (in.readObject());
                    loansList = (ArrayList) (in.readObject());
                    in.close();
                } catch (IOException e) {
                }
            } catch (ClassNotFoundException f) {
            }
        }
    }
    Errors
    ====
    Code:
    I:\LibrarySystem\src\Library.java:79: incomparable types: java.util.ArrayList<Member> and boolean
                    if(loanExistsUndergrad(studID,membersList == false)){
    I:\LibrarySystem\src\Library.java:79: loanExistsUndergrad(int,java.util.ArrayList<UndergradMember>) in Library cannot be applied to (int,boolean)
                    if(loanExistsUndergrad(studID,membersList == false)){
    I:\LibrarySystem\src\Library.java:87: incomparable types: java.util.ArrayList<Member> and boolean
                    if(loanExistsLecturer(lectID,membersList == false)){                
    I:\LibrarySystem\src\Library.java:87: loanExistsLecturer(java.lang.String,java.util.ArrayList<LecturerMember>) in Library cannot be applied to (java.lang.String,boolean)
                    if(loanExistsLecturer(lectID,membersList == false)){

  5. #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    17,027
    Thanks
    4
    Thanked 2,668 Times in 2,637 Posts
    You're argument handling is incorrect, the bracket is in the wrong place:
    Code:
                    if(loanExistsUndergrad(studID,membersList == false)){
                       u.deleteUndergrad((ArrayList) membersList, studID);
                    }
    for these types of checks.

    Should be
    Code:
    if (loanExistsUndergrad(studID, membersList) == false)
    // OR
    if (!loanExistsUndergrad(studID, membersList))
    Same goes for the other ones as well.
    What you're original is doing is providing the values of int, boolean to you're method which is expecting an int and a List<Member>.
    Last edited by Fou-Lu; 11-30-2008 at 05:06 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  6. #6
    New Coder
    Join Date
    Nov 2008
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Can i request this thread to be deleted please?

  7. #7
    Senior Coder
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    psychofox19, in general we don't delete a thread once it has replies in it, as it affects other members. We can only edit out sensitive info such as usernames or passwords, which I don't see any in this thread. Sorry.
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •