0 Replies - 1227 Views - Last Post: 11 December 2006 - 11:01 AM Rate Topic: -----

#1 w2lrnjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-December 06

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.
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);

		  itemCount = i;

		   //Call to the method sortPeoplesName to perform a selection sort on the list of names
		   //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", 
						  //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.showMessageDialog(null, itemSearch + " was not found.", "Search Result", 
				//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
		  // This is to properly terminate the program execution										

				   //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;
			 while(!found && first <= last)
				mid = (first + last) / 2;
				if(iNum[mid] == (Integer.parseInt(item)))
					 found = true;
		JOptionPane.showMessageDialog(null, "This program organizes a list of items and then performs "
											 + "a search on the list.", "Program Description", 
					if(iNum[mid] == (Integer.parseInt(item)))
						last = mid - 1;
						first = mid + 1;

   		   return mid;
			return -1;
	 //End of the program

Is This A Good Question/Topic? 0
  • +

Page 1 of 1