Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 30

Thread: Keeping track of found objects in array and smallest value

  1. #1
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Keeping track of found objects in array and smallest value

    Hello I am trying to devise some code to store the index values of where I have found a book title and key code (which will be ISBN) in an array, I am stuck at here

    for (int k = 0; k < bkArr2.length; k++)
        {
    	if (bkArr2[k].getTitle().equals(titl) && bkArr2[k].getISBN() == ISB)
             {
    	System.out.println("Index Information At Index Arr[" + k + "]: " +  bkArr2[k].toString());
    	//bkArr2[k].getPrice();
              }
         }

    if there equal I print a line saying I found it but also want to store all the values found, there could be more then 1. Should I make a second array and store the values there?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Keeping track of found objects in array and smallest value

    An array would work, but it has maintenance problems. An ArrayList is much easier to use.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Keeping track of found objects in array and smallest value

    I have to keep my array like this in my book class:
    update:
    import java.util.Random;
    import java.util.Scanner;
    public class ModifyBooks {
     
    	public static void main(String[] args) {
     
    		 int i = 0, b = 0;
    		 String titl;
    		 long ISB;
     
    		 Book[] bkArr2 = new Book[10];
    		 int [] valueFound = new int[10];
     
    		 Random rand = new Random();
     
    		  Book b1 = new Book ("ho",10, 1001);
    		    Book b2 = new Book ("ho",45, 1001);
    		    Book b3 = new Book ("ho",50, 1001);
    		    Book b4 = new Book ("ho",100, 1004);
    		    Book b5 = new Book ("ho",75, 1005);
    		    Book b6 = new Book ("ho",65, 1006);
    		    Book b7 = new Book ("ho",40, 1007);
    		    Book b8 = new Book ("ho",10, 1008);
    		    Book b9 = new Book ("ho",10, 1009);
    		    Book b10 = new Book ("ho",100,1010);
     
    		    bkArr2[0] = b1;
    		    bkArr2[1] = b2;
    		    bkArr2[2] = b3;
    		    bkArr2[3] = b4;
    		    bkArr2[4] = b5;
    		    bkArr2[5] = b6;
    		    bkArr2[6] = b7;
    		    bkArr2[7] = b8;
    		    bkArr2[8] = b9;
    		    bkArr2[9] = b10;
     
    		    for (b = 0; b < bkArr2.length; b++)
    		    {
    		    	bkArr2[b].price = rand.nextInt(100);
    		    }
     
    		    Scanner scan = new Scanner(System.in);
     
    		    System.out.println("Please enter a book title: ");
     
    		    titl = scan.next();
     
    		    System.out.println("Please enter a ISBN number: ");
     
    		    ISB = scan.nextLong();
     
    		    for (int k = 0; k < bkArr2.length; k++)
    		    {
    		    	if (bkArr2[k].getTitle().equals(titl) && bkArr2[k].getISBN() == ISB)
    		    	{
    	      System.out.println("Index Information At Index Arr[" + k + "]: " +  bkArr2[k].toString());
    		    		k = valueFound[i];
    		    		i++;
     
    		    	}
    		    }
     
    	}
     
    }
    Last edited by paco; April 13th, 2014 at 12:12 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Keeping track of found objects in array and smallest value

    Do you have a question or problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Keeping track of found objects in array and smallest value

    how do I keep track of the values stored and then find the smallest of those values using my above code and my other class (down below), is the question.

    class Book {
    //Attributes of Book
     
    public String title;
    public long ISBN;
    public double price;
     
    //Constructor of Book
     
    public Book()
    {
    System.out.println("Creating Book.....");
    	title = "abc";
    	price = 25;
    	ISBN = 1234;
    }
     
    public Book(String ti, double pr, long ib)
    {
    	title = ti;
    	price = pr;
    	ISBN = ib;
    }
     
    //Methods of book
    public Book(Book bk)
    { //Copy Constructor
    System.out.println("Creating object with copy constructor.....");
    	title = bk.title;
    	price = bk.price;
    	ISBN = bk.ISBN;
    }
     
    public void setPrice(double pr)
    {//sets the price of the book
    	price = pr;
    }
    public double getPrice()
    {//gets the price of the book
    	return price;
    }
     
    public void setTitle(String ti)
    {//sets the title of the book
    	title = ti;
    }
     
    public String getTitle()
    {//gets the title of the book
    	return title;
    }
     
    public void setISBN(long num)
    {//sets the ISBN of the book
    	ISBN = num;
    }
     
    public long getISBN()
    {//gets the ISBN of the book
    return ISBN;
    }
     
    public String toString()
    {//returns book values
    	System.out.println("The book title is " + title + ", the ISBN number is " + ISBN + " and the price is " + price +"$.\n");
    	return (title + " " + ISBN + " " + price);
    }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Keeping track of found objects in array and smallest value

    how do I keep track of the values stored
    Save them in a list or array.

    find the smallest
    Search the list for the smallest.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Keeping track of found objects in array and smallest value

    thanks for stating the obvious, I was hoping for an example, but I guess you think I am being lazy and want you to spit out the answer...WRONG! I am deeply confused and need help, heres my attempt, please let me know if I am on the right track, its hard to wrap my head around thats why i ask for help!

     for (int k = 0; k < bkArr2.length; k++)
    		    {
    		    	if (bkArr2[k].getTitle().equals(titl) && bkArr2[k].getISBN() == ISB)
    		    	{
    		    System.out.println("Index Information At Index Arr[" + k + "]: " +  bkArr2[k].toString());
    		    		k = valueFound[i];
    		    		i++;
    		    	}
    		    }
    		    int minValue = 100000;
    		    for (j = 0; j < valueFound.length; j++)
    		    {
    		    	if (bkArr2[valueFound[j]].getPrice() < minValue)
    		    	{
    		    		minValue = valueFound[j];
     
    		    	}
     
    		    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Keeping track of found objects in array and smallest value

    What are the two loops for? Is this code only for finding the smallest value or is there other stuff mixed in with it?
    BTW That is what comments are for > To describe what the code is supposed to do.

    Have you worked out the logic for the search for the smallest value?
    Here's one way to search a list for the smallest value:
    set smallest value variable to the first element in the list
    begin loop starting at the second element in the list
    is this element < smallest value -> if it is, save this element in smallest value
    end loop
    //>>> smallest value has the smallest value
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Keeping track of found objects in array and smallest value

    the first for loop goes through my list checking for books with the same price and ISBN # and then stores where they are located in that array, into a separate array (i want to change the price after). The second for loop is looking for the smallest price amongst those found with the same title and ISBN. All books that were found with
    the same title and the same ISBN will then be changed to have the smallest price that any of them has.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Keeping track of found objects in array and smallest value

    If those comments were in the code, it would make the code easier to understand.

    Did you understand the pseudo code I posted for searching a list for the smallest value?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Keeping track of found objects in array and smallest value

    "set smallest value variable to the first element in the list
    begin loop starting at the second element in the list"

    whats wrong with the way I have it above? If I am checking all the values of the list, the first one will be the smallest and then so on I continue checking to see if the other elements are smaller than the first one and so on.


    heres code with comments, your right I think it will be easier this way to explain what I mean:

    //Go through my array check to see if ISBN and TITLE are equal
    for (int k = 0; k < bkArr2.length; k++)
    		    {
    		    	if (bkArr2[k].getTitle().equals(titl) && bkArr2[k].getISBN() == ISB)
    		    	{//if equal, display where in the index they are found, and store that value for later
    		    		System.out.println("Index Information At Index Arr[" + k + "]: " +  bkArr2[k].toString());
    		    		k = valueFound[i];
    		    		i++;
    		    	}
    		    } //check to see which is the smallest value because I want to change all of the found books with the same
                          //ISBN and TITLE to the smallest price amongst them
     
                          int minValue = 100000; //initialize a value that will be to big for it to be minvalue
    		    for (j = 0; j < valueFound.length; j++) //go through the array of values found that have same titile/isbn
    		    {
     
                           	if (bkArr2[valueFound[j]].getPrice() < minValue) //get price at that array value
    		    	{
    		    		minValue = valueFound[j]; //set it to the min so anything that comes after that is smaller will replace it
     
    		    	}
    		    }
    		    //Modify all books found  To Smallest Price amongst them
    		    for (int p = 0; p < valueFound.length; p++)
     
                       {
    		    	bkArr2[valueFound[p]].setPrice(minValue); //set the prices of all those values found to the smallest price
    		    	System.out.println("Price of Book at index "+  ); // and then display them, incomplete!
    		    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Keeping track of found objects in array and smallest value

    Does the code do what you want now?

    You have gone too far with the comments now. Several of them are redundant.
    Put a few lines of comments before a major loop describing its purpose. Most of the lines don't need comments because its obvious what they are doing.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Keeping track of found objects in array and smallest value

    I just ran it and got:

    Please enter a book title:
    ho
    Please enter a ISBN number:
    1001
    The book title is ho, the ISBN number is 1001 and the price is 70.0$.

    Index Information At Index Arr[0]: ho 1001 70.0
    The book title is ho, the ISBN number is 1001 and the price is 40.0$.

    Index Information At Index Arr[1]: ho 1001 40.0
    The book title is ho, the ISBN number is 1001 and the price is 40.0$.

    Index Information At Index Arr[1]: ho 1001 40.0
    The book title is ho, the ISBN number is 1001 and the price is 40.0$.

    Index Information At Index Arr[1]: ho 1001 40.0
    The book title is ho, the ISBN number is 1001 and the price is 40.0$.

    Index Information At Index Arr[1]: ho 1001 40.0
    The book title is ho, the ISBN number is 1001 and the price is 40.0$.

    Index Information At Index Arr[1]: ho 1001 40.0
    The book title is ho, the ISBN number is 1001 and the price is 40.0$.

    Index Information At Index Arr[1]: ho 1001 40.0
    The book title is ho, the ISBN number is 1001 and the price is 40.0$.

    Index Information At Index Arr[1]: ho 1001 40.0
    The book title is ho, the ISBN number is 1001 and the price is 40.0$.

    Index Information At Index Arr[1]: ho 1001 40.0
    The book title is ho, the ISBN number is 1001 and the price is 40.0$.

    Index Information At Index Arr[1]: ho 1001 40.0
    The book title is ho, the ISBN number is 1001 and the price is 40.0$.

    Index Information At Index Arr[1]: ho 1001 40.0
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at ModifyBooks.main(ModifyBooks.java:58)

    so not what I wanted

    System.out.println("Index Information At Index Arr[" + k + "]: " +  bkArr2[k].toString());
    		    		k = valueFound[i]; // <--- something wrong here
    		    		i++;

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Keeping track of found objects in array and smallest value

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at ModifyBooks.main(ModifyBooks.java:58)
    At line 58 the code used an index with value of 10 in an array that had less than 11 elements in it.
    Check the code to see why the value of the index went past the end of the array.
    Remember that array indexes range in value from 0 to the array length-1.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    paco (April 13th, 2014)

  16. #15
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Keeping track of found objects in array and smallest value

    reversed those 2 and my first part is working again.

    valueFound[i] = k;

    but now output:

    Please enter a book title: 
    ho
    Please enter a ISBN number: 
    1001
    The book title is ho, the ISBN number is 1001 and the price is 28.0$.
     
    Index Information At Index Arr[0]: ho 1001 28.0
    The book title is ho, the ISBN number is 1001 and the price is 6.0$.
     
    Index Information At Index Arr[1]: ho 1001 6.0
    The book title is ho, the ISBN number is 1001 and the price is 7.0$.
     
    Index Information At Index Arr[2]: ho 1001 7.0
    Price of Book at index 0has been modified from 28.0to 0
    Price of Book at index 1has been modified from 6.0to 0
    Price of Book at index 2has been modified from 7.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Last edited by paco; April 13th, 2014 at 03:53 PM.

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Keeping track of found objects in array and smallest value

    What was the size of the array where the error happened?
    How did the index for that array get the value: 10?


    was repeated so many times
    The code changes the value of the for loop control variable: k inside the loop.
    That should hardly ever be done. It is a sure way to mess up the looping.
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:

    paco (April 13th, 2014)

  19. #17
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Keeping track of found objects in array and smallest value

    yea got that part took some time though, i am very slow.

    update: i still am stuck at this, my problem is valueFound[i] = k; . I dont know how to get around this, I can't declare my second array thats stores values to be 10, because as you see below my code is checking all 10 elements.

    output:

     
    Please enter a book title: 
    ho
    Please enter a ISBN number: 
    1001
    The book title is ho, the ISBN number is 1001 and the price is 28.0$.
     
    Index Information At Index Arr[0]: ho 1001 28.0
    The book title is ho, the ISBN number is 1001 and the price is 6.0$.
     
    Index Information At Index Arr[1]: ho 1001 6.0
    The book title is ho, the ISBN number is 1001 and the price is 7.0$.
     
    Index Information At Index Arr[2]: ho 1001 7.0
    Price of Book at index 0has been modified from 28.0to 0
    Price of Book at index 1has been modified from 6.0to 0
    Price of Book at index 2has been modified from 7.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Price of Book at index 0has been modified from 0.0to 0
    Price of Book at index 0has been modified from 0.0to 0


    --- Update ---

    ripping hair out of skull

    i tried implementing a counter and I think this will finalize what I am trying to do:

     
    for (int k = 0; k < bkArr2.length; k++)
    		    {
    		    	if (bkArr2[k].getTitle().equals(titl) && bkArr2[k].getISBN() == ISB)
    		    	{
    		    		System.out.println("Index Information At Index Arr[" + k + "]: " +  bkArr2[k].toString()+ "\n");
    		    		valueFound[i] = k;
    		    		i++;
    		    		count++;
    		    	}
    		    }
    		    valueFound.length = count;

    but of course i get a red x on valueFound.length = count; final field array. length cannot be assigned....why not lol?
    Last edited by paco; April 13th, 2014 at 04:20 PM.

  20. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Keeping track of found objects in array and smallest value

    final field array. length cannot be assigned
    That value has the length of the array. You can NOT change the size of an array. You CAN define a new array of the desired size and copy the contents of the old array to the new array.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #19
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Keeping track of found objects in array and smallest value

    By looking at my code is that what you recommend I should do?

  22. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Keeping track of found objects in array and smallest value

    What are you trying to do with this statement:
    valueFound.length = count; // change the size of the array????
    If you don't understand my answer, don't ignore it, ask a question.

  23. #21
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Keeping track of found objects in array and smallest value

    Yes I am trying to change the size of the ray and make it equal to my count which would have been the amount of times where my title and ISBN are the same. That way only the ones that are found will be displayed and also the prices of those will be changed instead of displaying every element and changing every element.

  24. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Keeping track of found objects in array and smallest value

    to change the size of the ray
    See post #18
    To change size: define a new array of the desired size and copy the contents of the old array to the new array.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #23
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Keeping track of found objects in array and smallest value

    ok I will try now, thanks, thought there might have been another way/method

  26. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Keeping track of found objects in array and smallest value

    Use an ArrayList to hold the data and there is no need to resize it.
    If you don't understand my answer, don't ignore it, ask a question.

  27. #25
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Keeping track of found objects in array and smallest value

    I dont know what array list is, havent learned that yet, also if I were to move it to another array, say ar1 = ar2 wouldn't they just be the same size and the same outcome would happen?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: October 25th, 2013, 03:28 PM
  2. [SOLVED] Keeping Track
    By Nuggets in forum Loops & Control Statements
    Replies: 2
    Last Post: April 15th, 2012, 05:37 PM
  3. keep track of objects
    By keep smiling in forum Java Theory & Questions
    Replies: 4
    Last Post: February 20th, 2012, 03:44 PM
  4. keeping track of array taht holds object
    By jack_nutt in forum Java Theory & Questions
    Replies: 1
    Last Post: June 20th, 2011, 11:17 PM
  5. Keeping objects in webservice
    By ashleypursglove in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 16th, 2010, 12:02 PM