Object

I have such a problem :

in method fileReader I read file. And data in this file have different types. And I want mot to make a lot of getData methods for each type, but to make one getData method which will return an object with all of my data.

So 1: I need to make an object(structure) from different types of data I received.

2: Description of Getdata method which return object

3: How to use this method

Thanks

[465 byte] By [M_Timur] at [2007-9-26 1:16:16]
# 1
1:For example : I read a : int;b : String;c : byte;how to make an object theObject with fields a,b,c
M_Timur at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 2
Have a look into the java.io package. There are a number of methods to read various data type such as readDouble(), readInt() etc.Klint
saen at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 3

> 1:For example :

>

> I read a : int;

> b : String;

> c : byte;

> how to make an object

> theObject with fields a,b,c

public class MyData {

int a;

String b;

byte c;

// constructor

public MyData(int newA, String newB, byte newC) {

a = newA;

b = newB;

c = newC;

}

// add the getters

...

}

about reading your datas in a file, if you know the datas' size, i think it will be better for you to use randomAccessFile .

Hope this helps !

Badr.

zbadr at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 4

Hi Timur,

I still couldn't get what you exactly need. If you want to pass this Object as a Parameter in a Web Server, you can very well go for getParameter() method.

Or if it is an application, You can very well make the corresponding "casting" concept.

If you can able to send your sample code, I can give you the exact solution.

Thanks

Bakrudeen

bakrudeen_indts at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 5
I 've already read them , and now I need to combine them in one object. If somebody know say me please.Timur
M_Timur at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 6
Are the records and data structure always of the same length or do they vary in size?Klint
saen at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 7

My code is :

String FMT_ID, PATCH, NHM_B, NHM_A, HMTYPE, MONDEV = null;

int KDTRES, NRUN, LENHIS, NUMHIS, IBR, ICR, NTD, KHIDAF;

int[] NTINI = new int[16];

int[] NTO = new int[16];

int[] ENTFIN = new int[16];

int[] NSC = new int[3];

..........

protected void headerReader(){

try{

FMT_ID = readChar(0,2); //(0,2) 0 - number of byte from which FMT_ID begins in file .bin

//2 - number of char

KDTRES = readInt(2); // 2 = number of byte from which KDTRES begins in file .bin

NRUN = readInt(6); // my own methods because file was written in special format

PATCH = readChar(8,16);

LENHIS = readInt(28);

NUMHIS = readInt(30);

...................

}

catch{}

So you see I have a lot of different datas and it is a problem to make a lot getNameOfData methods.

And I want to combine all data in one object the Object and return it with method

getObject(){

return theObject;

}

Timur

M_Timur at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 8
mistake : need : and I want to combine all data in object theObject in method headerReader and return it with getObject method
M_Timur at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 9

Hi M_Timur,

Here is a sample code, which has a class file and it reads all the data types.

Here I have explained by getting the input from the Keyboard, Please modify by getting from the file.

mport java.io.*;

import java.util.*;

class EasyIn {

static InputStreamReader is = new InputStreamReader( System.in );

static BufferedReader br = new BufferedReader( is );

StringTokenizer st;

StringTokenizer getToken() throws IOException {

String s = br.readLine();

return new StringTokenizer(s);

}

boolean readBoolean() {

try {

st = getToken();

return new Boolean(st.nextToken()).booleanValue();

} catch (IOException ioe) {

System.err.println("IO Exception in EasyIn.readBoolean");

return false;

}

}

byte readByte() {

try {

st = getToken();

return Byte.parseByte(st.nextToken());

} catch (IOException ioe) {

System.err.println("IO Exception in EasyIn.readByte");

return 0;

}

}

short readShort() {

try {

st = getToken();

return Short.parseShort(st.nextToken());

} catch (IOException ioe) {

System.err.println("IO Exception in EasyIn.readShort");

return 0;

}

}

int readInt() {

try {

st = getToken();

return Integer.parseInt(st.nextToken());

} catch (IOException ioe) {

System.err.println("IO Exception in EasyIn.readInt");

return 0;

}

}

long readLong() {

try {

st = getToken();

return Long.parseLong(st.nextToken());

} catch (IOException ioe) {

System.err.println("IO Exception in EasyIn.readLong");

return 0L;

}

}

float readFloat() {

try {

st = getToken();

return new Float(st.nextToken()).floatValue();

} catch (IOException ioe) {

System.err.println("IO Exception in EasyIn.readFloat");

return 0.0F;

}

}

double readDouble() {

try {

st = getToken();

return new Double(st.nextToken()).doubleValue();

} catch (IOException ioe) {

System.err.println("IO Exception in EasyIn.readDouble");

return 0.0;

}

}

char readChar() {

try {

String s = br.readLine();

return s.charAt(0);

} catch (IOException ioe) {

System.err.println("IO Exception in EasyIn.readChar");

return 0;

}

}

String readString() {

try {

return br.readLine();

} catch (IOException ioe) {

System.err.println("IO Exception in EasyIn.readString");

return "";

}

}

// This method is just here to test the class

public static void main (String args[]){

EasyIn easy = new EasyIn();

System.out.print("enter char: "); System.out.flush();

System.out.println("You entered: " + easy.readChar() );

System.out.print("enter String: "); System.out.flush();

System.out.println("You entered: " + easy.readString() );

System.out.print("enter boolean: "); System.out.flush();

System.out.println("You entered: " + easy.readBoolean() );

System.out.print("enter byte: "); System.out.flush();

System.out.println("You entered: " + easy.readByte() );

System.out.print("enter short: "); System.out.flush();

System.out.println("You entered: " + easy.readShort() );

System.out.print("enter int: "); System.out.flush();

System.out.println("You entered: " + easy.readInt() );

System.out.print("enter long: "); System.out.flush();

System.out.println("You entered: " + easy.readLong() );

System.out.print("enter float: "); System.out.flush();

System.out.println("You entered: " + easy.readFloat() );

System.out.print("enter double: "); System.out.flush();

System.out.println("You entered: " + easy.readDouble() );

}

}

Then use the EasyIn Class for getting the data type.

EasyIn easy = new EasyIn();

int i = easy.readInt(); // gets an int from System.in

boolean b = easy.readBoolean(); // gets a boolean from System.in

double d = easy.readDouble(); // gets a double from System.in

... etc.

EasyIn is free, comes with source, and you can do what you like with it, including improve it, and send me back the results.

If, instead, you want to "roll your own" code (why?!), in JDK 1.0.2

java.io.DataInputStream in = new java.io.DataInputStream(System.in);

String s = in.readLine();

One way in JDK 1.1:

java.io.BufferedReader in =

new java.io.BufferedReader( new InputStreamReader(System.in));

String s = in.readLine();

Once you have the token in a String, it is easy to parse it into one of the other types, as shown earlier in the FAQ. Yes, it is bone-headed, as it makes the simplest case of keyboard I/O

unnecessarily complicated. A bug was filed with Javasoft to record this problem, but don't count on this being fixed any time soo

I hope this will help you.

Thanks

Bakrudeen

bakrudeen_indts at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 10

OK

Here's another example showing you how to read and write a RandomAccessFile. In your case forget the output. ou can easily adapt your application to read any kind of data.

import java.lang.*;

import java.io.*;

public class Testraf

{

public static void main(String [] args)

{

try

{

File fin = new File("infile.dat");

File fout = new File("test.out");

long fileSizein = fin.length();

final int RECORD_SIZE = 31;

RandomAccessFile rin = new RandomAccessFile(fin, "r");

RandomAccessFile rout = new RandomAccessFile(fout, "rw");

long filePointer = rin.getFilePointer();

byte bt;

bt = rin.readByte();

int i =0;

while (bt > 0)

{

i++;

rout.writeChar(bt);

bt = rin.readByte();

rout.writeChar(0);

}

System.out.println("Bytes read: " + i);

rin.close();

rout.close();

}

catch(java.io.IOException e)

{

System.out.println(e.toString());

}

}

}

Klint

saen at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 11

A lot of thanks to everybody.

But I need a little bit another idea.

It is not problem for me to read data.

I want to know how I can make an object(structure) from different types of data I've read, to use it like a parametr somewhere else.

It seems I explain very bad :)

M_Timur at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 12
Hi M_Timur,Please go through my code, there I am making a String Tokenizer Object. I am not clear that much in your query. Anyhow I will try to give the solution in a different way.ThanksBakrudeen
bakrudeen_indts at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 13

OK

Here I have a class called verbs. It is supposed to contain a dictionary between English and Japanese. As you can see in the declaration "String en, " etc. I have defined it like a structure which I keep in an object. Further down I have two "getters", getEnglish and getJapanese2.

Hope this will help a little.

Klint

import java.io.*;

public class Verbs

{

public Verbs(String en, String jap1, String jap2, String jap3,

String jap4, String jap5)

{

english= en;

japanese1 = jap1;

japanese2 = jap2;

japanese3 = jap3;

japanese4 = jap4;

japanese5 = jap5;

}

public Verbs()

{}

public String getEnglish()

{

return english;

}

public String getJapanese2()

{

return japanese2;

}

public void print()

{

System.out.println(english);

System.out.println(japanese1);

System.out.println(japanese2);

System.out.println(japanese3);

System.out.println(japanese4);

System.out.println(japanese5);

}

public void writeData(DataOutput out) throws IOException

{

DataIO.writeFixedString(english, WORD_SIZE, out);

DataIO.writeFixedString(japanese1, WORD_SIZE, out);

DataIO.writeFixedString(japanese2, WORD_SIZE, out);

DataIO.writeFixedString(japanese3, WORD_SIZE, out);

DataIO.writeFixedString(japanese4, WORD_SIZE, out);

DataIO.writeFixedString(japanese5, WORD_SIZE, out);

}

public void readData(DataInput in) throws IOException

{

english = DataIO.readFixedString(WORD_SIZE, in);

japanese1 = DataIO.readFixedString(WORD_SIZE, in);

japanese2 = DataIO.readFixedString(WORD_SIZE, in);

japanese3 = DataIO.readFixedString(WORD_SIZE, in);

japanese4 = DataIO.readFixedString(WORD_SIZE, in);

japanese5 = DataIO.readFixedString(WORD_SIZE, in);

}

public static final int WORD_SIZE = 20;

public static final int RECORD_SIZE = WORD_SIZE * 6 *2;

private String english;

private String japanese1;

private String japanese2;

private String japanese3;

private String japanese4;

private String japanese5;

}

saen at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 14

> And I want to combine all data in one object the

> Object and return it with method

> getObject(){

> return theObject;

> }

>

> Timur

I'm not sure i've understood what you want to do :

so create your object

public class MyData {

String FMT_ID, PATCH, NHM_B, NHM_A, HMTYPE, MONDEV = null;

int KDTRES, NRUN, LENHIS, NUMHIS, IBR, ICR, NTD, KHIDAF;

int[] NTINI;

// all fieds

...

// create a constructor with all parameters :

public MyData(String newFTM_ID, String newPATCH, String newNHM_A.....) {

FMT_ID = newFMT_ID;

PATCH = newPATCH;

....

}

}

Now in the class which perform the read operation add

a field :

MyData dataObject;

add also the getter :

public MyData getMyDataObject { return dataObject;}

in your headerReader make an instance of dataObject object with the fields you get back :

protected void headerReader(){

// your previous code

...

// create a MyData object

theData = new MyData(FTM_ID, PATCH, NHM_A...);

}

Hope this helps !

Badr.

zbadr at 2007-6-29 0:43:54 > top of Java-index,Archived Forums,Java Programming...
# 15

oops !

replace this line :

theData = new MyData(FTM_ID, PATCH, NHM_A...);

by :

dataObject = new MyData(FTM_ID, PATCH, NHM_A...);

what i've done here is :

instanciate the object in the readerHeader in order to access it with getMyDataObject() method.

Badr.

zbadr at 2007-6-30 21:27:16 > top of Java-index,Archived Forums,Java Programming...
# 16
Thank you Badr I'll think a little. Make a look here please some time later. I've got an idea. Want to try it.Thanks
M_Timur at 2007-6-30 21:27:16 > top of Java-index,Archived Forums,Java Programming...
# 17

Ok I have such simplified code.

Look it please. What I need to use in the line ** instead of ? to read object.

class MyData {

String str;

int intOne;

public MyData(String newStr, int newIntOne){

str = newStr;

intOne = newIntOne;

}

}

class readData{

MyData dataObject;

String str=null;

int intOne;

protected void headerReader(){

str="I am string";

intOne = 100;

dataObject = new MyData(str,intOne);

}

public MyData getObject(){return dataObject;}

}

public class test1 {

public static void main(String[] args) {

readData data = new readData();

data.headerReader();

/**/? = data.getObject

}

}

And what is the way to use ? fields

Thank you

M_Timur at 2007-6-30 21:27:17 > top of Java-index,Archived Forums,Java Programming...
# 18

Hi M Timur,

there are 2 ways to access ? fields :

1. declare all fields public then to access a field do :

MyData mData = data.getObject();

int myInt = mDate.intOne;

2. create a getter for each field in the class MyData:

public int getIntOne() { return intOne;}

public String getStr() {return str;}

}

and then you access fields :

int myInt = mDate.getIntOne();

String myStr = mDate.getStr();

Badr.

zbadr at 2007-6-30 21:27:17 > top of Java-index,Archived Forums,Java Programming...
# 19

don't really understand your problem but,

MyData could hold values that are equivalent, eg

class MyData {

private int iVal;

private String sVal;

private double dVal;

}

where each is the representation for the same value eg 100, "100", 100.0

then I would do this instead,

class MyData {

private double dVal;

public void set( ClassNeedsAsDouble a ) {

a.setThatValue( dVal );

}

public void set( ClassNeedsAsString b ) {

b.setThatValue( String.valueOf( dVal ) );

}

...

}

if the variable have 'unrelated' values,

class MyData {

private int iVal;

private String sVal;

private double dVal;

public void set( ClassExpectsTheString a ) {

a.setThatValue( sVal );

}

public void set( ClassExpectsTheInt b ) {

b.setThatValue( iVal );

}

...

}

you can also have get methods by value type for MyData users to call upon.

if you need something like a union, then it is slightly more complicated.

mchan0 at 2007-6-30 21:27:17 > top of Java-index,Archived Forums,Java Programming...
# 20
if you are wanting to use objects , shouldn't you be using serializable?
slinqi at 2007-6-30 21:27:17 > top of Java-index,Archived Forums,Java Programming...
# 21

> Hi M Timur,

> there are 2 ways to access ? fields :

> 1. declare all fields public then to access a field do

> :

> MyData mData = data.getObject();

> int myInt = mDate.intOne;

>

> 2. create a getter for each field in the class MyData:

>

> public int getIntOne() { return intOne;}

> public String getStr() {return str;}

> }

> and then you access fields :

> int myInt = mDate.getIntOne();

> String myStr = mDate.getStr();

>

> Badr.

>

Thank you Badr

I did the first way you advise. You gave me a vary good lesson! I'am quite young in Java.

By the way I didn't define any fields as public; and it works! Is it OK?

the last code (it works):

class MyData {

String str;

int IntOne;

public MyData(String newStr, int newIntOne){

str = newStr;

IntOne = newIntOne;

}

}

class readData{

MyData dataObject;

String str=null;

int IntOne;

protected void headerReader(){

str="I am string";

IntOne = 100;

dataObject = new MyData(str,IntOne);

}

public MyData getObject(){return dataObject;}

}

public class test1 {

public static void main(String[] args) {

readData data = new readData();

data.headerReader();

MyData mainObject = data.getObject();

int myInt = mainObject.IntOne;

String str = mainObject.str;

System.out.println(myInt);

System.out.println(str);

}

}

M_Timur at 2007-6-30 21:27:17 > top of Java-index,Archived Forums,Java Programming...
# 22
yes it is ;-)however i will advise you to use getter.good luck and thanx for the duke dollars !Badr.
zbadr at 2007-6-30 21:27:17 > top of Java-index,Archived Forums,Java Programming...