Constructor for importing file [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
If I wanted to build a constructor for a class to import a file, which I have passed in a String nameOfFile, how can I initialize the object's state, then open the document file, and process each line of the document? References and clear explanation would be appreciated. I'm just beginning to learn java.
To clear up my question, how can you build an object for a document file you have imported? What I'm doing right now is that I have wrote a class but i'm struggling with building an object for a SPECIFIC file. What I have thus far is
public class theImport
{
theImport(String nameOfFile)
{
(Here is where I want to achieve all the listing I have above.)
}
.
.
.
}

I believe you would do this in a two-step process.
Step one: The actual constructor.
private String nOF;
public ClassName(String nameOfFile) {
nOF = nameOfFile;
}
Step two: Evaluation of the file. Since this can fail for a variety of reasons (for example the file not existing, it shouldn't go to the constructor (you can't catch these errors from a return type you aren't having in the constructor)
public boolean Evaluate() {
//Evaluate your file and return false if it fails for whatever reason.
}
My Java isn't the best at the moment, but this should tip you in the right direction.

Propably you mean something like this:
public class theImport
{
theImport(String nameOfFile)
{
try {
FileReader input = new FileReader(nameOfFile);
BufferedReader bufRead = new BufferedReader(input);
String line;
line = bufRead.readLine();
//this will loop thought the lines of the file
while (line != null){
line = bufRead.readLine();
//do whatever you want with the line
}
bufRead.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}

Related

easier way to format lines of code in Java to make your job easier? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am currently making an application that uses an api and it prints out information about that thing. So basically it gets the api and If i do System.out.println(result.getPlayer().get("displayname")); it will return the display name of the player that I am searching for. I was wondering if there was a way to make result.getPlayer().get("displayname") a variable because I have hundreds of statistics that I need to gather. so is it possible to have that line of code called displayname? Sorry if you don't understand.
I suggest that you make a special statistics/logging class that has static methods specifically for this. For example with your case, the following class can be used both to get the name and to print it. Of course you can combine them into a single method if you want just one functionality.
public class StatsLog {
public static String getPlayerDisplayName(final Result result) {
return (result == null)
? null
: result.getPlayer().get("displayname");
}
public static void printPlayerDisplayName(final Result result) {
final String displayName = getPlayerDisplayName(result);
if (displayName != null) {
System.out.println(displayName);
}
}
}
And when you call it:
StatsLog.printPlayerDisplayName(result);
You can use a getter like #Andrew Tobilko said. like this:
public String getDisplayName(){
return (result != null)? result.getPlayer().get("displayname") : "";
}
However, it depends on what is the "result" and the design of your class. And make sure to check for Null.

Looping through Object of Objects in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an object which in turn contains other objects. Now I have to iterate through this main object and then pick each object and then iterate through them to find out whether any empty fields are present in them. If the object itself is empty, I have to cut it out of main object. Any thoughts on this please.
public class Transactions {
private Integer totalTransactionCount = null;
private List<Transaction> transactionsList = new ArrayList<Transaction>();
}
public class Transaction {
private String amount = null;
private Foreign foreign = null;
}
public class Foreign {
private String amount = null;
private String commissionAmount = null;
private String exchangeRate = null;
}
Now I have a Transaction object with me and I have to loop throught each of its fields and in turn loop through their fields to find out any null/empty fields.
pseudo code for looping through a list of lists:
for each (innerList in outerList) do
if(innerlist.size == 0) then
//Code for removing empty inner lists.
else
for each ( object in innerList) do
//Check if objects are empty as well and remove it
end for
end if
end for
EDIT: Pointing out lack of research.
I would like to point out that you haven't really done your research properly, simply by googling iterate list of object as well as iterate list of list of object I got plenty of solutions.
Not to mention a question already asked here on Stack Overflow, please read the first answer of this post

Need bindings equivalent of String.replaceAll(...) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to create a binding that allows me to do the equivalent of String.replaceAll(...) but with bindings. I have a string, "${driver} driving ${name}", and I want the keys, "${driver}", etc. to be replaced with the specific property. I also want the returned property to be able to add listeners so when driverProperty or another changes, the returned property value will change without having to re-call getString().
public String getString(Derby derby) {
String ret;
if (driverProperty.get().equals("") && nameProperty.get().equals("") && numberProperty.get().equals("") && groupProperty.get().equals("")) {
ret = "[blank]";
} else {
ret = (String) derby.getSettings().get("general.cardisplay").getValue();
ret = ret.replace("${driver}", driverProperty.get());
ret = ret.replace("${name}", nameProperty.get());
ret = ret.replace("${number}", numberProperty.get());
ret = ret.replace("${group}", groupProperty.get());
}
return ret;
}
Use Bindings.createStringBinding(). This takes a function supplying the String and a list of values to observe; if any of those values change the binding is marked as invalid.
Your question isn't too clear to me, but I think you can do something like
StringBinding formattedString = Bindings.createStringBinding(
() -> getString(derby),
derby.settingsProperty(),
nameProperty, numberProperty,
driverProperty, groupProperty);
Now you can do things like
formattedString.addListener((obs, oldFormattedString, newFormattedString) -> {
// invoked any time the formatted string changes...
});
or
Label label = new Label();
label.textProperty().bind(formattedString);

If something name is one of file name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
im creating minecraft sectors system..
I want to get worldguard region what player is actually.
All regions are saved into files like (Regions/region1.yml, region2.yml)
Now my question is:
How can i list all files in folder regions to String list?
i need it to do something like this
if(e.getregion.getiid.contains(list1) {
//do something
}
you can use list function of File class http://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()
File regionFolder = new File("path/to/Regions/folder");
String[] regionFile = regionFolder.list();
If you want to leverage the capabilities of ArrayList to check if a String is in the list, you can do this:
File regionFolder = new File("/path/to/files");
FileFilter filter = new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.getPath().endsWith(".yml");
}
};
ArrayList<String> myYmlFiles = new ArrayList<String>(Arrays.asList(regionFolder.list(filter)));
You can then use .contains on the myYmlFiles object, and it will only contain your .yml files.
if ( myYmlFiles.contains(e.getRegion().getId()) ) {
// do something
}

How to write a method that returns List<classname>? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So, in a test that i took few days ago we were supposed to make a method that returns a list, that contained all info about a class we had made before that.
The code looked like this:
public List<File> getall{
}
whereas File is the class that contains some variables and a toString() method.
So, what is a good way to write a method that returns this data type?
I may be missing the point here, but it's really quite simple:
public List<File> getall() {
// create a List object to return
List<File> returnValues = new ArrayList<>();
// add File objects to returnValues
....
// and return it
return returnValues;
}
The nature of the class contained in the List is of no importance - however in this particular case you should be aware that there's already a File class in the standard java.io package, so File may not be the best name to give to a custom class...
As you need to return a List<File> ,it depends on how you have implemented the File class.
public List<File> getAll() {
// create a List object to return
List<File> listobj= new ArrayList<File>();
//Do your stuff here you want to do and return that object
return listobj;
}
Hope it will help you.
Try :
public List<File> getAll() {
// create a List object
List<File> files = new ArrayList<>();
files.add(new file); // Keep doing this
return files;
}

Categories