Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Bubble Sorter and 2d Array

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Bubble Sorter and 2d Array

    I am currently working on a program that is using 2d arrays and a bubble sorter to short a list of values and print them all. I am having issues with my program compiling and am not sure where my errors are. If anyone could assist me in debugging my program, it would be greatly appreciated. Thank you.

    import java.util.Scanner;
     
    public class SearchSort
    {
       public static void main(String[] args)
       {
      		final int MIN = 100,   // Minimum and maximum valid id values
    		          MAX = 199;   
     
    		int[] custId = { 105, 199, 108, 112, 151, 134, 128 };  // Array of customer IDs
     
          int id;                         // A single customer ID
    		int foundLocation;              // Array location of a searched for ID
     
    		System.out.println("First we will display the stored customer numbers.\n");
     
    		// WRITE A CALL TO THE displayValues method. PASS IT THE custId ARRAY.
    		displayValues(custId[id]);
     
     
          System.out.println("\n\nNow we will search for customer numbers.\n");
     
    		id = getInput(MIN, MAX);
     
    		while(id != 0)        // 0 is the sentinel value to quit
    		{
    			linearSearch(array,desiredValue);
    			foundLocation = linearSearch; 
     
             if (foundLocation == -1)                  
    			   System.out.println("The id " + id + " was not found."); 
    			else
                System.out.println("The id " + id + " was found in position " + 
    				                   (foundLocation + 1) );
     
    			id = getInput(MIN, MAX);
    		}
     
    		System.out.println("\n\nNow we will sort the customer numbers " + 
    		                   "and display them again.\n");
     
    		sortValues(custId[id]);
     
    		displayValues(custId[id]);
     
    		// WRITE A CALL TO THE sortValues method. PASS IT THE custId ARRAY.
    	 	// WRITE A CALL TO THE displayValues method. PASS IT THE custId ARRAY.
     
       }
     
     
       /**********************************************************
                              getInput
     
    	   This method inputs, validates, and returns an
    		integer in a specified range.
     
    		@param min The minimum valid value to accept (when not 0).
          @param max The maximum valid value to accept.
          @return The validated input.
    	***********************************************************/
    	public static int getInput(int min, int max)
    	{
    		int num;  // Local variable to hold an input until it can be
    		          // validated and returned.
     
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.print("\nEnter an integer between " + min + " and " + max +
    		                 " (or 0 to quit): ");
    		num = keyboard.nextInt();
     
    		while (id < MIN || id > MAX) 
    		{	
    			System.out.println("Invalid entry.");
    		   System.out.print("\nEnter an integer between " + min + " and " + max +
    		                    " (or 0 to quit): ");
    		   num = keyboard.nextInt();
    		}
    		return num;
     
    	}// End of getInput method	
     
     
       /**********************************************************
                            displayValues 
     
    	   This method displays all the values in an array of integers.
    		@param array The array holding the values.    
       ************************************************************/
       public static void displayValues(int[] array)
    	{
     
    		System.out.print(custId[id] + "   ");
    		// WRITE THE CODE TO DISPLAY ALL THE VALUES IN THE ARRAY.
    		// DISPLAY THEM ALL ON ONE LINE SEPARATED BY SEVERAL SPACES.
     
    	}// End of displayValues method	
     
     
       /**********************************************************
                          linearSearch 
     
    	   This method searches an array for a value.
    		It uses a linear search (i.e., a sequential search).
     
          @param array The array to search.
          @param value The value to search for.
          @return Subscript of the value (if found), otherwise -1.
       ************************************************************/
       public static int linearSearch(int[] array, int desiredValue)
       {
          int index;              // Loop control variable
          int pos = -1;           // Position where the desired value is found.
    		                        // -1 means it has not yet been found.
          boolean found = false;  // Flag indicating search results. 
     
          // Search the array. Element 0 is the starting point.
      		index = 0;
     
    		while (!found && index < array.length)
          {
             if (array[index] == desiredValue)
             {
        			found = true;        
             }
             index++;
          }
     
    		return array[index];
     
       }// End of linearSearch method
     
     
    	 /**********************************************************
                              sortValues 
     
    	   This method sorts the values in an array of integers
    		in ascending order.
     
    		@param array The array holding the values.    
       ************************************************************/
       public static void sortValues(int[] array)
    	{
    		int  temp;
    		boolean swapOccurred;
     
    		do
    		{	swapOccurred = false;
    			for (int pos = 0; pos < (array.length - 1); pos++)
    			{
    				if (array[pos] > array[pos+1])
    				{
    					temp = array[pos];
    					array[pos] = array[pos + 1];
    					array[pos + 1] = temp;
    					swapOccurred = true;					
    				}
    			}// end for
     	 	}while (swapOccurred);
     
    	}// End of sortValues method	
    }// End of SearchSort class declaration

    The errors are as below.

    SearchSort.java:22: error: method displayValues in class SearchSort cannot be applied to given types;
    		displayValues(custId[id]);
    		^
      required: int[]
      found: int
      reason: actual argument int cannot be converted to int[] by method invocation conversion
    SearchSort.java:31: error: cannot find symbol
    			linearSearch(array,desiredValue);
    			             ^
      symbol:   variable array
      location: class SearchSort
    SearchSort.java:31: error: cannot find symbol
    			linearSearch(array,desiredValue);
    			                   ^
      symbol:   variable desiredValue
      location: class SearchSort
    SearchSort.java:32: error: cannot find symbol
    			foundLocation = linearSearch; 
    			                ^
      symbol:   variable linearSearch
      location: class SearchSort
    SearchSort.java:46: error: method sortValues in class SearchSort cannot be applied to given types;
    		sortValues(custId[id]);
    		^
      required: int[]
      found: int
      reason: actual argument int cannot be converted to int[] by method invocation conversion
    SearchSort.java:48: error: method displayValues in class SearchSort cannot be applied to given types;
    		displayValues(custId[id]);
    		^
      required: int[]
      found: int
      reason: actual argument int cannot be converted to int[] by method invocation conversion
    SearchSort.java:77: error: cannot find symbol
    		while (id < MIN || id > MAX) 
    		       ^
      symbol:   variable id
      location: class SearchSort
    SearchSort.java:77: error: cannot find symbol
    		while (id < MIN || id > MAX) 
    		            ^
      symbol:   variable MIN
      location: class SearchSort
    SearchSort.java:77: error: cannot find symbol
    		while (id < MIN || id > MAX) 
    		                   ^
      symbol:   variable id
      location: class SearchSort
    SearchSort.java:77: error: cannot find symbol
    		while (id < MIN || id > MAX) 
    		                        ^
      symbol:   variable MAX
      location: class SearchSort
    SearchSort.java:98: error: cannot find symbol
    		System.out.print(custId[id] + "   ");
    		                 ^
      symbol:   variable custId
      location: class SearchSort
    SearchSort.java:98: error: cannot find symbol
    		System.out.print(custId[id] + "   ");
    		                        ^
      symbol:   variable id
      location: class SearchSort
    12 errors


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Bubble Sorter and 2d Array

    where my errors are
    Copy and paste here the full text of the error messages.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bubble Sorter and 2d Array

    Errors have been posted.

  4. #4
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Bubble Sorter and 2d Array

    required: int[]
    found: int
    The compiler found an int variable where there should have been a variable that was an array of int: int[]
    Change the arg being passed to the method to match what the method requires.
    arrayName[index] is an element of the array
    arrayName is the array

    cannot find symbol
    The compiler can not find a definition for the symbol/variable that is in the message. Check that the variable is defined and that the spelling matches.

Similar Threads

  1. Bubble Sort Help
    By asundar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 24th, 2012, 16:14
  2. array bubble sort
    By Olympaphibian89 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 1st, 2012, 17:53
  3. Sorter Project
    By Justin.ITPRO in forum Object Oriented Programming
    Replies: 1
    Last Post: May 22nd, 2012, 06:09
  4. Bubble Sort Int Array Problem
    By thisbeme in forum Collections and Generics
    Replies: 1
    Last Post: September 11th, 2011, 09:24
  5. Bubble Sort help
    By baueml01 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 5th, 2011, 20:47