Java question

Sam334

Golden Member
Nov 20, 2004
1,150
0
0
Working on some homework and I've been stuck for a few hours.

I want to sort by a specific field in a database that is given by the user using compareTo.

I want to send that answer to the compareTo method so it can sort by a specific field. When I try to change the compareTo method I get an error...


I tried:

public int compareTo(Object obj, int answer)


but it tells me "DataBase is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable"
 

emmpee

Golden Member
Nov 26, 2001
1,100
0
0
Change

public int compareTo(Object obj, int answer)

to

public int compareTo(Object obj)

And then compare the two objects (the one that you "are" and the one passed in), and return an integer indicating which one is greater.
 

Sam334

Golden Member
Nov 20, 2004
1,150
0
0
Originally posted by: emmpee
Change

public int compareTo(Object obj, int answer)

to

public int compareTo(Object obj)

And then compare the two objects (the one that you "are" and the one passed in), and return an integer indicating which one is greater.



This is what I had:


public int compareTo(Object obj)
{
DataBase d2 = (DataBase)obj;
DataBase d1 = (DataBase)this;
if(d1.item[0*].compareTo(d2.item[0*]) > 0)
{
return 1;
}
else if(d1.item[0*].compareTo(d2.item[0*]) < 0)
{
return -1;
}
else
{
return 0;
}
} //end

If you look at the *'s above, that's what I want send in to the method. So it will compare the appropriate items. There are 4 fields, when the person indicated he wants to sort by first name, it's a 0, but last name, that should be a 1, etc.
 

emmpee

Golden Member
Nov 26, 2001
1,100
0
0
Originally posted by: Sam334
Originally posted by: emmpee
Change

public int compareTo(Object obj, int answer)

to

public int compareTo(Object obj)

And then compare the two objects (the one that you "are" and the one passed in), and return an integer indicating which one is greater.



This is what I had:


public int compareTo(Object obj)
{
DataBase d2 = (DataBase)obj;
DataBase d1 = (DataBase)this;
if(d1.item[0*].compareTo(d2.item[0*]) > 0)
{
return 1;
}
else if(d1.item[0*].compareTo(d2.item[0*]) < 0)
{
return -1;
}
else
{
return 0;
}
} //end

If you look at the *'s above, that's what I want send in to the method. So it will compare the appropriate items. There are 4 fields, when the person indicated he wants to sort by first name, it's a 0, but last name, that should be a 1, etc.

I'm confused. What type is this item[] array?
 

emmpee

Golden Member
Nov 26, 2001
1,100
0
0
Sorry man, don't have the time/energy to go through that much code, especially without tabstops :p
If you post it here, you might have more luck.
 

Sam334

Golden Member
Nov 20, 2004
1,150
0
0
Originally posted by: emmpee
Sorry man, don't have the time/energy to go through that much code, especially without tabstops :p
If you post it here, you might have more luck.


Here goes nothing.

I'm trying to sort by a specific field within db.txt that is read into an array by the tokenizer. The string reply is parsed into an int and I want to send that into to the compareTo method so it can use it as the thing it compares. I've been messing with this thing for 3 full days and I'm going out of my mind.

db.txt is below the program



//start

import java.util.*;

public class HW4
{

final static int MAX = 4;
final static int BIG = 16;
static int count;

public static void main(String[] asd)
{
DataBase[] dv = new DataBase[BIG];


readArray(dv);
spark(dv);


System.out.println();
}

public static int[] spark(DataBase[] x)
{
int[] answer = new int[1];

System.out.println("How would you like to sort the database?"
+ "\n1 = First name" + "\n2 = Last name"
+ "\n3 = Social Security number" + "\n4 = Age"
+ "\n5 = Exit");

StreamReader reader = new StreamReader(System.in);
BufferedReader buff = new BufferedReader(reader);
String s = buff.readLine();



int num = Integer.parseInt(s);

switch (num)
{
case 1: System.out.println("\nSorting by first name...\n");

final int sortby = 0;

// DataBase.assign(num);
answer[0] = 1;
Rays.sort(x);
printArray(x); break;

case 2: System.out.println("\nSorting by last name...\n");
Rays.sort(x);
printArray(x); break;
case 3: System.out.println("\nSorting by Social Security number...\n");
Rays.sort(x);
printArray(x); break;
case 4: System.out.println("\nSorting by age...\n");
Rays.sort(x);
printArray(x); break;
case 5: System.out.println("\nGoodbye.\n");
System.exit(1);
break;
default: System.out.println("Invalid entry, please retry.\n");
spark(x); break;
}

continyou(x);

return answer;
}

public static void continyou(DataBase[] x)
{

//readArray(x);

System.out.println("\nWould you like to continue?\n1 = Yes\n2 = No");

StreamReader reader = new StreamReader(System.in);

BufferedReader buff = new BufferedReader(reader);

String t = buff.readLine();

int num2 = Integer.parseInt(t);

if (num2 == 1)
{
spark(x);
} else

if (num2 == 2)
{
System.out.println("Goodbye.");
System.exit(1);
}

else
{
System.out.println("Invalid entry.");
continyou(x);
}
}

public static void readRecord(DataBase db, String line)
{
StringTokenizer toke = new StringTokenizer(line);
int j = 0;
while(toke.hasMoreTokens() )
{
db.item[j] = toke.nextToken();
j++;
}
}

public static void readArray(DataBase[] dv)
{
count = 0;
DataBase data ;
FileStringReader f = new FileStringReader("db.txt");
String line;
do
{
line = f.readLine();//reads a record
//System.out.println(line);
/*
* tests for null lines and blank lines so that they are not included
* in the array dv
*/
if(line != null && line.trim().length() != 0)
{
data = new DataBase();
readRecord(data, line);//data is a record w/4 fields
dv[count] = data;
count++;
}
}while(line != null);

}

public static void printArray(DataBase[] dv)
{
for(int j = 0; j < BIG; j++)
{
printRecord(dv[j]);
}
}

public static void printRecord(DataBase db)
{
for(int j = 0; j< MAX; j++)
{
System.out.print(db.item[j] + "\t" );
}
System.out.println();
}



}

class DataBase implements Comparable
{
final int MAX = 4;
String[] item = new String[MAX];


public int compareTo(Object obj)
{
DataBase d2 = (DataBase)obj;
DataBase d1 = (DataBase)this;
if(d1.item[0].compareTo(d2.item[0]) > 0)
{
return 1;
}
else if(d1.item[0].compareTo(d2.item[0]) < 0)
{
return -1;
}
else
{
return 0;
}
}
}

class Rays
{
public static void sort(Comparable[] x)
{
boolean sorted = false;
Comparable temp;
while (!sorted)
{
sorted = true ;
for(int j = 0; j < x.length - 1; j++)
{
//prototype is "public int compareTo(Object obj)"

if(x[j].compareTo(x[j+1]) > 0 )
{
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
sorted = false;
}
}
}
}
}


***********************************************************************
what follows is db.txt
************************************************************************
marin gil 269972639 34
abrams rita 005456788 17
nottobe tobe 029209330 19
backus jim 379228370 23
lucky lady 039470923 57
canada oh 437983890 46
caliper surgical 457474709 18
dante ron 090229292 29
english rum 457090290 35
goto state 507348278 19
home hume 387928783 23
identity crie 236982373 56
jackson andy 109290029 36
korn cob 329480928 25
faulty tower 439287983 12
abrams sol 098337377 54
 

sunase

Senior member
Nov 28, 2002
551
0
0
Lookup up Comparator in the API. That's the correct way to do this, not Comparable.
 

Sam334

Golden Member
Nov 20, 2004
1,150
0
0
Originally posted by: sunase
Lookup up Comparator in the API. That's the correct way to do this, not Comparable.

THe assignment requires us to use Comparable. It seems like it should be easy to do, but I just can't get it. Pass an integer to the compareTo method to use as the parameter to compare at....
 

sunase

Senior member
Nov 28, 2002
551
0
0
The closest you could could come to what you want is to have a separate static method on the class that gets called ahead of time and sets a static field that the compareTo method then checks when called to determine the ordering it should use.

That sort of state doesn't belong in the class, however, which is why Comparable is incorrect for what you are trying to use it for. An interface is a contract, if you change the method signatures you are implementing a different contract. How would methods like Collections.sort suddenly know to call compareTo differently? They wouldn't.
 

Sam334

Golden Member
Nov 20, 2004
1,150
0
0
Just figured it out! Woot. reply in the HW4 class is the number the user enters. 1 for first name, 2 for last, etc. Thanks for all the help.

public int compareTo(Object obj)
{
DataBase d2 = (DataBase)obj;
DataBase d1 = (DataBase)this;

int sortby = HW4.reply - 1;

if(d1.item[sortby].compareTo(d2.item[sortby]) > 0)
{
return 1;
}
else if(d1.item[sortby].compareTo(d2.item[sortby]) < 0)
{
return -1;
}
else
{
return 0;
}
}