ArrayList to Array - java
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);
Related
Trying to separate a txt file into two ArrayLists?
This image is a text file I need to separate by date and the digits alongside it BufferedReader wordReader = new BufferedReader(new FileReader("\\Users\\rosha\\eclipse-workspace\\working\\src\\workingfix\\spx_data_five_years.txt")); ArrayList<String> spxIndex = new ArrayList<>(); ArrayList<String> date = new ArrayList<>(); //populating the Array with the file String line = wordReader.readLine(); while (line != null) { date.add(line); line = wordReader.readLine(); } wordReader.close(); Would really love to understand how to separate this file into two Arrays. Been at it for a while and some Guidance in the right direction would be incredible. Apologies if it's a simple solution for some reason I'm having trouble getting started. Here is the some of the Text File, if I can get guidance on this I'll be in good shape 1/4/2010 1132.99 1/5/2010 1136.52 1/6/2010 1137.14 1/7/2010 1141.69 1/8/2010 1144.98 1/11/2010 1146.98 1/12/2010 1136.22 1/13/2010 1145.68
Your two examples are a little bit different, in the Image, it seems like the entries are separated by a tab. In your text example, the entries are separated by a space. If they are separated by a space, a simple String[] splitter = line.split(" "); suffices. This gives you the result as an Array, which you can write in the ArrayLists.
Here is the solution, using split method public static void main(String[] args) throws IOException { ArrayList<String> spxIndex = new ArrayList<>(); ArrayList<String> date = new ArrayList<>(); String sCurrentLine; BufferedReader br = new BufferedReader(new FileReader("\\Users\\rosha\\eclipse-workspace\\working\\src\\workingfix\\spx_data_five_years.txt")); while ((sCurrentLine = br.readLine()) != null) { String[] lineValues = sCurrentLine.split(" "); date.add(lineValues[0]); spxIndex.add(lineValues[1]); } br.close(); System.out.println(date); System.out.println(spxIndex); }
Spliting file content into ArrayList
I have a file where I get to read it's content. Now I would like to split each seperate line into an arrayList individually and cant succeed. This is what I have so far try { input = new FileInputStream(test); byte testContent[] = new byte[(int) test.length()]; input.read(testContent); String testFile = new String(testContent); System.out.println(testFile); } An example of file content is as follows, 0,0,5,13,9,1,0,0,0,0,13,15,10,15,5,0,0,3,15,2,0,11,8,0,0,4,12,0,0,8,8,0,0,5,8,0 0,0,0,12,13,5,0,0,0,0,0,11,16,9,0,0,0,0,3,15,16,6,0,0,0,7,15,16,16,2,0,0,0,0,1,1 0,0,0,4,15,12,0,0,0,0,3,16,15,14,0,0,0,0,8,13,8,16,0,0,0,0,1,6,15,11,0,0,0,1,8,1 0,0,7,15,13,1,0,0,0,8,13,6,15,4,0,0,0,2,1,13,13,0,0,0,0,0,2,15,11,1,0,0,0,0,0,1 0,0,0,1,11,0,0,0,0,0,0,7,8,0,0,0,0,0,1,13,6,2,2,0,0,0,7,15,0,9,8,0,0,5,16,10,0,1 I would like the above to be in arrays like [0,0,5,13,9,1,0,0,0,0,13,15,10,15,5,0,0,3,15,2,0,11,8,0,0,4,12,0,0,8,8,0,0,5,8,0] [0,0,0,12,13,5,0,0,0,0,0,11,16,9,0,0,0,0,3,15,16,6,0,0,0,7,15,16,16,2,0,0,0,0,1,1] [0,0,0,4,15,12,0,0,0,0,3,16,15,14,0,0,0,0,8,13,8,16,0,0,0,0,1,6,15,11,0,0,0,1,8,1] [0,0,7,15,13,1,0,0,0,8,13,6,15,4,0,0,0,2,1,13,13,0,0,0,0,0,2,15,11,1,0,0,0,0,0,1] [0,0,0,1,11,0,0,0,0,0,0,7,8,0,0,0,0,0,1,13,6,2,2,0,0,0,7,15,0,9,8,0,0,5,16,10,0,1] Thanks in advance for any help
How about Files.readAllLines(): List<String> lines = Files.readAllLines(new File(test).toPath()); If you're using Java 7, you'll need this version: List<String> lines = Files.readAllLines(new File(test).toPath(), StandardCharsets.UTF_8);
String [] lines = testFile.split("\\r?\\n");
Use String#split() String[] strings= testFile.split("\\r?\\n"); List<String> list = Arrays.asList(strings); You can have more details about this method from this question How to split a string in Java
You can try something like this: BufferedReader br = new BufferedReader(new FileReader(yourFile)); List<String[]> listOfArrays = new ArrayList<String[]>(); String nextLine; while((nextLine = br.readLine()) != null){ String[] lineArray = nextLine.split(","); listOfArrays.add(lineArray); }
How to mask the content of a text file in Java
I am looking to mask the content of a text file. Ex: The text file contains data like Peter|peter#gmail.com|312-445-9988|....| John|john#gmail.com|123-457-6789|....| Expected Output: Peter|XXXXX#gmail.com|XXX-XXX-XXXX|....| John|XXXX#gmail.com|XXX-XXX-XXXX|....| I have to mask the content like phone number and mail ID till peter not #gmail.com Here is my code that I tried I have tried till reading the data from the text file after that I am not getting any ideas... public class DataMasking { public static void main(String args[]) throws IOException{ BufferedReader in = new BufferedReader(new FileReader("Filepath")); String str; List<String> parts = new ArrayList<String>(); while ((str = in.readLine()) != null) { parts.add(str); } int size = parts.size(); //we are reducing the size by one because we are not counting the first line(Only contains file name and time stamp). size = size-1; System.out.println("The Number of lines in the text file "+size); Any help is appreciated.
Okay so may be you want something like this. Try it - BufferedReader in = new BufferedReader(new FileReader("filepath")); String str; List<String> parts = new ArrayList<String>(); while ((str = in.readLine()) != null) { parts.add(str); } List<String> newList = new ArrayList<String>(); List<String> emailPart=new LinkedList<String>(); List<String> numberPart=new LinkedList<String>(); for(int i=1;i<parts.size();i++){ String[] strArr=parts.get(i).split("\\|"); for(int j=0;j<strArr.length;j++){ if(strArr[j].matches(".*#.*")){ int index=strArr[j].indexOf("#"); emailPart.add(strArr[j].substring(0, index).replaceAll("[A-Za-z0-9]", "X")+ strArr[j].substring(index, strArr[j].length())); } if(strArr[j].matches("[0-9\\-]+")){ numberPart.add(strArr[j].replaceAll("[0-9]", "X")); } } newList.add(strArr[0]+"|"+emailPart.get(i-1)+"|"+numberPart.get(i-1)); } System.out.println(newList);
Reading text into an ArrayList
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+|,");
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]); } }