So I have a project in which I have to read book reference numbers and book titles from a .txt file into an array, and then a user is to enter a reference number that will do a search for the book with that reference number, so here is what i have, Keep in mind I'm not very experienced with java
public class Book {
ArrayList<String> books = new ArrayList<String>();
BufferedReader br = null;
{
try {
br = new BufferedReader(new FileReader("BookList.txt"));
String book;
while ((book = br.readLine()) != null) {
books.add(book);
}
} catch (IOException e){
} finally {
try {
br.close();
} catch (IOException ex) {
}
}
String [] bookList = new String[books.size()];
books.toArray(bookList);
}
}
That is to read the file into an array list, and then convert the array list into an array
Im not 100% sure if that's right so if theres a problem, I would gladly take your solution.
The problem i'm having is when i try to set up a method that allows a user to search
private void FindItActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input = Input.getText();
for(int i=0; i<bookList.length; i++){
}
}
I get an error that says cannot find symbol bookList, but im not sure what why
Thanks for any help or advice you may be able to offer
your init code is inside a scoped brackets, you missed a method declaration by the way.
you cant reach bookList as its not a class parameter but declared in the scope.
put a method declaration (above the try) and add bookList declaration under your BufferedReader variable instead of declaring it in the scope.
You need to have bookList available to all of your methods
public class Book {
ArrayList<String> books = new ArrayList<String>();
String[] bookList;
BufferedReader br = null;
// ...
Then you need to set it to something. Your current line books.toArray(bookList); uses bookList as the argument for toArray to know what kind of array it is producing, then it will return an array of that type. So you need to do
this.bookList = books.toArray(bookList);
Your code should look like this:
public class Book {
ArrayList<String> books = new ArrayList<String>();
BufferedReader br = null;
String[] bookList; //difference (bookList is now visible to all methods in class)
{
try {
br = new BufferedReader(new FileReader("BookList.txt"));
String book;
while ((book = br.readLine()) != null) {
books.add(book);
}
} catch (IOException e){
} finally {
try {
br.close();
} catch (IOException ex) {
}
}
bookList = new String[books.size()]; //difference
books.toArray(bookList);
}
}
private void FindItActionPerformed(java.awt.event.ActionEvent evt){
String input;
input = Input.getText();
for(int i=0; i<bookList.length; i++){
//do something...
}
}
Problem in your code is that you have tried to use variable bookList which was in different scope defined.
Related
So I have a text file that looks like this...
4234
Bob
6858
Joe
I am trying to read the file with java and insert the data into an array. I want to separate the data by that empty line (space). Here is the code that I have come up with to solve the issue, but I am not quite there.
public class Main {
public static void main(String[] args) {
// This name is used when saving the file
BufferedReader input;
String inputLine;
try {
input = new BufferedReader(new FileReader("test.txt"));
while ((inputLine = input.readLine()) != null) {
System.out.println(Arrays.toString(inputLine.split(" ")));
}
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
}
The issue that I am coming across is that the output from the code above looks something like this
[4234]
[Bob]
[]
[6858]
[Joe]
The outcome that I would like to achieve, and for the life of me can't think of how to accomplish, is
[4234, Bob]
[6858, Joe]
I feel like with many things that it is a relatively simple code change; I am just not sure what that is.
You need:
2D array
Logic to keep track of where you are in the array position
If your Line is a Number/String
This sounds like hw :) so I wont be solving it, I will just help a bit.
String[][] myData = define your 2D array;
//You need to create a consumer. This is what will take the String line, figure out where to put it into your 2D array.
Consumer<String> processLine = (line) -> {
if(StringUtils.isNumeric(line)){
//Put into array[counter][1]
}
else{
//its a String
//Put into array[counter][0]
}
};
The below try/catch, Opens a File, Reads its Lines, and goes over each one in order (forEachOrdered), ignoring all empty lines, and send it to your processLine consumer.
try (Stream<String> lines = Files.lines(Paths.get("C:/example.txt"), Charset.defaultCharset())) {
lines.filter(line -> !line.isEmpty()).forEachOrdered(processLine);
}
catch (Exception e){
//Handle Exception
}
Used Apache StringUtils http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
IF you dont want to use any external Libs. You can probably do
Integer.parseInt(line) <-- If that throws an exception, its not a number
Your way of reading the file is not most convenient, in this case.. Scanner would have eased all this work; however, if you insist, that you want to use BufferedReader and FileReader, it's going to be a bit verbose, boilerplate and even ugly code, something like this:
public class Main {
public static void main(String[] args) {
// This name is used when saving the file
BufferedReader input;
String inputLine;
String answer = "";
try {
input = new BufferedReader(new FileReader("path\\to\\your\\test.txt"));
while ((inputLine = input.readLine()) != null) {
answer = answer + "[" + inputLine + ", ";
while ((inputLine = input.readLine()) != null && !inputLine.equals("")) {
answer += inputLine;
}
answer += "]";
System.out.println(answer);
answer = "";
}
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
}
This code, with test.txt containing:
4234
Bob
6858
Joe
4234
John
5352
Martin
will output:
[4234, Bob]
[6858, Joe]
[4234, John]
[5352, Martin]
I don't know if it's an actual requirement for you to use arrays of strings, but the better way in the long run is to create a class.
class Person {
public String id;
public String name;
public String toString() { return String.format("[%s, %s]", id, name); }
}
(note: It's a bad idea to actually make the fields public, but this makes the code shorter. You should probably use getters and setters).
Now you can create Persons while reading the file.
List<Person> allInFile = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("path\\to\\your\\test.txt"))) {
String line = reader.readLine();
while (line != null) {
line = line.trim();
// ignore empty lines
if (line.length() == 0) {
continue;
}
// this is an id; create a person and assign id
Person person = new Person();
person.id = line;
// read consecutive field, which is the name
person.name = reader.readLine();
// add the person to the list
allInFile.add(person);
}
}
allInFile.forEach(System.out::println);
Lots of improvements to be done on this, but the main point is to put the two data points into a class.
Try with this code:
it work only when file contains number followed by name otherwise pair would be different format
pair : [number, string]
public static void main(String[] args) {
BufferedReader input;
String inputLine;
List<String> pair = new ArrayList<String>();
List<String> list = new ArrayList<String>();
try {
input = new BufferedReader(new FileReader("Test.txt"));
while ((inputLine = input.readLine()) != null) {
if (!inputLine.isEmpty()) {
pair.add(inputLine);
}
if (pair.size() == 2) {
list.add(pair.toString());
pair.clear();
}
}
for (String s : list) {
System.out.println(s);
}
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
After looking at the answers posted by my fellow Stack Overflow members I figured out that there was a very simple way of solving this issue and that was by using Scanner rather than using BufferedReader. I am not sure why I didn't think of this before, but hindsight is 2020. Anyway, the code below is what I used to solve my issue.
public static void main(String[] args) {
ArrayList<String> test = new ArrayList<>();
File file = new File("test.txt");
try {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
test.add(sc.next()); // The id
test.add(sc.next()); // The name
}
sc.close();
System.out.println(test.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
All this is doing is getting each line with the different data on it and is skipping the blank. From there it is adding it to an ArrayList for later processing. Remember K.I.S.S (Keep It Simple Stupid) no need to overcomplicate anything.
I am trying to complete a little program.
I've got a text file (.txt) to store different data on objects that i've got.
The structure of the file is the next (exemples data.txt) :
Sedane
2005
195000
Diesel
Blue
SUV
2013
34000
Fuel
Black
Each object is made true a class that i've build called Cars.
So the 1 line is the type of car, the 2nd the year of built, the 3rd line is the milage, the 4th is the type of fuel, and the 5th line is the color of the car.
So basicly i need to open the file, and load the data into the memory when i execute my program into an array with object in it.
I'm ok to open the file but i'm blocked when it comes to reading the data and putting it in an array.
The array size is 2 for this exemple, but if i have more entries in the file it's going to adapt it's size when loading at the startup of the program.
Here's what i've got unti now (for my code ...)
public static void loadCars () {
FileReader fopen;
BufferedReader opened;
String line;
try {
fEntree = new FileReader( "data.txt" );
opened = new BufferedReader( fopen );
while ( opened.ready() ) {
line = opened.readLine();
// Don't know what to do here ????
}
opened.close();
} catch ( IOException e ) {
System.out.println( "File doesn't exist !" );
}
}
Someting like this will do the trick. I'm adding the file contents line by line to an Arraylist instead of an array though. This way you don't have to know how big your array needs to be before hand. Plus you can always change it to an array later.
public ArrayList<String> readFileToMemory(String filepath)
{
in = new BufferedReader(new FileReader( "data.txt" ));
String currentLine = null;
ArrayList<String> fileContents = new ArrayList<String>();
try
{
while((currentLine = in.readLine()) != null)
{
fileContents.add(currentLine);
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
return fileContents;
}
LineNumberReader lnr = new LineNumberReader(new FileReader(new File("File1")));
lnr.skip(Long.MAX_VALUE);
long length = lnr.getLineNumber();
lnr.close();
in = new BufferedReader(new FileReader( "data.txt" ));
Car[] cars= new Car[length/5];
String currentLine;
int i=0;
for(int i=0;i<length/5;i+=5) {
String name = in.readLine();
String year = in.readLine();
String miles = in.readLine();
String gas = in.readLine();
String color = in.readLine();
cars[i] = new Car(name,year,miles,gas,color);
}
You'll have to handle exceptions too, surround stuff in try catch structures.
You can look at my solution here below (I also corrected/simplified some problems with the variables for reading the file, anyway this was not the main topic):
public static void loadCars() {
FileReader fopen;
BufferedReader opened;
String line;
ArrayList<Car> carList = new ArrayList<Car>();
try {
fopen = new FileReader("data.txt");
opened = new BufferedReader(fopen);
int nFields = 5; // we have 5 fields in the Car class
String[] fields = new String[nFields]; // to temporary store fields values read line by line
int lineCounter = 0;
while ((line = opened.readLine()) != null) {
fields[lineCounter] = line;
lineCounter++;
if ((lineCounter) % nFields == 0) { //it means we have all 5 fields values for a car
carList.add(new Car(fields)); //therefore we create a new car and we add it to the list of cars
}
}
opened.close();
} catch (IOException e) {
System.out.println("File doesn't exist !");
}
}
Basically we use an ArrayList to store all the cars, and we read the file, waiting to have all the fields values in order to create the Car object. I store the fields values in an array of Strings: I don't know how you implemented the Car class, but maybe it is useful to create a constructor that takes as parameter an array of strings, so it can set the fields, for instance:
class Car {
private String type;
private String year;
private String milage;
private String fuel;
private String color;
public Car(String[] fields) {
type=fields[0];
year=fields[0];
milage=fields[0];
fuel=fields[0];
type=fields[0];
}
}
But I've to say that probably this is a little 'too static'.
For simplicity I assumed that all your fields are of String type, but probably fields like 'year' or 'milage' might be of int type. In this case you can use array of Object[] (instead of String[]), and then cast the value with the right type.
I hope this may help you.
Hello is there any way to convert vector into arraylist ? I want to make search options for my table but looks like it would be much easier with arraylist for me. I used search but surprising there was nothing about vectors.
public Vector<Vector<Object>> InfoForTheTable() {
Scanner s = null;
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
try {
s = new Scanner(new File("info.txt"));
while (s.hasNextLine()) {
String line = s.nextLine();
if (line.startsWith("")) {
String[] atoms = line.split("[#]");
Vector<Object> row = new Vector<Object>();
row.add(atoms[0]);
row.add(atoms[1]);
row.add(atoms[2]);
row.add(atoms[3]);
row.add(atoms[4]);
row.add(atoms[5]);
data.add(row);
}
}
}
catch(IOException e) {
e.printStackTrace();
}
finally {
if (s != null) {
s.close();
}
}
return data;
}
Instead of
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
I recommend to use:
List<List<Object>> data = new ArrayList<>();
Or even better: create a class MyInfo and assign the atoms values to its properties
Then use:
List<MyInfo> data = new ArrayList<>();
So here is a more modern version of your code:
public List<Info> readInfoFromFile() {
List<Info> infoList= new ArrayList<>();
try (Scanner s = new Scanner(Paths.get("info.txt")){
while (s.hasNextLine()) {
String line = s.nextLine();
if (line.startsWith("")) {
String[] atoms = line.split("[#]");
Info info = new Info();
info.setA(atoms[0]);
info.setB(atoms[1]);
info.setC(atoms[2]);
info.setD(atoms[3]);
info.setE(atoms[4]);
info.setG(atoms[5]);
infoList.add(rowinfo);
}
} catch(IOException e) {
e.printStackTrace();
}
return data;
}
Replace the properties and their types as needed.
I'd recommend you to don't use that type of collections, first of all the main difference between ArrayList and Vector is that all vector operations are synchhronized. Second, you have a Vector< Vector<Object> > and you want this to be ArrayList< ArrayList<Object> > So in my opinion you should create your own class (bean class).
Example with at least Java 1.7:
public class myTableModel{
private String somePropertyName1;
.
.
.
private String somePropertyNameN;
public MyTableModel(String ... array){
//assign values to instance attributes.
}
//getters and setters
}
//remember method names in java starts with lower-case
public List<MyTableModel> infoForTheTable() {
List<MyTableModel> data = new ArrayList<>(); //diamond inference
//use try-with-resources
try (Scanner s = new Scanner(Paths.getPath("info.txt"))){
while (s.hasNextLine()) {
String line = s.nextLine();
if (line.startsWith("")) {
String[] atoms = line.split("[#]");
data.add(new MyTableModel(atoms[0],atoms[1],atoms[2],atoms[3],atoms[4],atoms[5]));
}
}
}
catch(IOException e) {
//handle exception or throw it up!
}
return data;
}
Yes, ArrayList has a constructor that takes a Collection (which Vector obviously is) to do so:
ArrayList(Collection<? extends E> c)
just use the constructor which takes a collection as its parameter:
ArrayList<String> list = new ArrayList<String>(row);
Note that it only does a shallow copy.
for more details see this
I'm trying to read data from a .txt file. The format looks like this:
ABC, John, 123
DEF, Mark, 456
GHI, Mary, 789
I am trying to get rid of the commas and put the data into an array or structure (structure most likely).
This is the code I used to to extract each item:
package prerequisiteChecker;
import java.util.*;
import java.io.*;
public class TestUnit {
public static void main(String[]args){
try {
FileInputStream fstream = new FileInputStream("courses.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] splitOut = strLine.split(", ");
for (String token : splitOut)
System.out.println(token);
}
in.close();
} catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
}
At one point I had a print line in the "while" loop to see if the items would be split. They were. Now I'm just at a loss on what to do next. I'm trying to place each grouping into one structure. For example: ID - ABC. First Name - John. Room - 123.
I have a few books on Java at home and tried looking around the web. There is so much out there, and none of it seemed to lead me in the right direction.
Thanks.
Michael
create a class that looks something like this:
class structure {
public String data1;
public String data2;
public String data3;
}
This will form your basic data structure that you can use to hold the kind of data you have mentioned in your question. Now, you might want to follow proper object oriented methods like declaring all your fields as private, and writting getters and setters. you can find more on there here ... http://java.dzone.com/articles/getter-setter-use-or-not-use-0
Now, just outside your while loop, create an ArrayList like this: ArrayList<structure> list = new ArrayList<structure>(); This will be used to hold all the different rows of data that you will parse.
Now, in your while loop do something like this:
structure item = new structure();//create a new instance for each row in the text file.
item.data1 = splitOut[0];
item.data2 = splitOut[1];
item.data3 = splitOut[2];
list.add(item);
this will basically take the data that you parse in each row, put in the data structure that you declared by creating a new instance of it for each new row that is parsed. this finally followed by inserting that data item in the ArrayList using the list.add(item) in the code as shown above.
I would create a nice structure to store your information. I'm not sure if how you want to access the data, but here's a nice example. I'll go off of what you previously put. Please note that I only made the variables public because they're final. They cannot change once you make the Course. If you want the course mutable, create getters and setters and change the instance variables to private. After, you can use the list to retrieve any course you'd like.
package prerequisiteChecker;
import java.util.*;
import java.io.*;
public class TestUnit {
public static void main(String[] args) {
try {
FileInputStream fstream = new FileInputStream("courses.txt");
// use DataInputStream to read binary NOT text
// DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
List<Course> courses = new LinkedList<Course>();
while ((strLine = br.readLine()) != null) {
String[] splitOut = strLine.split(", ");
if (splitOut.length == 3) {
courses.add(new Course(splitOut[0], splitOut[1],
splitOut[2]));
} else {
System.out.println("Invalid class: " + strLine);
}
}
in.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
public static class Course {
public final String _id;
public final String _name;
public final String _room;
public Course(String id, String name, String room) {
_id = id;
_name = name;
_room = room;
}
}
}
public class File_ReaderWriter {
private static class Structure{
public String data;
}
public static void main(String[] args) throws IOException{
String allDataString;
FileInputStream fileReader = new FileInputStream ("read_data_file.txt");
DataInputStream in = new DataInputStream(fileReader);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));
String[] arrayString = {"ID - ", " NAME - ", " ROOM - "};
int recordNumber = 0;
Structure[] structure = new Structure[10];
for (int i = 0; i < 10; i++)
structure[i] = new Structure();
while((allDataString = bufferReader.readLine()) != null){
String[] splitOut = allDataString.split(", ");
structure[recordNumber].data = "";
for (int i = 0; i < arrayString.length; i++){
structure[recordNumber].data += arrayString[i] + splitOut[i];
}
recordNumber++;
}
bufferReader.close();
for (int i = 0; i < recordNumber; i++){
System.out.println(structure[i].data);
}
}
}
I modify your given code. It works. Try it and if any query then ask.
We have
public class UKWacSentenceIterator implements SentenceIterator
which is obviously an Tterator but I don't have any information on what's in SentenceIterator. This class has this property: Scanner fileScanner.
The idea is that the constructor takes an array of files:
public UKWacSentenceIterator() throws IOException {
Properties p = new Properties();
p.load(prop.class.getClassLoader()
.getResourceAsStream("sources/ukwacdump.properties"));
Enumeration<Object> keys = p.elements();
while (keys.hasMoreElements()) {
source.add(keys.nextElement());
}
fileScanner = new Scanner(new File((String) source.get(0)));
}
And in the main method we can use a for loop:
public static void main(String[] args) throws IOException {
for(String line : new UKWacSentenceIterator()) {
System.out.println(line);
}
}
He has currently having a problem with this for loop because once the first file is EOF the for just stops. So he thought would be a good idea to override
#Override
public boolean hasNext() {
if(tmp != null) {
return true;
}
if (this.fileScanner.hasNext()) {
try {
this.skipToSequenceStart();
String sent = this.scanSentence();
this.tmp = sent;
return true;
} catch (Exception e) {
return false;
}
} else {
return advanceFileScanner();
}
}
But he doesn't know how to build advanceFileScanner().
My idea is to just to assign the variable fileScanner to a new Scanner with the next file name and then just copy
this.skipToSequenceStart();
String sent = this.scanSentence();
this.tmp = sent;
return true;
I don't know if he tried yet. I was wondering if you think is a good idea and if you can suggest me a good tutorial on how to create an iterable object. Because right now I'm just guessing, I don't know what the for loop use other than hasNext().
I am not sure but isn't your problem simply that your
fileScanner = new Scanner(new File((String) source.get(0)));
only contains 1 file
I explain. I use to read in many file given a string array of all the files I have to read. Me, I do it that way, I simply declare as an []. I give you an exemple of my code.
BufferedReader[] reader = new BufferedReader[myArrayFiles.length];
for (int i = 0; i < myArrayFiles.length; i++) {
reader[i] = new BufferedReader(new FileReader(myArrayFile[i]));
//do my reading
reader.close();
}
It is with buffered reader but I think you could apply it to your code. Could you do something like that (is source an array ? i assume yes so i use length. Perhaps it's "size()" in your case).
Scanner[] fileScanner = new Scanner[source.length()];
for (i = 0; i < source.length(); i++) {
fileScanner[i] = new Scanner(new File((String) source.get(i)));
}
Then of course you have to refactor the rest of the code to handel the filescanner array
Hope it helps