Reading text into an ArrayList - java

I have the following data stored in a .txt file:
one,1
two,2
three,3
......
I want to store the information in an array with the following structure:
[one,1,two,2....]
Here is my code so far:
public Shortener( String inAbbreviationsFilePath ) throws FileNotFoundException {
Scanner s = new Scanner(new File(inAbbreviationsFilePath));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
list.add(s.next());
}
abbreviations = list.toArray(new String[list.size()]);
s.close();
}
My problem is that I cant get the array to be stored so that one and 1 are in different positions. i.e at the moment the array is structured like this [one1,two2,...].
Thanks for help in advance

You have to split each line by the coma and add two parts of it to your result list:
public Shortener( String inAbbreviationsFilePath ) throws FileNotFoundException {
Scanner s = new Scanner(new File(inAbbreviationsFilePath));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()) {
//HERE
String line = s.next();
String[] lineSplit = line.split(","); //split into two tokens
list.add(lineSplit[0]); //word
list.add(lineSplit[1]); //number
}
abbreviations = list.toArray(new String[list.size()]);
s.close();
}

Use this instead of your while loop,
String str;
String []st;
while ((str=s.nextLine())!=null){
st=str.split(",");
list.add(st[0]);
list.add(st[1]);
}

try this
Scanner s = new Scanner(new File...
s.useDelimiter("\\s+|,");

Related

How to split an ArrayList of sentences into an ArrayList of words in Java without reading a text file more than once?

I need to read a text file only once and store the sentences into an ArrayList. Then, I need to split the ArrayList of sentences into another ArrayList of each individual word. Not sure how to go about doing this?
In my code, I've split all the words into an ArrayList, but I think it's reading from the file again, which I can't do.
My code so far:
public class Main {
public static void main(String[] args){
try{
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
ArrayList<String> sentences = new ArrayList<String>();
ArrayList<String> words = new ArrayList<String>();
String line;
while((line=br.readLine()) != null){
String[] lines = line.toLowerCase().split("\\n|[.?!]\\s*");
for (String split_sentences : lines){
sentences.add(split_sentences);
}
/*Not sure if the code below reads the file again. If it
does, then it is useless.*/
String[] each_word = line.toLowerCase().split("\\n|[.?!]\\s*|\\s");
for(String split_words : each_word){
words.add(split_words);
}
}
fr.close();
br.close();
String[] sentenceArray = sentences.toArray(new String[sentences.size()]);
String[] wordArray = words.toArray(new String[words.size()]);
}
catch(IOException e) {
e.printStackTrace();
}
}
}
/*Not sure if the code below reads the file again. If it does, then it is useless.*/
It doesn't. You are simply reparsing the line that you have already read.
You have already solved your problem.

Read file by word, Scanner

I have txt file, which each row contains two words, for example:
USA 321
France 1009
...
Germany 902
How can I read this file by word in two-dimensional array? I have:
List<List<String>> temps = new ArrayList<>();
Scanner dataScanner = new Scanner(dataFile);
while (dataScanner.hasNextLine()) {
Scanner rowScanner = new Scanner(dataScanner.nextLine());
temps.add(new ArrayList<>(2));
while (rowScanner.hasNextLine()) {
...
}
}
I would do it like this assuming your code works
List<List<String>> temps = new ArrayList<>();
Scanner dataScanner = new Scanner(dataFile);
while (dataScanner.hasNextLine()) {
String[] data = dataScanner.nextLine().split(" ");
temps.add(new ArrayList<>(Arrays.asList(data[0],data[1]));
}
This takes the current line and splits it at a space character.
Afterwards it creates a list with the two elements and adds it to your temps list
If you want absolutely use Scanner :
List<List<String>> temps = new ArrayList<>();
Scanner dataScanner = new Scanner("a b\nc d\ne f\n");
while (dataScanner.hasNextLine()) {
Scanner rowScanner = new Scanner(dataScanner.nextLine());
List<String> datas=new ArrayList<>(2);
temps.add(datas);
while (rowScanner.hasNext("[^\\s]+")) {
datas.add(rowScanner.next("[^\\s]+"));
}
}
My advice is to ALWAYS separate different functionalities in different functions. The code becomes easier to read, easier to mantain and reusable:
public static List<String> readFileLineByLine(String file) {
List<String> lines = new ArrayList<>();
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
temps.add(scanner.nextLine());
}
return lines;
}
public static List<MyData> parseLines(List<String> lines) {
List<MyData> list = new ArrayList<>();
for (String line : lines) {
String[] data = line.split(" ");
list.add(new MyData(data[0], data[1]));
}
return list;
}
(Use List<String> as MyData if you need to)
I'm a big fan of Scanner, but in this case you can get by reading line-by-line and using String.split. This becomes quite simple using streams. If you want to read into a two-dimensional array, you can do this:
try (Stream<String> lines = Files.lines(Paths.get(FILENAME), UTF_8)) {
String[][] result = lines.map(s -> s.split("\\s+"))
.toArray(String[][]::new);
}
Or if you want nested Lists, you can do this:
try (Stream<String> lines = Files.lines(Paths.get(FILENAME), UTF_8)) {
List<List<String>> result = lines.map(s -> s.split("\\s+"))
.map(Arrays::asList)
.collect(toList());
System.out.println(result);
}

Scanning, spliting and assigning values from a text file

I'm having trouble scanning a given file for certain words and assigning them to variables, so far I've chosen to use Scanner over BufferedReader because It's more familiar. I'm given a text file and this particular part I'm trying to read the first two words of each line (potentially unlimited lines) and maybe add them to an array of sorts. This is what I have:
File file = new File("example.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] ary = line.split(",");
I know It' a fair distance off, however I'm new to coding and cannot get past this wall...
An example input would be...
ExampleA ExampleAA, <other items seperated by ",">
ExampleB ExampleBB, <other items spereated by ",">
...
and the proposed output
VariableA = ExampleA ExampleAA
VariableB = ExampleB ExampleBB
...
You can try something like this
File file = new File("D:\\test.txt");
Scanner sc = new Scanner(file);
List<String> list =new ArrayList<>();
int i=0;
while (sc.hasNextLine()) {
list.add(sc.nextLine().split(",",2)[0]);
i++;
}
char point='A';
for(String str:list){
System.out.println("Variable"+point+" = "+str);
point++;
}
My input:
ExampleA ExampleAA, <other items seperated by ",">
ExampleB ExampleBB, <other items spereated by ",">
Out put:
VariableA = ExampleA ExampleAA
VariableB = ExampleB ExampleBB
To rephrase, you are looking to read the first 2 words of a line (everything before the first comma) and store it in a variable to process further.
To do so, your current code looks fine, however, when you grab the line's data, use the substring function in conjunction with indexOf to just get the first part of the String before the comma. After that, you can do whatever processing you want to do with it.
In your current code, ary[0] should give you the first 2 words.
public static void main(String[] args)
{
File file = new File("example.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = "";
List l = new ArrayList();
while ((line = br.readLine()) != null) {
System.out.println(line);
line = line.trim(); // remove unwanted characters at the end of line
String[] arr = line.split(",");
String[] ary = arr[0].split(" ");
String firstTwoWords[] = new String[2];
firstTwoWords[0] = ary[0];
firstTwoWords[1] = ary[1];
l.add(firstTwoWords);
}
Iterator it = l.iterator();
while (it.hasNext()) {
String firstTwoWords[] = (String[]) it.next();
System.out.println(firstTwoWords[0] + " " + firstTwoWords[1]);
}
}

removeAll operation on arraylist makes program hang

I'm trying to read in from two files and store them in two separate arraylists. The files consist of words which are either alone on a line or multiple words on a line separated by commas.
I read each file with the following code (not complete):
ArrayList<String> temp = new ArrayList<>();
FileInputStream fis;
fis = new FileInputStream(fileName);
Scanner scan = new Scanner(fis);
while (scan.hasNextLine()) {
Scanner input = new Scanner(scan.nextLine());
input.useDelimiter(",");
while (scan.hasNext()) {
String md5 = scan.next();
temp.add(md5);
}
}
scan.close();
return temp;
Each file contains almost 1 million words (I don't know the exact number), so I'm not entirely sure that the above code works correctly - but it seems to.
I now want to find out how many words are exclusive to the first file/arraylist. To do so I planned on using list1.removeAll(list2) and then checking the size of list1 - but for some reason this is not working. The code:
public static ArrayList differentWords(String fileName1, String fileName2) {
ArrayList<String> file1 = readFile(fileName1);
ArrayList<String> file2 = readFile(fileName2);
file1.removeAll(file2);
return file1;
}
My main method contains a few different calls and everything works fine until I reach the above code, which just causes the program to hang (in netbeans it's just "running").
Any idea why this is happening?
You are not using input in
while (scan.hasNextLine()) {
Scanner input = new Scanner(scan.nextLine());
input.useDelimiter(",");
while (scan.hasNext()) {
String md5 = scan.next();
temp.add(md5);
}
}
I think you meant to do this:
while (scan.hasNextLine()) {
Scanner input = new Scanner(scan.nextLine());
input.useDelimiter(",");
while (input.hasNext()) {
String md5 = input.next();
temp.add(md5);
}
}
but that said you should look into String#split() that will probably save you some time:
while (scan.hasNextLine()) {
String line = scan.nextLine();
String[] tokens = line.split(",");
for (String token: tokens) {
temp.add(token);
}
}
try this :
for(String s1 : file1){
for(String s2 : file2){
if(s1.equals(s2)){file1.remove(s1))}
}
}

ArrayList to Array

How do I convert this ArrayList's value into an array? So it can look like,
String[] textfile = ... ;
The values are Strings (words in the text file), and there are more than a 1000 words. In this case I cannot do the, words.add("") 1000 times. How can I then put this list into an array?
public static void main(String[]args) throws IOException
{
Scanner scan = new Scanner(System.in);
String stringSearch = scan.nextLine();
List<String> words = new ArrayList<String>(); //convert to array
BufferedReader reader = new BufferedReader(new FileReader("File1.txt"));
String line;
while ((line = reader.readLine()) != null) {
words.add(line);
}
You can use
String[] textfile = words.toArray(new String[words.size()]);
Relevant Documentation
List#toArray(T[])
words.toArray() should work fine.
List<String> words = new ArrayList<String>();
String[] wordsArray = (String[]) words.toArray();
you can use the toArray method of Collection such as shown here
Collection toArray example
List<String> words = new ArrayList<String>();
words.add("w1");
words.add("w2");
String[] textfile = new String[words.size()];
textfile = words.toArray(textfile);

Categories