This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with sort code

 
Monic Sherm
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to compile this code. I am trying to sort by author with the code. The error message says it is looking for a symbol for inventorySort which is the class that I created to do the sort. Any help would be appreciated.

Thanks in advance

//The main driver class for the Bookstore Inventory



import java.util.Scanner;
import java.io.*;

public class BookInventory
{
public static void main (String[] args) throws IOException
{
String inventory;
Scanner fileScan, InventoryScan;


Book[] bookData = new Book[500];
fileScan = new Scanner (new File ("inventory.txt"));


while (fileScan.hasNext())
{
inventory = fileScan.nextLine();
System.out.println(inventory);


InventoryScan = new Scanner (inventory);
InventoryScan.useDelimiter("/");

inventorySort.selectionSort(inventory);
System.out.println(inventory);
}
}
}
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where in this class do you create inventorySort. I was looking for something like
InventorySort inventorySort = new InventorySort();

or

is selectionSort(inventory) a static method in the InventorySort class? In that case I'd guess that you need to be more aware of Java's case sensitivity.
 
Monic Sherm
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the inventorySort class that I created. Any advice would be greatly appreciated


public class inventorySort
{

//-----------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort (Comparable[] list)
{
int min;
Comparable temp;

for (int index = 0; index < list.length-1; index++)
{
min = index;
for (int scan = index+1; scan < list.length; scan++)
if (list[scan].compareTo(list[min]) < 0)
min = scan;

// Swap the values
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
}
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declaration:

Call:


selectionSort is only defined for Comparable[] not for String. So you need to create the array first.
 
Monic Sherm
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I am confused, I thought that I declared the array as

Book[] = new Book [500]

the list is in the inventory.txt file.

Any help would be greatly appreciated.

Thanks
 
Monic Sherm
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If anyone can help me with this code, I would really appreciate it

Thanks in advance
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vlado is right. Your method selectionSort takes a Comparable[] parameter:



But you are passing inventory which a String object:



So Java is looking for a method like this:



which you haven't declared.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic