Page 3 of 4 FirstFirst 1234 LastLast
Results 41 to 60 of 70
Like Tree2Likes

Thread: Array question

  1. #41
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,920
    Rep Power
    5

    Default Re: Array question

    The first is the correct one. And I didn't notice earlier but in your Textbook constructor you are not saving your authors parameter. So it never gets initialized. If you need more help you should probably repost your code since it has undergone many changes.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  2. #42
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    I thought my addTextbook method in the bookstore class would keep me from having to call the textbook class in the tester?

    Java Code:
    public void addTextbook(Textbook newTextbook)
    		{
    			textbook.add(newTextbook);
    		}

  3. #43
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Textbook:
    Java Code:
    public class Textbook
    	{
    		/**
    			Identifies the textbook
    		*/
    		private String id;
    		
    		/**
    			Title of the textbook
    		*/
    		private String title;
    		
    		/**
    			List of authors
    		*/
    		private String[] authors;
    		
    		/**
    			Publisher information
    		*/
    		private String publisher;
    		
    		/**
    			Price of book
    		*/
    		private double price;
    		
    		/**
    			Status of book
    		*/
    		private int status;
    		
    		/**
    			Integer representing book instock
    		*/
    		private static final int INSTOCK = 1;
    		
    		/**
    			Integer representing book ordered
    		*/
    		private static final int ORDERED = 2;
    		
    		/**
    		 	Integer representing book status unknown
    		*/
    		private static final int UNKNOWN = 0;
    		
    		/**
    			Create a DecimalFormat object
    		*/
    		DecimalFormat fmt = new DecimalFormat("0.00");
    		
    		/**
    			Default constructor
    		*/
    		public Textbook()
    		{
    			id = "";
    			title = "";
    			this.authors = new String[1000];
    			publisher = "";
    			price = 0;
    			status = 0;
    		}
    		
    		/**
    		*/
    		public Textbook(String aId, String aTitle, String[] authors , String aPublisher, double aPrice, int aStatus)
    		{
    			id = aId;
    			title = aTitle;
    			publisher = aPublisher;
    			price = aPrice;
    			status = aStatus;
    			this.authors = new String[1000];
    						
    		}

  4. #44
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Bookstore:
    Java Code:
    public class Bookstore
    	{
    		/**
    		*/
    		
    		private ArrayList<Textbook> textbook;
    		
    		/**
    		*/
    		private ArrayList<Apparel> apparel;
    		
    		/**
    		*/
    		private String name;
    		
    		
    		/**
    		*/
    		public Bookstore()
    		{
    			name = "";
    		}
    
    
    		/**
    		*/
    		public Bookstore(String aName)
    		{
    			name = aName;
    			this.textbook = new ArrayList<Textbook>();
    			this.apparel = new ArrayList<Apparel>();
    		}
    		
    		/**
    		*/
    		public String getName()
    		{
    			return name;
    		}
    		
    		
    		/**		
    		*/		
    		public ArrayList<Textbook> getTextbooks()
    		{
    			ArrayList<Textbook>textbook2 = new ArrayList<Textbook>();
    			for(int i = 0; i < textbook.size(); i++)
    			{
    				textbook2.add(textbook.get(i));
    			}
    			return textbook2;
    		}
    		
                     public void addTextbook(Textbook newTextbook)
    		{
    			
    			textbook.add(newTextbook);
    		}
    }

  5. #45
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,920
    Rep Power
    5

    Default Re: Array question

    In your Textbook constructor, why are you allocating a new array for authors? Just do the following:

    Java Code:
    this.authors = authors;
    // in your default constructor, don't do anything, it defaults to null
    // or give it a null String as an  author
    this.authors = new String[] {""};
    //
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #46
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Where to I tell it how long the Array should be?

  7. #47
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,920
    Rep Power
    5

    Default Re: Array question

    You don't have too, your passing a reference and assigning that reference to a type of String[].

    Please copy and paste the following and run it. Then look and try to understand what it is doing.
    I urge you to buy a book and read it. Ask you instructor for a recommendation. And talk to him/her
    about these particular issues which you and perhaps others are having problems with.

    Java Code:
    public class StringArrayExample {
       public static void main(String[] args) {
          String[] foo = new String[]{"This","is","a","test!"};
          System.out.println(foo);
          StringArrayExample sae = new StringArrayExample();
          sae.arrayMethod(foo);
       }
       
       public void arrayMethod(String [] words) {
          System.out.println(words + " -- compare to original reference value\n");
          System.out.println("Length = " + words.length);
          for (int j = 0; j < words.length;j++) {
             System.out.println(words[j]);
          }
          System.out.println("--------------------------");
          // you can also do it this way -- preferred
          for (String w : words) {
             System.out.println(w);
          }
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #48
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,920
    Rep Power
    5

    Default Re: Array question

    Actually, that example didn't really illustrate the problem. And I apologize for that.

    Java Code:
    String [] a = new String[]{"This","is","a","test!"};
    String [] b;
    String [] c;
    b = a;
    c = b;
    Now a and c are identical arrays. They point to the same array of elements. If you change a value in c, it will be reflected in a and vice versa.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #49
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    OK, I think I get that now.

    but it would not be the same for the ArrayList in the Bookstore class because they have not been initialized as a param right?


    Java Code:
    public class Bookstore
        {
            /**
            */
            
            private ArrayList<Textbook> textbook;
            
            /**
            */
            private ArrayList<Apparel> apparel;
    
    
    
    
    public Bookstore(String aName)
            {
                name = aName;
                this.textbook = new ArrayList<Textbook>();
                this.apparel = new ArrayList<Apparel>();
            }
    or should it be this?
    (this is what I feel like it should be, even though both compile)
    Java Code:
    public Bookstore(String aName)
            {
                name = aName;
                this.textbook = textbook;
                this.apparel = apparel;
            }
    Trying to figure out why the getTextbook method is not working in the tester....I figure it has to do with how I set the ArrayList us...
    Last edited by jwood; 08-04-2013 at 06:19 AM.

  10. #50
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Evidently I have gotten something wrong in the body of this method, but I am not sure what exactly. To me it looks like it should work:
    Java Code:
    public void addTextbook(Textbook newTextbook)
    		{
    			
    			textbook.add(newTextbook);
    		}

    when I run my tester

    bookstore.addTextbook(new Textbook("TB1", "Big Java", new String[]{"Cay Horstmann"}, "John Wiley & Sons, Inc.", 129.99, 1));

    I get this error:

    Exception in thread "main" java.lang.NullPointerException
    at Bookstore.addTextbook(Bookstore.java:100)
    at BookStoreTester.main(BookStoreTester.java:16)

  11. #51
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Array question

    EDIT: HA! There were multiple pages in this thread. I'm BLIND.

    You get an error, lucky you! Java is telling you exactly what is going on and where: on line 100 of your Bookstore class, something is null when it shouldn't be. Now go look at that line and try to figure out what's wrong.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #52
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Quote Originally Posted by gimbal2 View Post
    EDIT: HA! There were multiple pages in this thread. I'm BLIND.

    You get an error, lucky you! Java is telling you exactly what is going on and where: on line 100 of your Bookstore class, something is null when it shouldn't be. Now go look at that line and try to figure out what's wrong.

    I understand what the error is saying as to where and what. The null confused me, instance variable was the problem not the method....
    Last edited by jwood; 08-08-2013 at 12:04 AM.

  13. #53
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Array question

    And why would that confuse you?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #54
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default Re: Array question

    Java Code:
    textbook.add(newTextbook);
    this line is throwing the null pointer exception.
    There is only one thing on this line that would cause that exception.
    So you now need to find out why this hasn't been initialised.
    jwood likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #55
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Quote Originally Posted by Tolls View Post
    Java Code:
    textbook.add(newTextbook);
    this line is throwing the null pointer exception.
    There is only one thing on this line that would cause that exception.
    So you now need to find out why this hasn't been initialised.
    Thanks, I figured that part out.

    Where is my thinking wrong on this part?

    Directions: getTextbooks (String name) returns a list of Textbook objects whose title matches the specified name. For example, if called with "Java Programming" the method returns all textbooks with the title "Java Programming" as a new list. This method must create a new copy of an ArrayList that stores all the matched Textbook objects. If no books in the bookstore match the given name, an empty list is returned.

    my method:
    Java Code:
    public ArrayList<Textbook> getTextbooks(String name)
            {
                ArrayList<Textbook>textbook3 = new ArrayList<Textbook>();
                for(int i = 0; i < textbook.size(); i++)
                {
                    if(name.equals(textbook.get(i)))
                        {
                        textbook3.add(textbook.get(i));
                        return textbook3;
                        }
                }    
                return null;
            }
    My tester:
    Java Code:
    System.out.println("Enter textbook name: ");
    String c = keyboard.next();
    if(bookstore.getTextbooks(c)== null)
        System.out.println("Textbook not found.");
    else
    System.out.println(bookstore.getTextbooks(c));
    When I run the tester it always prints "textbook not found"

    I know the books are there because I can print the ArrayList and they all show up, just can't seem to search the ArrayList properly. I have been staring at this all day and just don't see the error in my logic....
    Last edited by jwood; 08-09-2013 at 01:10 AM.

  16. #56
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default Re: Array question

    Java Code:
    name.equals(textbook.get(i))
    You are comparing a String with a Textbook.

    I assume you actually want to be comparing the 'name' with the name held in the textbook object.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #57
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Quote Originally Posted by Tolls View Post
    Java Code:
    name.equals(textbook.get(i))
    You are comparing a String with a Textbook.

    I assume you actually want to be comparing the 'name' with the name held in the textbook object.
    Yes, that is what I thought I was doing....snap.....ok how do you search an array list for part of the object?

    I have a getTitle method in my Textbook class...but I haven't been able to figure out how to call a Textbook class method to search my ArrayList textbook objects...

    when I try something like this for a linear search

    textbook.getTitle() == name

    I get a cannot find symbol on the getTitle method.

  18. #58
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default Re: Array question

    Quote Originally Posted by jwood View Post
    textbook.getTitle() == name

    I get a cannot find symbol on the getTitle method.
    Check your Textbook class if exactly that method exists (and is public); also you can't compare Strings for equality with the == operator; read the API documentation for a simple alternative (that also applies to other types). A small statistical note: the probability for a thread to end successfully is inversely proportional to the length of the tread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #59
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Yes, I figured that one out. The problem I am facing now is the whole make a copy of the ArrayList and the return having to be the array list(so I know the return is wrong)...not so sure how to do that. Here is where I am so far (but I think I butchering it)

    Java Code:
    public ArrayList<Textbook> getTextbooks(String name)
            {
                
                for(Textbook myTitle : textbook)
                
                {
                    if(myTitle.getTitle().equals(name))
                        {
                        
                        return myTitle;
                        }
                }    
                return null;
            }
    Last edited by jwood; 08-09-2013 at 08:47 PM.

  20. #60
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    I think I am making a complete mess of this lol!

    Java Code:
    public ArrayList<Textbook> getTextbooks(String name)        {
                ArrayList<Textbook>textbook3 = new ArrayList<Textbook>();
                for(Textbook myTitle : textbook)
                {    
                    for(int i = 0; i < textbook.size(); i++)
                    {
                      textbook3.add(textbook.get(i));
    
    
                    if(textbook3.get(i).getTitle().equals(name))
                     
                        {
                        
                        return textbook3.get(i);
                        }
                    }
                }    
                return null;
            }
    error
    Bookstore.java:96: error: incompatible types LINE 14
    return textbook3.get(i); Arrow points to the first paren.
    ^
    required: ArrayList<Textbook>
    found: Textbook
    1 error
    Last edited by jwood; 08-09-2013 at 09:40 PM.

Page 3 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. Question about a 2d array example
    By silverglade in forum New To Java
    Replies: 8
    Last Post: 06-07-2011, 12:56 AM
  2. Array question..
    By gerarda in forum New To Java
    Replies: 14
    Last Post: 03-02-2011, 02:42 AM
  3. Question about array
    By hei1233212000 in forum New To Java
    Replies: 2
    Last Post: 09-18-2010, 04:55 PM
  4. Array question
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 06-11-2010, 02:41 AM
  5. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 03:18 AM

Posting Permissions

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