4 Replies - 1053 Views - Last Post: 23 March 2011 - 12:48 PM Rate Topic: -----

#1 Donjuan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 09

Custom methods for sorting/searching problems

Posted 23 March 2011 - 10:52 AM

I've got this program that I'm trying to write three new methods for:
1. a public void method 'sortByName' that sorts an array of Customer objects by name
2. a public void method 'sortByPurchase' that sorts an array of Customer objects by purchase
3. a method named searchByName that searches an array A of Customer objects for a Customer object whose name is equal to a parameter string. If such an object is found in the array A, the method returns the index i of such that A[i] is the object found; if such an object cannot be found, the method returns -1.

The program reads data from a file named indata.txt to create Customer objects of an array and returns the array.

Here is the program
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;


class prog5 {
  public static void main(String args[]) 
                throws FileNotFoundException
  {
    Scanner kb=new Scanner(System.in);

    Customer [] A =readArray(); //read data into objects in A

    while(true)
    {
      System.out.println();
      System.out.println();
      /*here, show prompt messages and menu*/
      System.out.println("Please select one of the follwing actions:");
      System.out.println("q - Quit");
      System.out.println("a - List the customer records sorted by customer name");
      System.out.println("b - Enter a customer name to find the customer's record");
      System.out.println("c - List the customer records sorted by purchase");
      System.out.println("Please enter q, a, b, or c:");

      String selection=kb.nextLine();  //read user's selection
      if (selection.equals("")) continue; //break; //if selection is "", show menu again

      switch (selection.charAt(0))
      {
        case 'a':
                sortByName(A);
                System.out.println("The customer list sorted by customer name:");  
                printArray(A);
                System.out.println();
                break;

        case 'b':
                sortByName(A);
                System.out.println("Please enter a customer name:");
                String n=kb.nextLine();
                for(int i=n.length(); i < 16; i++) n=n+" ";
                int k = searchByName(A, keyName);
                if (k < 0)
                  System.out.println("Cannot find customer named "+n);
                else
                  System.out.println("Customer found:\n"+A[k]);

                System.out.println();
                break;

        case 'c':
                //Arrays.sort(A, new CompareByPurchase());
                sortByPurchase(A);
                System.out.println("The customer list sorted by purchase:");
                printArray(A);
                System.out.println();
                break;

        case 'q':
                System.out.println("\nThank you.");
                return;

        default: System.out.println("Incorrect selection.\n");
      } //end switch
    } //end while
  } //end main();


  //the following method uses the data from indata.txt
  //to create Customer objects of an array
  //and returns the array

  private static Customer[] readArray()
                      throws FileNotFoundException
  {
    String name;
    double purchase;
    double rebate;

    Scanner infile=new Scanner(new File("indata.txt")); //input file

    int size=infile.nextInt();  //get number of lines
    infile.nextLine();          //skips end of line

    Customer A[]=new Customer[size];

    for (int k=0; k<size; k++)
    {
        //read a name = 16 characters
        infile.useDelimiter("");
        name="";

        for (int i=0; i<16; i++) name=name+infile.next();
        infile.reset();

        purchase=infile.nextDouble();
        rebate=infile.nextDouble();
        infile.nextLine(); //skip end of line

        A[k]=new Customer(name, purchase, rebate); //create object for A[i]

     } //end for loop

    infile.close();
    return A;

  } //end readArray

  //the method prints the Customer objects of the array A
  //one customer record per line
  
  private static void printArray(Customer [] A)
  {
   for (Customer x:A)
   {
     System.out.println(x);
   }

  } //end printArray
  
}  //end class prog4



class Customer implements Comparable <Customer>
{
  String name;
  double purchase;
  double rebate;

  public Customer(String n, double p, double r)
  { name=n;
    purchase=p;
    rebate=r;
  } //end Constructor
  
  public Customer(String n)
  { 
    name=n;
  } //end Constructor

  public String toString()
  {
    return String.format("%-20s%10.2f%10.2f", name, purchase, rebate);
  }

 public int compareTo(Customer a)
  {
    return name.compareTo(a.name);
  }

 public static void sortByName(Customer[] a)
 {
        int size = a.length;
        int i;
        int j;
        int minindex;
        Customer temp;

        for (i =0; i<size -1; i ++)
        {
          minindex = i;
          for(j=i+1; j<size; j++)
          if(a[j].name.compareTo(a[minindex].name) < 0)
          temp = a[i];
          a[i] = a[minindex];
          temp = a[minindex];
        }

}
 public static void sortByPurchase (Customer[] a)
 {
        int size = a.length;
        int i, j, minindex;
        Customer temp;
 
        for(i = 0; i < size - 1; i++)
        {
         minindex = i;
         for(j = i +1; j < size; j++)
         if(a[j].purchase.compareTo(a[minindex].purchase) < 0)
         temp = a[i]; a[i] = a[minindex]; temp = a[minindex];
        }
 }

 public static int searchByName(Customer[] a, String keyName)
 {
        int lastIndex = a.length -1;
        for (int i = 0; i <= lastIndex; i ++)
        if (a[i].name.equals(keyName)) return i;
        return -1;
 }

} //end class Customer



I'm getting several errors, which I think are just syntax and order issues that I can't seem to figure out, I'm pretty sure I have something in the wrong place and it's a somewhat simple fix, but I need a little assistance.
Here are the errors I get when I compile.

> Executing: C:\Program Files (x86)\ConTEXT\ConExec.exe "C:\Program Files\Java\jdk1.6.0_23\bin\javac.exe" "C:\Users\Matthew\Desktop\CS121\prog5.java"

C:\Users\Matthew\Desktop\CS121\prog5.java:35: cannot find symbol
symbol : method sortByName(Customer[])
location: class prog5
sortByName(A);
^
C:\Users\Matthew\Desktop\CS121\prog5.java:42: cannot find symbol
symbol : method sortByName(Customer[])
location: class prog5
sortByName(A);
^
C:\Users\Matthew\Desktop\CS121\prog5.java:46: cannot find symbol
symbol : variable keyName
location: class prog5
int k = searchByName(A, keyName);
^
C:\Users\Matthew\Desktop\CS121\prog5.java:57: cannot find symbol
symbol : method sortByPurchase(Customer[])
location: class prog5
sortByPurchase(A);
^
C:\Users\Matthew\Desktop\CS121\prog5.java:185: double cannot be dereferenced
if(a[j].purchase.compareTo(a[minindex].purchase) < 0)
^
5 errors
> Execution finished.

Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Custom methods for sorting/searching problems

#2 TFoSSDQ   User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Custom methods for sorting/searching problems

Posted 23 March 2011 - 11:08 AM

you're trying to call sortByName() from the Customer class directly in your main class without a class reference, it would have to be Customer.sortByName() as sortByName() is a static or class method.

keyName is never defined so Java will give ya a blank look.

for the last error, the double primitive data type does not have a compareTo method, the Double class however does
Was This Post Helpful? 1
  • +
  • -

#3 Donjuan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 09

Re: Custom methods for sorting/searching problems

Posted 23 March 2011 - 11:48 AM

View PostTFoSSDQ, on 23 March 2011 - 11:08 AM, said:

you're trying to call sortByName() from the Customer class directly in your main class without a class reference, it would have to be Customer.sortByName() as sortByName() is a static or class method.

keyName is never defined so Java will give ya a blank look.

for the last error, the double primitive data type does not have a compareTo method, the Double class however does


Wow, thanks, I knew I was leaving something simple out, I've gotten my code to compile without error and it runs, but it doesn't sort at all. I'm using the same code that I learned in class, so once again, I've left something out.
New code
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;


class prog5 {
  public static void main(String args[]) 
                throws FileNotFoundException
  {
    Scanner kb=new Scanner(System.in);

    Customer [] A =readArray(); //read data into objects in A

    while(true)
    {
      System.out.println();
      System.out.println();
      /*here, show prompt messages and menu*/
      System.out.println("Please select one of the follwing actions:");
      System.out.println("q - Quit");
      System.out.println("a - List the customer records sorted by customer name");
      System.out.println("b - Enter a customer name to find the customer's record");
      System.out.println("c - List the customer records sorted by purchase");
      System.out.println("Please enter q, a, b, or c:");

      String selection=kb.nextLine();  //read user's selection
      if (selection.equals("")) continue; //break; //if selection is "", show menu again

      switch (selection.charAt(0))
      {
        case 'a':
                Customer.sortByName(A);
                System.out.println("The customer list sorted by customer name:");  
                printArray(A);
                System.out.println();
                break;

        case 'b':
                Customer.sortByName(A);
                System.out.println("Please enter a customer name:");
                String n=kb.nextLine();
                for(int i=n.length(); i < 16; i++) n=n+" ";
                int k = Customer.searchByName(A, n);
                if (k < 0)
                  System.out.println("Cannot find customer named "+n);
                else
                  System.out.println("Customer found:\n"+A[k]);

                System.out.println();
                break;

        case 'c':
                Customer.sortByPurchase(A);
                System.out.println("The customer list sorted by purchase:");
                printArray(A);
                System.out.println();
                break;

        case 'q':
                System.out.println("\nThank you.");
                return;

        default: System.out.println("Incorrect selection.\n");
      } //end switch
    } //end while
  } //end main();


  //the following method uses the data from indata.txt
  //to create Customer objects of an array
  //and returns the array

  private static Customer[] readArray()
                      throws FileNotFoundException
  {
    String name;
    double purchase;
    double rebate;

    Scanner infile=new Scanner(new File("indata.txt")); //input file

    int size=infile.nextInt();  //get number of lines
    infile.nextLine();          //skips end of line

    Customer A[]=new Customer[size];

    for (int k=0; k<size; k++)
    {
        //read a name = 16 characters
        infile.useDelimiter("");
        name="";

        for (int i=0; i<16; i++) name=name+infile.next();
        infile.reset();

        purchase=infile.nextDouble();
        rebate=infile.nextDouble();
        infile.nextLine(); //skip end of line

        A[k]=new Customer(name, purchase, rebate); //create object for A[i]

     } //end for loop

    infile.close();
    return A;

  } //end readArray

  //the method prints the Customer objects of the array A
  //one customer record per line
  
  private static void printArray(Customer [] A)
  {
   for (Customer x:A)
   {
     System.out.println(x);
   }

  } //end printArray
  
}  //end class prog4



class Customer implements Comparable <Customer>
{
  String name;
  Double purchase;
  double rebate;

  public Customer(String n, double p, double r)
  { name=n;
    purchase=p;
    rebate=r;
  } //end Constructor
  
  public Customer(String n)
  { 
    name=n;
  } //end Constructor

  public String toString()
  {
    return String.format("%-20s%10.2f%10.2f", name, purchase, rebate);
  }

 public int compareTo(Customer a)
  {
    return name.compareTo(a.name);
  }

 public static void sortByName(Customer[] a)
 {
        int size = a.length;
        int i;
        int j;
        int minindex;
        Customer temp;

        for (i =0; i<size -1; i ++)
        {
          minindex = i;
          for(j=i+1; j<size; j++)
          if(a[j].name.compareTo(a[minindex].name) < 0)
          temp = a[i];
          a[i] = a[minindex];
          temp = a[minindex];
        }

}
 public static void sortByPurchase (Customer[] a)
 {
        int size = a.length;
        int i, j, minindex;
        Customer temp;
 
        for(i = 0; i < size - 1; i++)
        {
         minindex = i;
         for(j = i +1; j < size; j++)
         if(a[j].purchase.compareTo(a[minindex].purchase) < 0)
         temp = a[i]; a[i] = a[minindex]; temp = a[minindex];
        }
 }

 public static int searchByName(Customer[] a, String n)
 {
        int lastIndex = a.length -1;
        for (int i = 0; i <= lastIndex; i ++)
        if (a[i].name.equals(n)) return i;
        return -1;
 }

} //end class Customer




Was This Post Helpful? 0
  • +
  • -

#4 TFoSSDQ   User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Custom methods for sorting/searching problems

Posted 23 March 2011 - 11:55 AM

Ever heard of the Vegas rule? "What happens in Vegas stays in Vegas". It's the same for methods, what happens in methods stays in methods. Your two sorting methods are void methods which are messing with only the arguments and local variables to that method. Without returning anything, nothing outside of the method will change, that includes the Customer array that will be passed as an argument as it is defined elsewhere. You're going to want to change those to return methods.
Was This Post Helpful? 1
  • +
  • -

#5 Donjuan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 09

Re: Custom methods for sorting/searching problems

Posted 23 March 2011 - 12:48 PM

View PostTFoSSDQ, on 23 March 2011 - 11:55 AM, said:

Ever heard of the Vegas rule? "What happens in Vegas stays in Vegas". It's the same for methods, what happens in methods stays in methods. Your two sorting methods are void methods which are messing with only the arguments and local variables to that method. Without returning anything, nothing outside of the method will change, that includes the Customer array that will be passed as an argument as it is defined elsewhere. You're going to want to change those to return methods.


Well the directions I was given specifically say to use public static void methods, and I got my program to work using the following code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;


class prog5 {
  public static void main(String args[])
                throws FileNotFoundException
  {
    Scanner kb=new Scanner(System.in);

    Customer [] A =readArray(); //read data into objects in A

    while(true)
    {
      System.out.println();
      System.out.println();
      /*here, show prompt messages and menu*/
      System.out.println("Please select one of the follwing actions:");
      System.out.println("q - Quit");
      System.out.println("a - List the customer records sorted by customer name");
      System.out.println("b - Enter a customer name to find the customer's record");
      System.out.println("c - List the customer records sorted by purchase");
      System.out.println("Please enter q, a, b, or c:");

      String selection=kb.nextLine();  //read user's selection
      if (selection.equals("")) continue; //break; //if selection is "", show menu again

      switch (selection.charAt(0))
      {
        case 'a':
                Customer.sortByName(A);
                System.out.println("The customer list sorted by customer name:");  
                printArray(A);
                System.out.println();
                break;

        case 'b':
                Customer.sortByName(A);
                System.out.println("Please enter a customer name:");
                String n=kb.nextLine();
                for(int i=n.length(); i < 16; i++) n=n+" ";
                int k = Customer.searchByName(A, n);
                if (k < 0)
                  System.out.println("Cannot find customer named "+n);
                else
                  System.out.println("Customer found:\n"+A[k]);

                System.out.println();
                break;

        case 'c':
                Customer.sortByPurchase(A);
                System.out.println("The customer list sorted by purchase:");
                printArray(A);
                System.out.println();
                break;

        case 'q':
                System.out.println("\nThank you.");
                return;

        default: System.out.println("Incorrect selection.\n");
      } //end switch
    } //end while
  } //end main();


  //the following method uses the data from indata.txt
  //to create Customer objects of an array
  //and returns the array

  private static Customer[] readArray()
                      throws FileNotFoundException
  {
    String name;
    double purchase;
    double rebate;

    Scanner infile=new Scanner(new File("indata.txt")); //input file

    int size=infile.nextInt();  //get number of lines
    infile.nextLine();          //skips end of line

    Customer A[]=new Customer[size];

    for (int k=0; k<size; k++)
    {
        //read a name = 16 characters
        infile.useDelimiter("");
        name="";

        for (int i=0; i<16; i++) name=name+infile.next();
        infile.reset();

        purchase=infile.nextDouble();
        rebate=infile.nextDouble();
        infile.nextLine(); //skip end of line

        A[k]=new Customer(name, purchase, rebate); //create object for A[i]

     } //end for loop

    infile.close();
    return A;

  } //end readArray

  //the method prints the Customer objects of the array A
  //one customer record per line
  
  private static void printArray(Customer [] A)
  {
   for (Customer x:A)
   {
     System.out.println(x);
   }

  } //end printArray
  
}  //end class prog4



class Customer implements Comparable <Customer>
{
  String name;
  Double purchase;
  double rebate;

  public Customer(String n, double p, double r)
  { name=n;
    purchase=p;
    rebate=r;
  } //end Constructor
  
  public Customer(String n)
  { 
    name=n;
  } //end Constructor

  public String toString()
  {
    return String.format("%-20s%10.2f%10.2f", name, purchase, rebate);
  }

 public int compareTo(Customer a)
  {
    return name.compareTo(a.name);
  }

 public static void sortByName(Customer[] a)
 {
        int size = a.length;
        int i;
        int j;
        int minindex;
        Customer temp;

        for (i =0; i<size; i ++)
        {
          minindex = i;
          for(j = i + 1; j < size; j++)
          {
            if (a[j].name.compareTo(a[minindex].name) < 0)
            {
             minindex = j;
            }
          }
             if (minindex != i)
             {
                temp = a[i];
                a[i] = a[minindex];
                a[minindex] = temp;
             }
        }
 }
 public static void sortByPurchase (Customer[] a)
 {
        int size = a.length;
        int i, j, minindex;
        Customer temp;
 
        for(i = 0; i < size - 1; i++)
        {
         minindex = i;
         for(j = i +1; j < size; j++)
         {
               if(a[j].purchase.compareTo(a[minindex].purchase) < 0)
               {
                 minindex = j;
               }
         }
               if (minindex != i)
               {
                  temp = a[i];
                  a[i] = a[minindex];
                  a[minindex] = temp ;
               }
        }
 }

 public static int searchByName(Customer[] a, String n)
 {
        int lastIndex = a.length -1;
        for (int i = 0; i <= lastIndex; i ++)
        if (a[i].name.equals(n)) return i;
        return -1;
 }

} //end class Customer





But thanks for your help, I wouldn't have been able to solve it otherwise.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1