7 Replies - 302 Views - Last Post: 13 December 2012 - 05:39 AM Rate Topic: -----

#1 b5b5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-December 12

problem with array

Posted 12 December 2012 - 03:18 PM

Hello everybody;

I have a problem with array in this static method

we assume we have a 100 books and their titles (I defined an array of object in the main)

and some of them have a same title
so we want to define a method that search first five books that have the title which is entered by user


and i have errors with my code


 public static void findBooks (Book[] findB)//to display the first five books with the same title.
             {
                 Scanner input = new Scanner (System.in);
            int x=0;
            System.out.println("Searching for first five with same title ");
            findB = input..next().charAt(0);
                do{
                
      for (int i=0; i<100; i++){
         
              if (findB[i].getTitle().equals(findB[j].getTitle()) )
                  findB[j].display ();
              x++;
          
              }}while (x<5);
            }
             


Is This A Good Question/Topic? 0
  • +

Replies To: problem with array

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: problem with array

Posted 12 December 2012 - 03:34 PM

What is j ? Where is is defined
and what is that supposed to do

findB = input..next().charAt(0);

first the two .. should be only one and the char return by charAt(0) cannot be stored in findB which is an array of Book
Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: problem with array

Posted 12 December 2012 - 03:38 PM

The design of your method is wrong. Why would a findBooks method be responsible for getting user input? No cohesion.
Was This Post Helpful? 0
  • +
  • -

#4 b5b5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-December 12

Re: problem with array

Posted 12 December 2012 - 03:48 PM

Sorry for that , I forgot change it

 public static void findBooks (Book[] findB)//to display the first three books with the same title.
             {
                 Scanner input = new Scanner (System.in);
            int x=0;
            System.out.println("Searching for first five with same title ");

           findB = input.next().charAt(0);

                do{
                
      for (int i=0; i<100; i++){
         
              if (findB[i].getTitle().equals(findB) )
                  findB[i].display ();
              x++;
          
              }
      
            }while (x<3);
            }

Was This Post Helpful? 0
  • +
  • -

#5 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: problem with array

Posted 12 December 2012 - 04:26 PM

is the code working for you or not?
Was This Post Helpful? 0
  • +
  • -

#6 b5b5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-December 12

Re: problem with array

Posted 13 December 2012 - 05:03 AM

no it didn't
it failed in line 7 (in the code above )

i don't know what is true to define the array
Was This Post Helpful? 0
  • +
  • -

#7 Mike+9  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 104
  • Joined: 10-July 12

Re: problem with array

Posted 13 December 2012 - 05:30 AM

I don't know why you use the .charAt() function when you need a full String.

Quote

You can check to see which single character is in a particular string. The charAt method is used for this in Java. Here's some code to try:

String email_address = "meme@me.com";

char aChar = email_address.charAt( 4 );
System.out.println( aChar );

This code checks which letter as at position 4 in the email address string. The return value is a variable of type char:

char aChar = email_address.charAt( 4 );


So, you need to read the full book title not just the first string so try rewriting it.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: problem with array

Posted 13 December 2012 - 05:39 AM

I'm confused, where is the title you're searching for?

You'd think it would look something like:
public static void findBooks (Book[] books, String title) { // ...



What's with all the static? And what do you do with those books you found? Maybe return a list?

I'd go with something like:
class Books {
	private final Book [] items;
	Books() { /* define all those books here */ }
	public Book getBook(int index) { /* code here */ }
	public int getSize() { /* code here */ }
	
	// max 5 returned
	public Book [] findByTitle(String title) { /* code here */ }

	//...
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1