File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Array Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Constructor" Watch "Array Constructor" New topic
Author

Array Constructor

c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
What constructor is most efficent to use to store the data of the file before extracting the data into an object?

I hope I am saying it correctly.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

I don't recall java having an Array class. It does have an "Arrays" class, but I don't think that class has a public constructor.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
java.lang.reflect.Array or java.sql.Array?


There is no emoticon for what I am feeling!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

Originally posted by Jeff Albertson:
java.lang.reflect.Array or java.sql.Array?


I knew while typing the response, that someone would call me on that ...

Henry
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by c york:
What constructor is most efficent to use to store the data of the file before extracting the data into an object?

I hope I am saying it correctly.

I'm still confused about the original question. By "constructor" do you actually mean "data structure"?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
I may not have been clear. Let me try again.

I have a file that I am trying to extract into an object using a constructor and populate the object(that is related to a class- using an array list) with the data .

I have created the object, but unsure what constructor to use.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18114
    
    8

Unclear? Yes, I agree.
I have a file that I am trying to extract into an object using a constructor...
Now, are you saying that this object uses a constructor and you are trying to extract into it, or that you are trying to use a constructor to extract into the object, or something else? Shorter and less gramatically-complex sentences might help. And I think we are all baffled why you are insisting on constructors here, although that should become clear when your question becomes clear.
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
I am confused myself.
You are correct is it - use a constructor to extract into the object.
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
It is use a constructor to extract into the object.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

What I usually ask in a situation like this is that the person try to describe what they need to accomplish, not how they're trying to do it. For example, in this case, you might say "I have a text file, and I want to read it in so that each line is a separate String", or something like that. Avoid terms whose meanings you're not 100% sure of, and I believe (forgive me) that "constructor" is in that category.


[Jess in Action][AskingGoodQuestions]
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
I have a text file that I am trying to use a constructor to extract the data into an object. I have looked at the String constructor and I am not sure which one to use and how to get the data into the object that was created.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

Originally posted by c york:
I have a text file that I am trying to use a constructor to extract the data into an object. I have looked at the String constructor and I am not sure which one to use and how to get the data into the object that was created.


Wow... It took a long while, but I think I finally got the question.

There is no way to contruct a string that is automatically mapped to the data in a file. However, there are a few options that will allow you to open a file and read strings from them. Take a look at the FileInputStream, FileReader, and RandomAccessFile classes.

Henry
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
Reading and writing the file has been done. I am now using the output file that was created and need to store the output into a class that contains the following for ex.
public String getT1_CNT() {
return t1_CNT;
}
/**
* @param t1_cnt The t1_CNT to set.
*/
public void setT1_CNT(String t1_cnt) {
t1_CNT = t1_cnt;
}

So, I created an object. I need to get the data into the object created and into the class which is another java file.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
C. York, I have carefully read the previous 12 threads, and still I can figure out what your question mean.

At times, I believe the word "constructor" is related to the Object Oriented Concept of constructors, that's to say, the method executed when you instantiate a new object. But after reading the whole question, I have impression that you may be referring to something else, like a method responsible to constructing something.

Whatever the case, your question as well as your previous example are both inintelligible for me.

Would you please explain me your question as if I were a 5 years old kid?
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
I thought we were suppose to be nice. Sorry, the subject is in reference to "constructor", however, the last reply did not mention it. It is a continuation of what I am trying to accomplish.

Where are the people with compassion?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

Originally posted by c york:
I thought we were suppose to be nice. Sorry, the subject is in reference to "constructor", however, the last reply did not mention it. It is a continuation of what I am trying to accomplish.

Where are the people with compassion?


C. York,

Trust me. We are not trying to be mean. We seriously do *not* have any idea what you are talking about !!

Think about it... This is a long running thread with a lot of people. Do you really think that many people can be mean for so long?

Henry
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Indeed. At this point I, for one, am personally invested in seeing you solve your problem. I'm very curious to see how this gets resolved.

But note that I asked you to restate your question without using the word "constructor", and yet you just repeated the same statement, and included the forbidden word. As a result, we haven't been able to test my belief (apparently shared by Edwin) that you mean something different by the word "constructor" than we are understanding.

To make this perfectly clear: a constructor is a special form of a Java method that takes the same name as the class in which it appears and has no return type. It can be used only in an object creation expression. In the following



the part in bold is the constructor, and you use it like this:

A myA = new A();

OK, now we all know what a constructor is.

In your last post, you say "I need to get the data into the object created and into the class which is another java file." So to me, it sounds as if you're reading a file, and you want to take the data in the file and call a buch of "setX()" methods on an instance of some class. That would just look like (assuming one object property per line in the file, only one object, and skimping on error handing:



So, are we getting closer? Nothing I've written here is particularly specific to constructors, nor is it related to arrays.
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
Yes, that was explained to me earlier. I am a java beginner and the first place were I learned about "constructor" was in the java.sun.com api website; this was all I knew and I was confused by the way someone explained it to me.

Now I understand and I can see the light at the end of the tunnel. Please forget the subject "Array Constructor" -- the way it was explained to me was not clear.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Now that the term "constructor" is clear, can you please explain WHAT you want to do. Don't worry about HOW to do it. That's what we are here to help you with. Also, you should avoid using the word "constructor" in your explanation since a constructor is a detail about HOW to accomplish the task.

So to help you get started, you might want to write some thing like this:

"I need to write a program that reads text from a file. And then..."

Please fill in the details from there about WHAT you need to do then we will help you figure out HOW to do it.


Java API Documentation
The Java Tutorial
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
The description for the exercise is as follows:
In this exercise, we will take the exercise 2 program and modify it. We will extract the applicant´┐Żs demographic information and some transaction related information from the EFT file. The information extracted will then be stored in an Applicant object. The Applicant class will be provided. In your application, you will need to create the Applicant object using correct constructor and populate the object with extracted values. You also need to implement the toString() method that will be used when output the Applicant object to a file.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
So if I understand correctly, you have been given a complete class called Applicant that has some constructors and presumably some methods for adding values, and what you need to do is create a new instance of the Applicant class, read some data from a file, add this data to your Applicant instance, and write information to another file using the String obtained by calling toString() on your Applicant instance. Is that correct?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3162
    
  10
Do you have the source code for the Applicant class ? If so can you post it or if it is too large just post the first line of any constructors (i.e. any methods called Applicant that don't have a return type specified). See EFH's earlier post for an example.

Don't forget to put the code in CODE tags.
[ April 07, 2006: Message edited by: Joanne Neal ]

Joanne
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
Yes, the Applicant class is very long. Here are the first few lines.
import java.io.*;

public class Applicant {
private String t1_LEN_1;
private String t1_VER;
private String t1_CNT;
.......

/**
* @return Returns the t1_CNT.
*/
public String getT1_CNT() {
return t1_CNT;
}
/**
* @param t1_cnt The t1_CNT to set.
*/
public void setT1_CNT(String t1_cnt) {
t1_CNT = t1_cnt;
}
/**
* @return Returns the t1_LEN_1.
*/
public String getT1_LEN_1() {
return t1_LEN_1;
}
/**
* @param t1_len_1 The t1_LEN_1 to set.
*/
public void setT1_LEN_1(String t1_len_1) {
t1_LEN_1 = t1_len_1;
}
.........
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3162
    
  10
Is there a line in the Applicant class that looks something like
There may be some parameter declarations between the ()
e.g.

These are the constructors. Can you post any line that looks anything like this.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Well, all that we know for sure about this class so far is that the variable names aren't very descriptive, and the documentation is no help. But is does show that there are at least two getXXX() and setXXX() methods, I presume that when you extract whatever demographic information is contained in the file you have to read from, you will have to call one or more of these methods. According to your instructions, you will also have to implement a toString() method that does something useful. Maybe you could show us the constructors that you have to work with, and as Joanne said, please dont forget to use code tags.
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
Yes, the Applicant method looks like this in the Applicant class.



public Applicant() {
}

The constructor that I am using is in the Applicant2 file:


public class Applicant2 extends Applicant{


public Applicant2(){
super();
}
public String ToString() {// Just for testing

return ("Printing String");
}

}

And the main method in the Exercise 3 file where I open the file -
public class Exercise3{
public static void main(String[] args)
{

Applicant2 appob = new Applicant2();
appob.ToString();
}
}

This approach is to help with testing; before reading the output file.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296


Does it look exactly like that, with no implementation? Are there any other constructors?

Also concerning your toString() method, Java is case sensitive so if you want to override the toString() method that your class inherits, you must use a lower-case t (toString() insted of ToString()). If you are using Tiger, one of the cool new features is using annotations to indicate that you intend to override a method.



BTW: I see you took a shot at using the code tags, thanks for that. For future referecnce the code goes between the tags like this. (without the spaces in the tags)
[ code]
//code goes here
[ /code]
[ April 07, 2006: Message edited by: Garrett Rowe ]
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
Yes, there is no implementation. The only constructor is the one that I created.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3162
    
  10
So now you create an Applicant and then use the setXXX methods to add the data you read from the file e.g.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Constructor
 
Similar Threads
javacaps mock 2 Q:8
Handling creation of new database
Funny Output
is it approved??
B&S: Data Constructors Signature