import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
//Name of the public class
public class StoreProgram
{
//The method main and file not found exception
public static void main(String[] args) throws
FileNotFoundException, IOException
{
//Declare the variables
int index;
int itemCount;
int itemNum;
int itemStock;
int i = 0;
String itemSearch;
String inputString;
boolean moreStock = true;
int[] itemNo = new int[10];
String[] itemDesc = new String[10];
double[] itemPr = new double[10];
int[] itemInv = new int[10];
//Create and Associate the stream objects
Scanner inFile = new Scanner(new FileReader("groceryinv.txt"));
PrintWriter outFile = new PrintWriter("groceryinvupdate.txt");
while (inFile.hasNext() && i < 10)
{
itemNo[i] = inFile.nextInt();
itemDesc[i] = inFile.next();
itemPr[i] = inFile.nextDouble();
itemInv[i] = inFile.nextInt();
//GroceryInv newItem = new GroceryInv(itemNo, itemDesc, itemPr, itemInv);
i++;
}
itemCount = i;
//Call to the method sortPeoplesName to perform a selection sort on the list of names
sortItems(itemNo,itemCount);
//Display the program's purpose
JOptionPane.showMessageDialog(null, "This program organizes a list of items and then performs "
+ "a search on the list.", "Program Description",
JOptionPane.INFORMATION_MESSAGE);
//Prompt the user for a name to search
itemSearch = JOptionPane.showInputDialog("Please enter the number that you want to search for: ");
//Call to the method binSearch to find the name requested by the user
index = binSearch(itemNo, itemCount, itemSearch);
//Condition to display the search results
if (index != -1)
JOptionPane.showMessageDialog(null, itemSearch + " was found.", "Search Result",
JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, itemSearch + " was not found.", "Search Result",
JOptionPane.INFORMATION_MESSAGE);
//Ask the user if they wish to search again
inputString = JOptionPane.showInputDialog(null, "Would you like to search again? (y/n): ");
//Close the input and output file
inFile.close();
outFile.close();
// This is to properly terminate the program execution
System.exit(0);
}
//Method to perform a selection sort
public static void sortItems(int[] iNum, int itemCount)
{
int i, j;
int min;
int temp;
for(i = 0; i < itemCount; i++)
{
min = i;
for(j = i + 1; j < itemCount; j++)
if(iNum[j] < iNum[min])
min = j;
temp = iNum[i];
iNum[i] = iNum[min];
iNum[min] = temp;
}
}
//Method to perform the binary search
public static int binSearch(int[] iNum, int itemCount, String item)
{
int first, last, mid = 0;
boolean found;
first = 0;
last = itemCount;
found = false;
Integer.parseInt(item);
while(!found && first <= last)
{
mid = (first + last) / 2;
if(iNum[mid] == (Integer.parseInt(item)))
found = true;
else
JOptionPane.showMessageDialog(null, "This program organizes a list of items and then performs "
+ "a search on the list.", "Program Description",
JOptionPane.INFORMATION_MESSAGE);
if(iNum[mid] == (Integer.parseInt(item)))
last = mid - 1;
else
first = mid + 1;
}
if(found)
return mid;
else
return -1;
}
//End of the program
}
Item Not Found in a Binary Search
Page 1 of 10 Replies - 1168 Views - Last Post: 11 December 2006 - 11:01 AM
#1
Item Not Found in a Binary Search
Posted 11 December 2006 - 11:01 AM
Ok, I had posted yesterday due to having import issues. Found the problem, so I am posting under a new string. I am working on a program that will eventually mimic a gui shopping cart. I want to get my import, sort and search going first. Problem now is my binary search it not finding my item #. This program is going to develop into being able to type in an item number and have a text field that will display the item descr, price and deduct the amount from inventory. Before I get to that, this is what I have. Can someone let me know if I am on the right track-- or not. Thanks.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote


|