I am currently writing a java program that takes data from a text file and adds to to an array list. My problem is, that every time I run the program, the arraylist keeps updating and has duplicate elements - I would like each element to only appear once.
`
BufferedReader br = new BufferedReader(new FileReader("s.txt"));
for (String line = br.readLine(); line != null; line = br.readLine()) {
if(!listID.contains(Integer.parseInt(line))){
listID.add(Integer.parseInt(line));
}
for(int i=0;i<listID.size();i++){
// do stuff
}
`
I have tried to use does not contain but it isnt working.
Does this work for you?
Set<Integer> set = new HashSet();
BufferedReader br = new BufferedReader(new FileReader("s.txt"));
for (String line = br.readLine(); line != null; line = br.readLine()) {
set.add(Integer.parseInt(line));
for(int i=0;i<listID.size();i++){
// do stuff
}
}
Related
I am trying to read multiple lines from a file into an ArrayList as a String.
What I aim to do is to make it so the program reads from a file line by line until the reader sees a specific symbol (- in this case) and saves those rows as one single String. the code below makes every row a new string that it later adds to the list instead.
BufferedReader br = null;
br = new BufferedReader(new FileReader(file));
String read;
while ((read = br.readLine()) != null) {
String[] splited = read.split("-");
carList.add(Arrays.toString(splited));
}
for (String carList2 : carList) {
System.out.println(carList2);
System.out.println("x");
}
First, you need to check if the read line contains "-".
If it doesn't, concatenate the line with the previous ones.
If it does, concatenate only the first part of the line with the previous line.
This is a quick implementation:
BufferedReader br = null;
br = new BufferedReader(new FileReader(file));
String read;
String concatenatedLine = "";
while ((read = br.readLine()) != null) {
String[] splited = read.split("-");
// if line doesn't contains "-", splited[0] and read are equals
concatenatedLine += splited[0];
if (splited.length > 1) {
// if read line contains "-", there will be more than 1 element
carList.add(Arrays.toString(splited)); // add to the list
// store the second part of the line, in order to add it to the next ones
concatenatedLine = splited[1];
}
}
Note the output could not be what is expected if a line contains more than one -.
Also, concatenating String using + is not the best way to do it, but I let you find out more about that.
It's not very clear for me what is the output you desire.
If you would like to have each customer on one string without "-"
then you could try the following code:
while ((read = br.readLine()) != null) {
String splited = read.replace("-", " ");
carList.add(splited);
}
I have an assignment where I need to read a file's contents in reverse, like this:
Original:
This is how you reverse a file, 10
New:
10 ,file a reverse you how is This
Here's the code I have:
public static void main(String [args]{
Path file = Paths.get("C:\\Java\\Sample.txt");
InputStream input = null;
ArrayList<String> words = new ArrayList<String>();
try{
input = Files.newInputStream(files);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String s;
while((s = reader.readLine()) != null)
words.add(s);
for(int i = words.size() - 1; i >= 0; i--){
System.out.print(words.get(i));
}
catch(Exception e){
System.out.println(e);
}
}
Sorry if the formatting is off. The program simply reads the file in original form. What am I doing wrong? Can anyone explain what I need to have this print backwards? My textbook doesn't explain anything.
Also, I realize that my catch block is possibly too broad. I'll work on that.
EDIT: I forgot to add the ArrayList code when typing this out. It exists already in my original program.
Thank you
while((s = reader.readLine()) != null)
words.add(s);
The second line implies that s holds words, but the first line reads a line at a time. You need to read a word at a time, or split the lines into words.
I'm working on this item. I did the spell checking algorithm but I have no idea how to read data correctly. When I use this:
StringBuilder text = new StringBuilder();
Scanner sc = new Scanner(System.in);
String temp;
while ((temp = sc.nextLine()).length() > 0){
text.append(temp);
}
/*spell checking algorithm*/
It waits for the empty string.
In this case:
while (sc.hasNext()){
text.append(temp);
}
it doesn't continue to execute the code at all. If I try to read 10000 signs I should type it all.
How could I read data correctly for this task?
Read them from file:
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
//do what you want
}
My txt file looks like this:
data;data2;data3;data4..........up till data3146
When I open the txt file in notepad I see it in the form given above.
But when I copy paste the first few lines to another place, There is a 1 line gap b/w data1 and everything else. Because of this I am getting problems while accessing the file in Java and using the data with a bufferedreader in a loop. How can I correct this? I can't remove the empty line as it is not even visible in the original file.
You can ignore the blank line(s). Something like this -
while ((line = reader.readLine()) != null) {
if(line.trim().isEmpty()) {
continue;
}
...
you can try this way:
BufferedReader reader = new BufferedReader(new FileReader(new File("your file path")));
String str = null;
while((str = reader.readLine())!=null) {
if (str.matches("[' ']+")) {
continue;
} else {
// to do
}
}
I believe that the problem is in the line endings. Basically you can skip the empty lines:
String line;
while ((line = reader.readLine()) != null) {
if ("".equals(line.trim()) {
continue;
}
// do your stuff here
}
I have a file that looks similar to this
12345 one
12345 two
12345 three
.......
Question is how can i get all of the values from second row and store them in a String[]
i know how to read file in java just dont know how to cut the second row
1. Store Each line from the file into an ArrayList<String>, its more flexible than String[] array.
2. Then access the line you need by get() method of ArrayList
Eg:
ArraList<String> arr = new ArrayList<String>();
//Now add each lines into this arr ArrayList
arr.get(1); // Getting the Second Line from the file
`
You can split the file line by new line.
String [] names = fileString.split("\n");
Ok this is what i did but it skips first line
FileInputStream fstream = new FileInputStream("/sys..........");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
strLine = br.readLine();
while ((strLine = br.readLine()) != null) {
delims = strLine.split(" ");
first = delims[1];
where.add(first);
}
in.close();
From example above it contains only "two" and "three"