1

good night. I'm trying to retrieve and compare an int variable value from an ArrayList (if that is possible) but no matter what I do it never works. I already tried methods like contains(), get() and others. My logic is really bad I guess, could someone help me ? Please?

    public class Obras extends Books implements ILibrary {

        protected ArrayList<Obras> ListObra = new ArrayList<Obras>();
        protected String typeObra;
        protected String statusBorrow;
        protected int ISBNuser;


        Scanner userInput = new Scanner(System.in);
        Scanner tipoInput = new Scanner(System.in);

        public void createnewObra()
            {
                System.out.println("Insert the type of the new item: [Book, article...");
                typeObra = tipoInput.nextLine();

                super.createnewObra();

            }

        ....

        public void addObra() {
            Obras newObra = new Obras();
            newObra.createnewObra();
            ListObra.add(newObra);
            System.out.println("> Uma nova obra foi adicionada com sucesso!\n");

        ....

public void BorrowObra() {

        System.out.println("> Choose a book from the list: ");
        showListObras();

            System.out.println("\n\n> Please choose one of the items from the above list by typing it's ISBN value: ");
                    ISBNuser = opcaoInput.nextInt();.

                    if(ListObra.get(ISBN).equals(ISBNuser))
                       {
                            System.out.println("> You successfully borrowed this book");
                            statusBorrow = false;
        }
6
  • I think he has override equals function in Obras, @sparkss can you paste your equals function? Commented Apr 27, 2015 at 1:28
  • Where is ISBN coming from? Commented Apr 27, 2015 at 1:31
  • Please isolate the few lines of code in your program that are surrounding the issue (i.e, the ones that involve reading ArrayLists), and only post that. In addition, please make it clearer what problem, exactly, is occuring. Is there an error message? Are you struggling with the conceptual side of it? Commented Apr 27, 2015 at 1:35
  • @DanielNugent - Hey, it's a variable from the class that Obras extends. Commented Apr 27, 2015 at 1:46
  • @chengpohi - if(ListObra.get(ISBN).equals(ISBNuser)) ISBN is an integer variable from another class and it is defined by the user in the createnewObra(). Commented Apr 27, 2015 at 1:49

2 Answers 2

1

To get an int from an ArrayList, your ArrayList would have to be defined along the lines of this:

ArrayList<Integer> arrayListOfIntegers = new ArrayList<Integer>();

or

List<Integer> listOfIntegers = new ArrayList<Integer>();

Your list appears to contain objects of class Obras.

Also, when you call ListObra.get(ISBN), this method is designed to return the object at the specified index within the list - I suspect ISBN is not an index in the list, rather an ISBN of a book?

On a separate note, try to stick to Java naming standards - variables start with lower case letters and methods use camel case (e.g. createNewObra()). It makes things easier for other developers to understand.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh thanks, yeah I noticed that now. ISBN is an integer variable that receives an user input and it is added to ListObras with another different types of variables. I guess that was my mistake. Yeah sorry about the Java naming standards.
0
ListObra.get(ISBN).equals(ISBNuser)

to:

Obras o = ListObra.get(ISBN);
if (o != null && o.getISBNuser() == ISBNuser)  {
    System.out.println("> You successfully borrowed this book");
    statusBorrow = false;
}

because you only get a object Obras and you doesn't Override equal function in Obras, so you need to get Integer ISBUser and equal to the user input.

Another Way:

Override equal:

public class Obras extends Books implements ILibrary {

@Override
public boolean equals(Object e) {
   Integer i = (Integer)e;
   if (this.ISBUser == i) return true;
   return false;
}
}

so now can use equals function to compare:

ListObra.get(ISBN).equals(ISBNuser)

2 Comments

Yeah that's what I'm trying to do @chengpohi. About your code, so for what I see I will have to create a get() method for ISBNuser? Both ISBN and ISBNuser are variable that get their values through user input and I want to compare both values.
I think also you can try the second way to do that.

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.