Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array with mixed types??

Status
Not open for further replies.

Elle

Programmer
Sep 19, 2000
20
0
0
GB
Hi Guys,

I'm trying to read in a file, use a string tokenizer and then store the useful tokens into an array. I'm reading my file in line by line as i later need to compare the values on one line with the values on another line. So i'm trying to store each line as a new row within my array. But the problem i'm facing is that i have a record id (identifying the line number) which is an int but my tokens are strings. Is there a way that i can have an array of mixed types? i.e id, username, printer number, document name, pages printed.

Any suggestions?
Elle
 
Hi, Elle.

You can put different types in one array, of course, but this solution is not good and will bring you a lot of problems. For example you'll be forced to use "instanceof" every time when you get elements stored in this array in order to avoid ClassCastException.
There are other solutions and they're better than this... To create special structure that will contain fields you're gonna keep together. for example:

public class MyStructure
{
public String string;
public int id;
}

or you may use java.util.Map instead of array.

best regards
 
Cheers for the suggestion Artemyy,

But i've done that. I made a special class for myrecords...

public static class PrintingRecord {
public int id =0;
public String date;
public String username;
public String printer;
public String document;
public String pages;


}

But how would i store each line separately and so that i can easily compare lines to other lines? With Maps do i just then call each one by the id number then instead of the actual position? Can you have multidimensional maps aswell?

 
Hi, Elle.

First of all this structure should be declared as simple not as abstract class, because you will use many instances of it... The second is you have to add public constuctor for creating it in future. I should get something like that:

public class PrintingRecord {
public PritingRecord (int id_, String date_, and so on) {
id = id_;
date = date_;
// and so for all fields and arguments
}
public int id =0;
public String date;
public String username;
public String printer;
public String document;
public String pages;
}

afterwards you code where you use this structure should be like this one:
...
List array = new ArrayList();
PrintingRecord rec = new PrintingRecord (id, date, ...);
/*of course id, date and other arguments are declared and initialized higher*/

array.add (rec); //put you struc into map
....

//now you need to get and compare some of these structures

int index; //access index. That's just an example and of
// course you have to use your own wat how to access
// your structures in the list (see java doc for Collection
// and whole java.util package)

PrintingRecord rec1 = (PrintingRecord)array.get(index);
PrintingRecord rec2 = (PrintingRecord)arrey.get(index+1);

//now you have to compare them
//you can make it simply and use something like that

if (rec1.id == rec2.id) {
//do something
}

//or you can declare PrintingRecord as a class implementing
//interface java.lang.Comparable
//in this case you will have to implement compareTo method

for example:
public class PrintingRecord
implements java.lang.Comparable
{
//all what has been here before
....

public int compareTo (java.lang.Object objToCompare)
{
PrintingRecord rec = (PrintingRecord) objToCompare;
int result = (int) ((rec.id == id)&&(rec.date.equals
(date)&& ... &&(other field comparation));
}
}

and use it so:
if (rec1.compareTo (rec2)) {
//do something
}

use java.lang.Map if you need to get access through some kind of identifier but not through integer index...
in this case:
Map map = new HashMap();
Object key = //some identifier
map.put (key, rec);
...
PrintingRecord rec = (PrintingRecord)map.get(key);
//usage of rec later

That's all I guess
Good luck!
 
Cheers,

I followed your instructions. But when i go to compile and run i get an error msg...

java.lang.NoSuchMethodError: main
Exception in thread "main"

My main class was static as well but it produce another message cause my new class wasn't anymore could this by related?

Not sure if it was easier to show you my code or not, so i've just gone for it...

public class Test {
public void main(String[] args) throws IOException {

File output = new File("N:/output.txt");
FileWriter out = new FileWriter(output);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("N:/Printer.txt"))));
String line = "";
int tokenid = 0;
int id = 0;
String date = "";
String username = "";
String printer = "";
String document = "";
String pages = "";

List array = new ArrayList();
PrintingRecord myrecord = new PrintingRecord(id,date,username,printer,document,pages);

myrecord.id = 0;

while((line= reader.readLine()) != null) { //Keep on reading in lines until EOF
System.out.println(line); //reading in all lines ok
StringTokenizer st = new StringTokenizer(line, ",;:");

String token;
tokenid = 0;
myrecord.id++;

while(st.hasMoreTokens()){ //while there are still tokens to read
out.flush();

token = st.nextToken();
out.write(token); //output token to file
out.write('\n');

//assign variables to tokens
if (tokenid == 0){
myrecord.date= token;
}

if (tokenid == 8){
myrecord.username = token;
}

if (tokenid == 11){
myrecord.printer = token;
}

if (tokenid == 13){
myrecord.pages = token;
}

tokenid++;
}

System.out.println("Date " + myrecord.date + "Token ID is " + tokenid);
System.out.println("Username " + myrecord.username + "Token ID is " + tokenid);
System.out.println("Printer " + myrecord.printer + "Token ID is " + tokenid);
System.out.println("RecordID is " + myrecord.id);
//Send tokens and myrecord.id to an array
array.add(myrecord);

}
System.out.println("Array is holding: " + myrecord);
out.close();


}


public class PrintingRecord {
public PrintingRecord(int id_, String date_, String username_, String printer_, String document_, String pages_){
id = id_;
username = username_;
printer = printer_;
document = document_;
pages = pages_;

}
public int id =0;
public String date;
public String username;
public String printer;
public String document;
public String pages;


}

}
 
the main method has to be static.

public static void main(String[] args) {
.....
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top