Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to pull out Unique values from database

 
Bal Sharma
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody:
I am trying to display unique value from Carrier, Origin and Destination to populate dynamically combo boxes. GUI part I have not done any thing yet. I added getDistinctValues method as below in Data class and trying to see unique information.

It compiles fine. But I am getting following message on DOS window.
A:\scjd\starting>javac suncertify\db\Data.java
A:\scjd\starting>java suncertify.db.Data
[Ljava.lang.Object;
I got only header on my DataOut file. That is it. What is heck �[Ljava.lang.Object;� ? Any help, clue, suggestion, comments please. Thanks in advance BK
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it means you have an array with one element that is an object. I used Vector, which might not be the best choice, but if you look at the returnArray declaration you will see how I got the values into a String array.
getFieldDataArray is the same method as your getUniqueValues method
private String[] getFieldDataArray(String fieldName){
Vector fieldData = new Vector();
String value;
try{
DataInfo[] records = dataAccess.criteriaFind("All");
for (int dataInfoNumber = 0; dataInfoNumber < records.length; dataInfoNumber++){
value = getFieldData(fieldName, records[dataInfoNumber]);
if (!fieldData.contains(value)){
fieldData.add(value);
}
}
} catch (Exception e){ }
String[] returnArray = (String[])fieldData.toArray(new String[fieldData.size()]);
return returnArray;
}
Hope this is readable for you.
Thanks
Mark
 
Bal Sharma
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark thanks for response. Still it is not working.
I do not know what is going on? This time I have following code.

I uncommented line 16 I got following output.
A:\scjd\starting>java suncertify.db.Data
SpeedyAir
PromptAir
RainvilleAir
BeethAir
[Ljava.lang.Object;
I commented line 16 now I have
A:\scjd\starting>java suncertify.db.Data
[Ljava.lang.Object;
It looks like to me line 15 is not working. It is not adding any element in vector. Any clue? Hope I am not being pain for you. Thaks BK

[This message has been edited by Bal Sharma (edited November 09, 2001).]
 
Bisi Adedokun
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
I checked your code and re-wrote it using an implementation of Set, HashSet. It works fine with reduce line of codes. The Set does not allow dups so that makes it straight forward. See the code and the file output below:
public Set getUniqueValues(String fieldName)
throws suncertify.shared.DatabaseException {
Set uniques = new HashSet();
int size = getRecordCount();
for (int i = 1; i <= size; i++) {
DataInfo dataInfo = getRecord(i);
String[] values = dataInfo.getValues();
FieldInfo[] fields = dataInfo.getFields();
for (int j = 0; j < fields.length; j++) {
String name = fields[j].getName();
if (name.equals(fieldName)) {
uniques.add(values[j].trim());
}
}
}
return uniques;
}
The main method is basically same as yours except the loop construct is different:
public static void main(String[] args) throws java.io.IOException {
PrintWriter outFile = null;
Data d = new Data("g:\\suncertify\\data\\db.db");
try {
outFile = new PrintWriter(new FileWriter("g:\\suncertify\\data\\DataOut.txt"), true);
outFile.println("Unique values ");
outFile.println("==============");
outFile.println();
Set set = d.getUniqueValues("Carrier");
Iterator iter = set.iterator();
while (iter.hasNext()) {
outFile.print(iter.next());
outFile.println();
}
} catch (DatabaseException de) {
System.out.println(de.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
outFile.close();
}
Then the output file has four entries:
Unique values
==============
SpeedyAir
PromptAir
BeethAir
RainvilleAir
I hope this helps.
Bisi
 
Bal Sharma
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bisi thanks, it is working. Still I am not sure why it did not work? while I changed into from set OR vector into String []. Like:
String[] returnArray = (String[])newValue.toArray();
return returnArray;
Who cares, right! I need distinct values. That is it. Thanks for guiding me. BK
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic