Java - Parsing CSV into ArrayList (Need to recognize line breaks) - java

Preface: This is for an assignment in one of my classes.
I need to parse through a CSV file and add each string to an ArrayList so I can interact with each string individually with pre-coded functions.
My problem is that the final string in each line (which doesn't end with a comma) is combined with the first string in the next line and recognized as being at the same index in the ArrayList. I need to learn how to either add a line break or do something else that will stop my loop at the end of each line and read the next line separately. Perhaps there is a built-in method in the scanner class that I'm unaware of that does this for me? Help is appreciated!
Here is the information in the CSV file:
Fname,Lname,CompanyName,Balance,InterestRate,AccountInception
Sally,Sellers,Seashells Down By The Seashore,100.36,3,7/16/2002
Michael,Jordan,Jordan Inc.,1000000000,3,6/12/1998
Ignatio,Freely,Penultimate Designs,2300.76,2.4,3/13/1991
Here is my code so far
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class InterestCalculator {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(new File("smalltestdata-sallysellers.csv"));
// Chomp off at each new line, then add to array or arraylist
scanner.useDelimiter("\n");
ArrayList<String> data = new ArrayList<String>();
while (scanner.hasNext()) {
// Grab data between commas to add to ArrayList
scanner.useDelimiter(",");
// Add grabbed data to ArrayList
data.add(scanner.next());
}
System.out.println(data.get(10));
scanner.close();
}
}
And here is the output
7/16/2002
Michael

It seems like you just need to do...
String s[] = scanner.nextLine().split(",");
Collections.addAll(data, s);

Related

Java .split() only putting a .txt file into a single Array element [duplicate]

This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(16 answers)
Closed 1 year ago.
I am trying to split a .txt file into an array so I can access individual elements from it. However I get the following error, Index 1 out of bounds for length 1 at babySort.main(babySort.java:21).
I am unsure where I am going wrong because I used the same code on a test string earlier and it splits into the appropriate amount of elements.
I suspect it has something to do with the while loop, but I can't seem to wrap my mind around it, any help would be appreciated.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class babySort {
public static void main(String[] args) throws FileNotFoundException {
File inputFile = new File("src/babynames.txt");
Scanner in = new Scanner(inputFile);
String test = "Why wont this work?";
String[] test2 = test.split("\\s+");
System.out.println(test2[2]);
while (in.hasNext()) {
String input = in.next();
String[] inputSplit = input.split("\\s+");
//System.out.println(Arrays.toString(inputSplit));
System.out.println(inputSplit[1]);
}
}
}
From the documentation:
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
My understanding is that you want to read the input line-by-line. You can use FileReader instead of Scanner to read the file line-by-line. You can also use BufferedReader like so:
try (BufferedReader br = new BufferedReader(new FileReader(inputFile, StandardCharsets.UTF_8))) {
String input;
while ((input = br.readLine()) != null) {
// process the line
String[] inputSplit = input.split("\\s+");
//System.out.println(Arrays.toString(inputSplit));
System.out.println(inputSplit[1]);
}
}

Converting a string that contains multiple words to a vector of words

I have an InputStream file, I have to put all the words from that file into a vector of strings.
I tried multiple things to convert the InputStream file to where I can read all the words in it, but no matter what I always end up with a long string with all the words.
How can I separate all the words in the file to that I can put them in a vector of strings?
here is my code for the conversion from InputStream file to string:
public static InputStream vocabDoc = Librarian.class.getClassLoader().getResourceAsStream("Vocabulary.txt");
String str = new Scanner(vocabDoc,"UTF-8").useDelimiter("\\A").next();
System.out.println(str);
this is what the file "vocabDoc" contains (exactly):
file
vocabulary
test
is
one
this
for
if I try to put it in a vector it always come back as:
[file
vocabulary
test
is
one
this
for
]
and if I take out the "\n" it comes out as: [filevocabularytestisonethisfor], my goal is to have something like: [file, vocabulary, test, is, one, this, for] instead.
I'm not sure where to go from here and would really appreciate some help.
For the expected output, simply do it without using any explicit delimiter. Using Scanner#hasNext, you can test if the file more words to read.
Demo:
import java.io.InputStream;
import java.util.Scanner;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
InputStream vocabDoc = Main.class.getClassLoader().getResourceAsStream("Vocabulary.txt");
Scanner scanner = new Scanner(vocabDoc);
Vector<String> vector = new Vector<>();
while (scanner.hasNext()) {
vector.add(scanner.next());
}
scanner.close();
System.out.println(vector);
}
}
Output:
[file, vocabulary, test, is, one, this, for]

How to read in 1 specific column of a txt file and store into an Array or ArrayList [Java]

The txt file I need to pull data from. I am only concerned with the STID column as I need to compare their Hamming distances with other inputted STID names in another part of the program
Trial code using a Scanner
I was thinking of using a BufferedReader (Although in my first trial I used a Scanner) and then extracting the data using .add() into an ArrayList but was not sure how to implement this as I am new to programming. Any help would be greatly appreciated
With the nextLine() method of the Scanner object (https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()) you can get each line of your text file.
Getting the STID column is pretty easy as it is the first and always starts at the beginning of the line.
What I would do: just use nextLine() a few times to skip the lines you're not interested in. Once you reached the first line containing the first STID the nextLine() method will give you the whole line.
It seems that the STID is always 4 characters long so you could use the substring(0,4) method on the line to get only the 4 characters that you want. Once you have this you can just add this substring to your ArrayList.
I went the BufferedReader route, and came up with this:
import java.util.ArrayList;
import java.util.List;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Mesonet {
public static void main(String[] args) {
try {
List<String> STIDS = new ArrayList<String>();
BufferedReader reader = new BufferedReader(
new FileReader("Mesonet.txt"));
Pattern p = Pattern.compile("^([A-Z0-9]{4}).*");
reader.readLine(); // repeat as necessary to skip headers...
while (reader.ready()) {
String line = reader.readLine();
Matcher m = p.matcher(line);
if (m.matches()) {
STIDS.add(m.group(1));
}
}
for (String STID : STIDS) {
System.out.println(STID);
}
}
catch (FileNotFoundException err) {
System.out.println("Where is the file?");
}
catch (IOException err) {
System.out.println("IO Problem");
}
}
}
This will match the very first 4 upper-case letter and digit combinations and put them in the array, which then gets printed.

How to skip a character when using Scanner

I want to read words from a text file which looks like:
"A","ABILITY","ABLE","ABOUT","ABOVE","ABSENCE","ABSOLUTELY","ACADEMIC","ACCEPT","ACCESS","ACCIDENT","ACCOMPANY", ...
I read the words using split("\",\"") so I have them in a matrix. Unfortunately I cannot skip reading the first quotation mark, which starts my .txt file, so as a result in my console I have:
"A
ABILITY
ABLE
ABOUT
ABOVE
Do you know how can I skip the first quotation mark? I was trying both
Scanner in = new Scanner(file).useDelimiter("\"");
and parts[0].replace("\"", "");, but it doesn't work.
package list_1;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class exercise {
public static void main(String[] args) throws FileNotFoundException{
File file = new File("slowa.txt");
Scanner in = new Scanner(file).useDelimiter("\""); //delimiter doesn't work!
String sentence = in.nextLine();
String[] parts = sentence.split("\",\"");
parts[0].replace("\"", ""); //it doesn't work!
for (int i=0; i<10 ; i++){
System.out.println(parts[i]);
}
}
}
Strings are immutable which means that you can't change their state. Because of that replace doesn't change string on which it was invoked, but creates new one with replaced data which you need to store somewhere (probably in reference which stored original string). So instead of
parts[0].replace("\"", "");
you need to use
parts[0] = parts[0].replace("\"", "");
Anyway setting delimiter and using nextLine doesn't make much sense because this method is looking for line separators (like \n \r \r\n), not your delimiters. If you want to make scanner use delimiter use its next() method.
You can also use different delimiter which will represent " or ",". You can create one with following regex "(,")?.
So your code could look like
Scanner in = new Scanner(file).useDelimiter("\"(,\")?");
while(in.hasNext()){
System.out.println(in.next());
}
You can use this regular expression. It works for me:
Scanner in = new Scanner(file).useDelimiter("\"(,\")?");
while(in.hasNext()){
System.out.println(in.next());
}

Manipulating a text file in Java?

I'm trying to make a program that reads in an external .txt file and manipulates it. The file has 5 different groups of data, 4 lines each (2 are int, 2 string). I need to read in the file using the Scanner class, Make an object to hold each group of data (write a class which stores the data group as a single object (lets call it ProgramData)). Then I need to create a ProgamData object and put that into an ArrayList, and repeat for each of the 5 groups.
I have a text file, and I read it in with the Scanner (I confirmed that I did this right through printing on the command line). I'm completely lost from there. Any help at all would be greatly appreciated.
Not like this will help, but here's my code so far:
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Project1
{
public static void main (String[] args) throws IOException
{
File dataFile = new File("C:\\Users/data.txt");
Scanner fileReader = new Scanner(dataFile);
int firstLine = fileReader.nextInt();
int secondLine = fileReader.nextInt();
String whiteSpace = fileReader.nextLine();
String thirdLine = fileReader.nextLine();
String fourthLine = fileReader.nextLine();
ArrayList<String> newArray = new ArrayList<String>();
}
}
Make sure when you're reading the input file, use the Scanner class's hasNext() method. It detects if there is still a line in the file so you don't reach the end of the file. Use it like so
// get file input
// this will make sure there are still lines left within the
// file and that you have not reached the end of the file
while(fileReader.hasNext()) {
int firstLine = fileReader.nextInt();
int secondLine = fileReader.nextInt();
String whiteSpace = fileReader.nextLine();
String thirdLine = fileReader.nextLine();
String fourthLine = fileReader.nextLine();
}
You need to take the provided above to do the operations you are looking for.
Here are the steps you can follow:
Create a class named as ProgramData
Make a constructor which will accept your group data. -->
What is constructor
Now in Project1 Class read the file properly. --> Scanner Tutorial and Reading a txt file using scanner java
Once you get all the first group data from file pass it to ProgramData class and create instance something like
ProgramData pd1 = new ProgramData (/* list of parameter */)
Add that ProgramData instace to Arraylist like below
// Define Arraylilst
ArrayList<ProgramData > list= new ArrayList<ProgramData >();
// Do some operation like reading or collecting the data and creating object
// shown in step 4
list.add(pd1); // add new object of group to list.
I hope this will help you to achieve your goal. If you have any question just ask. Good luck

Categories

Resources