Reading a file and creating objects from the information [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 8 years ago.
Improve this question
I have two files and I want to individually read them then use the the first file to create 5 objects and then use the second file to add the parameters to be passed to a constructor, question is is I am not quite sure how to do that.
What I wanted to do was to loop the hasNextLine and assign the next line to a string and have an object created from that string name, then passing variables in the same way but I see that may not be possible in Java. If it isn't what is another way I could approach this?
Well I was trying to do something like this
while(salesPersonScanner.hasNextLine()){
String personName = salesPersonScanner.nextLine();
SalesPerson personName = new SalesPerson();
}

You can use a BufferedReader to iterate over lines:
final BufferedReader reader = new BufferedReader(new FileReader("/path/to/file"));
String line;
while ((line = reader.readLine()) != null) {
// Create your object from the string
}

You have two options:
You can read all of the required parameters from files and then pass them all to the constructor to create a new object.
You can create an object with your constructor and use setters to set each of the instance variables.
I personally suggest the first approach unless you have to set lots of variables.

Related

best / better way to read / write arrays to firebase [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 2 years ago.
Improve this question
I was trying to find how to write/read arrays from my Firebase and I ended up with this. When I write I do:
String RestaurantIDs = "";
for(String RestaurantID : category.getRestaurantIDs()) { RestaurantIDs += RestaurantID + ","; }
DataToSave.put(CategoryFireBase.RestaurantIDs, "[" + RestaurantIDs + "]");
And when I read I do:
ArrayList<String> restaurantIDs = new ArrayList<>();
String[] Temp_restaurantIDs = document.get(CategoryFireBase.RestaurantIDs).toString().replace("[","").replace("]","").split(",");
for (String value : Temp_restaurantIDs){restaurantIDs.add(value);}
And I have 2 questions about it.
Is it ok that I'm saving it as long string instead of arrays?
If there is a way to read and write the array as an actual array, is it better then this?
Is it ok that I'm saving it as long string instead of arrays?
There is nothing wrong with that but it's more convenient to use arrays. For example, if you have in the database an array of restaurant ids, when you make a get() call, you can get that arrays directly as a list, without the need to split that long String.
If there is a way to read and write the array as an actual array, is it better then this?
Is it better because you can use specialized methods to update or remove elements within an array, as explained in the official documentation:
https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array

Is it possible to find a word in a txt file and print the line as a string [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 3 years ago.
Improve this question
I am stuck on some homework as i am new to java and still learning. I am wondering if it is possible to find a word in a .txt file and output the line that the word is on. I also need to allow a user to make a choice based on what is displayed back.
Example :
Word is Details
Txt file contains
Details on lion
Details on tiger
Output : "Details on tiger"
Thank in advanced for any help
This question has been answered before, but anyway You can go with this apporach.
Simply put:
Create a Scanner object and pass the required file into the
constructor as a new file object.
Iterate over the file with a
while loop until you find the specified string.
In order to store the lines that contains the desired word,we decalre a new string variable
Here's the code snippet:
Scanner scanner= new Scanner(new File("filename.txt"));
String lines = "";
while(scanner.hasNextLine()){
String stringLine = scanner.nextLine();
if(stringLine.indexOf("YOUR_WORD") != -1){
//print whatever you want here
System.out.println(stringLine);
//add every line that contains stringLine into another string;
lines+=stringLine;
}
}

how to split a text file by line gaps 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 5 years ago.
Improve this question
I am reading a text file in Java that looks like this,
"
Q1. You are given a train data set having 1000 columns and 1 million rows. The data set is based on a classification problem. Your manager has asked you to reduce the dimension of this data so that model computation time can be reduced. Your machine has memory constraints. What would you do? (You are free to make practical assumptions.)
Q2. Is rotation necessary in PCA? If yes, Why? What will happen if you don’t rotate the components?
Q3. You are given a data set. The data set has missing values which spread along 1 standard deviation from the median. What percentage of data would remain unaffected? Why? "
Now, I want to read this file and then store each of these sentences(questions) in a string array. How can I do that in java?
I tried this,
String mlq = new String(Files.readAllBytes(Paths.get("MLques.txt")));
String[] mlq1=mlq.split("\n\n");
But this is not working.
Try this
String mlq = new String(Files.readAllBytes(Paths.get("MLQ.txt")));
String[] mlq1=mlq.split("\r\n\r\n");
System.out.println(mlq1.length);
System.out.println(Arrays.toString(mlq1));
This should do it by line gap of 2 lines.
File file = new File("C:\\MLques.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null) {
System.out.println(st + "\n");
}
I think it will work.
This is a piece of code from one of my project.
public static List<String> readStreamByLines(InputStream in) throws IOException {
return IOUtils.readLines(in, StandardCharsets.UTF_8).stream()
.map(String::trim)
.collect(Collectors.toList());
}
But!!! If you have really big file, then collecting all content into a List is not good. You have to read InputStream line by line and do all you need for every single row.

convert string array to object array [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
I was asked to use an array to create a phonebook which would read in a text file.
However I am having trouble each line on the text file into my object array.
so my solution was to create an array of type String then make my object array = to string array... but thats the problem.
Is there any way of my directly using the Scanner class to read in my text and save it into an object array as Scanner class's next() method is type string... and my array is type object... ?
thanks.
Try:
List<String> lines = new ArrayList<>();
while(scanner.hasNextLine()) {
lines.add(sc.nextLine());
}
String[] array = lines.toArray(new String[lines.size()]);
ArrayList is the key All you need to do is convert your Strings to arrayLists and then you can add or pop the elements more easily

How to read previous line by using bufferedReader [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
I am making a some large project. In that I am stuck at one place. I am using the following code to read from the file:
String str = null;
FileReader fr = new FileReader("H:\\Eclipse\\Emulator\\progin8085.txt");
BufferedReader br = new BufferedReader(fr);
str = br.readLine(); //using it in a loop from 0 to total_no_of_lines
Now I when I reached suppose at line 8(counting lines numbers while entering data to the file), I want to go back to line 3 or 4 or any and again want to read and execute each statement. How to read previous statements using BufferedReader only? If it is not possible, any other solution?
Some Readers support marks. You can use those to rewind the file. A particularly useful Reader (imo) is the LineNumberReader and it supports marks. This sort of code might suit your needs.
public static int final READ_AHEAD_LIMIT = 100000;
LineNumberReader lnReader = new LineNumberReader(reader);
while (youWantToRead) {
...
if (mightBeInterestingLater) {
lnReader.mark(READ_AHEAD_LIMIT);
}
...
if (nowWantToRewind) {
lnReader.reset();
// We're now at whatever place mark() was last called at.
}
Save all the lines of text file in String[] StrArray and do whatever you want.

Categories