Reading integers from a file separated by space in java - java

Input file containing integers will be like this:
5 2 3 5
2 4 23 4 5 6 4
So how would I read the first line, separate it by space and add these numbers to Arraylist1. Then read the second line, separate it by space and add the numbers to ArrayList2 and so on. (So Arraylist1 will contain [5,2,3,5] etc)
FileInputStream fstream = new FileInputStream("file.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String data;
while ((data = br.readLine()) != null) {
//How can I do what I described above here?
}

Homework?
You can use this:
String[] tmp = data.split(" "); //Split space
for(String s: tmp)
myArrayList.add(s);
Or you have a look at the Scanner class.

Consider using a StringTokenizer
Some help : String tokenizer
StringTokenizer st = new StringTokenizer(in, "=;");
while(st.hasMoreTokens()) {
String key = st.nextToken();
String val = st.nextToken();
System.out.println(key + "\t" + val);
}

You can get a standard array out of data.split("\\s+");, which will give you int[]. You'll need something extra to throw different lines into different lists.

After I tried the answer provided by HectorLector, it didn't work in some specific situation. So, here is mine:
String[] tmp = data.split("\\s+");
This uses Regular Expression

what you would require is something like an ArrayList of ArrayList. You can use the data.split("\\s+"); function in java to get all the elements in a single line in a String array and then put these elements into the inner ArrayList of the ArrayList of ArrayLists.
and for the next line you can move to the next element of the outer ArrayList and so on.

Related

Separating elements in a string by white space into two dimensional array

I am trying to store the following strings in a file into a two dimensional array. What code I have written works except for when an element contains a space, it separates into an additional element. Here is my file:
Student1 New York
Student 2 Miami
Student3 Chicago
So I would want my output to look like this:
[Student1] [New York]
[Student 2] [Miami]
[Student3] [Chicago]
This is my actual output:
[Student1] [New] [York]
[Student] [2] [Miami]
[Student3] [Chicago]
Here is what I've written so far:
String file= "file.txt";
BufferedReader br = new BufferedReader(new FileReader(file));
while ((file = br.readLine()) != null) {
if (!file.isEmpty()) {
String strSingleSpace = file.trim().replaceAll("\\s+", " ");
String[] obj = strSingleSpace.trim().split("\\s+");
int i=0;
String[][] newString = new String[obj.length][];
for(String temp : obj){
newString[i++]=temp.trim().split("\\s+");
}
List<String[]> yourList = Arrays.asList(newString);
System.out.println(yourList.get(0)[0] + " " + yourList.get(1)[0]);
Just giving you some "food for thought": your code is treating all lines the same way. As if they were looking exactly the same. Although you already made it very clear, that some lines have a different format.
In other words: there is no point in blindly splitting on spaces, if sometimes spaces belong into the first or the second column.
Instead:
Determine the last index of a number in a line - and then everything up to that index "makes up the first column".
The remainder of that line (after that last number) should go into the second column; only call trim() on that remaining string to get rid of the potentially leading spaces.
You could put all of that into a single matching regular expression too; but as that is probably some kind of homework; I leave that exercise to the reader.
I think for your specific test case it will work if you change this line:
String[] obj = strSingleSpace.trim().split("\\s+");\
to this:
String[] obj = strSingleSpace.trim().split("\\s+", 1);

Appending text from array list to a String takes a lot of time

I am reading a Simple Notepad Text file containing a lot of data actually in a 3mb of size so you can imagine the number of words it can have! The problem is I am reading this file into a string then splits the string so that I can hold each single word inside an ArrayList(String). It works fine for me but the actual problem is that I am processing this array list for some purpose and then again I have to append or you can say put all the words of array list back to the String!
so that the steps are:
I read a text file into a String (alltext)
Split all words into an arraylist
process that array list (suppose I removed all the stop words like is, am, are)
after processing on array list I want to put all the words of array list back to the string (alltext)
then I have to work with that string (alltext)
(alltext is the string that must contains the text after all processing)
The problem is that at step number 4 it takes a lot of time to append all the words back to the string my code is:
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
alltext += line.trim().replaceAll("\\s+", " ") + " ";
}
br.close();
//Adding All elements from all text to temp list
ArrayList<String> tempList = new ArrayList<String>();
String[] array = alltext.split(" ");
for (String a : array) {
tempList.add(a);
}
//remove stop words here from the temp list
//Adding File Words from List in One String
alltext = "";
for (String removed1 : tempList) {
System.out.println("appending the text");
alltext += removed1.toLowerCase() + " ";
//here it is taking a lot of time suppose 5-10 minutes for a simple text file of even 1.4mb
}
So I just want any idea so that I can reduce the time for an efficient processing and relax the machine! I will be thankful for any suggestions and ideas...
Thanks
Use a StringBuffer instead of a String.
A String is immutable and thus you create a new Object everytime you append, which takes more and more time the longer your String becomes. A StringBuffer is mutable and made for cases like yours.
I would recommend StringBuilder
According to this stringbuilder-and-stringbuffer-in-java it's faster than a StringBuffer also check if you need the ArrayList because you can iterate through the array too

Java How to read text file line by line and get that line's text separately (length text) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have text file like this.
12A aa65 3244 5 665 fr 65 3434344344343 888dds 77786334 6h
1114 22 6FF 7 d M6 h o8 665466676 8Pt 543NNv 9
The file is in columns of varying widths, e.g., the first column is 6 characters wide, the second column is 5, the third is 5, and so on.
I want to split each line into the values in the columns, like this for the first line:
12A , aa65 , 3244 , 5 , , 665 , fr , 65 , 3434344344343 , 888dds , 77786334 , 6h
use a Scanner to read the file and the subString(start, end) method to parse each field.
Scanner sc = new Scanner(new File("myFile"));
while (sc.hasNextLine()) {
String aLine = sc.nextLine();
String field1 = aLine.subString(0,6);
String field2 = aLine.subString(6,11);
...
}
Updated answer:
Ah, okay, you want to split up the text by the width of the columns. It looks like your column lengths are:
6
5
5
6
8
6
4
18
9
(the rest)
So read the lines with BufferedReader#readLine and then just use String#substring to get the individual parts of them, and possibly String#trim to trim off whitespace:
BufferedReader r = /*...get a BufferedReader for your input...*/;
String line;
String[] parts;
int[] columns = new int[]{ // The starting index of each column
6,
5+6,
5+5+6,
6+5+5+6,
8+6+5+5+6,
6+8+6+5+5+6,
4+6+8+6+5+5+6,
18+4+6+8+6+5+5+6,
9+18+4+6+8+6+5+5+6
};
int i;
int start, end;
int linelen;
// Read each line
while ((line = r.readLine()) != null) {
// Get its length
linelen = line.length();
// Get an array for the result
parts = new string[columns.length];
// Loop through our column starting indexes
for (i = 0; i < columns.length; ++i ) {
// Get the start and end indexes for this column
start = columns[i];
end = i < columns.length - 1 ? columns[i+1] : linelen;
// Is the string long enough?
if (linelen < start) {
// No, use null
parts[i] = null;
}
else {
// Yes, grab the text
parts[i] = line.substring(start, end > linelen ? linelen : end);
// Note - you may want `.trim()` on the end of the above, if you
// don't want trailing spaces (or leading spaces, but none of your
// examples has leading spaces).
}
}
// **Use the `parts` of this line.
}
You might also consider using a class rather than an array for parts, and putting the parsing logic for it within the class.
Original answer:
It sounds like you're looking for a combination of BufferedReader#readLine and String#split:
BufferedReader r = /*...get a BufferedReader for your input...*/;
String line;
String[] parts;
while ((line = r.readLine()) != null) {
parts = line.split(" +");
// Use the `parts` array
}
readLine reads lines from the input.
split splits a string into a string array using a delimiter defined by a regular expression. In your case, it looks like the delimiter is just one or more spaces.
You can use readline() and then split by space.
you want to do a string split like here.
i assume you have read the file and just want to split it.
use Str.split("\n") for lines and Str.split(" ") for spaces ( if needed)
Several forms of reader in java have a .ReadLine() method.
This will read input from the source until a newline character is encountered.
for file reading I usually use BufferedReader as a wrapper around a FileReader, as this is more efficient for bulk reading. (FileReaders read from the file for every call to a read method.)
edit to add:
if you want the results sorted, it's going to be much more efficient to read the data fully into memory then sort, since random disk access is very slow.
reading lines into a list or priority queue, with a custom comparator would achieve what you're after.

Get csv and compare lines. ArrayList? Java

i dont't use java very often and now i got some Problem.
I want to read a CSV file like this one:
A,B,C,D
A,B,F,K
E,F,S,A
A,B,C,S
A,C,C,S
Java don't know dynamic arrays, so i choose an ArrayList. This works so far. The Problem is:
How can I store the ArrayList? I think an other ArrayList would help.
This is what I got:
BufferedReader reader = new BufferedReader(
new InputStreamReader(this.getClass().getResourceAsStream(
"../data/" + filename + ".csv")));
List rows = new ArrayList();
String line;
while ((line = reader.readLine()) != null) {
rows.add(Arrays.asList(line.split(",")));
}
Now I get an ArrayList with a size of 5 for rows.size().
How do I get row[0][0] for example?
What do I want to do? The Problem is i want to find the same row except the last column.
For example i want to find row 0 and row 3.
thank you very much
Thank you all! You helped me a lot. =) Maybe Java and I will become friends =) THANKS!
You don't need to know the row size in advance, String.split() returns a String array:
List<String[]> rows = new ArrayList<String[]>();
String line = null;
while((line = reader.readLine()) != null)
rows.add(line.split(",", -1));
To access a specific row:
int len = rows.get(0).length;
String val = rows.get(0)[0];
Also, are you always comparing by the entire row except the last column? You could just take off the last value (line.replaceFirst(",.*?$", "")) and compare the rows as strings (have to be careful of whitespace and other formatting, of course).
A slightly different way:
Set<String> rows = new HashSet<String>();
String line = null;
while((line = reader.readLine()) != null){
if(!rows.add(line.substring(0, line.lastIndexOf(','))))
System.out.println("duplicate found: " + line);
}
Of course, modify as necessary if you actually need to capture the matching lines.
You'll need to declare an ArrayList of arrays. Asuming that csv file has a known number of columns, the only dynamic list needed here are the "rows" of your "table", formed by an ArrayList(rows) of arrays char[] (columns). (If not, then an ArrayList of ArrayList is fine).
It's just like a 2D table in any other language: an array of arrays. Just that in this case one of the arrays needs to be dynamic.
To read the file you'll need two loops. One that reads each line, just as you're doing, and another one that reads char per char.
Just a quick note: if you are going to declare an array like this:
char[] row = new char[5];
and then going to add each row to the ArrayList like this:
yourList.add(row);
You will have a list full of pointers to the same array. You'll need to use the .clone() method like this:
yourList.add(row.clone());
To access it like table[1][2], you'll need to use arraylist.get(1).get(2);

Why is my array deleting the zeroes from a file I am reading?

Well, I am attempting to read a text file that looks like this:
FTFFFTTFFTFT
3054 FTFFFTTFFTFT
4674 FTFTFFTTTFTF
... etc
And when I am reading it, everything compiles and works wonderfully, putting everything into arrays like this:
studentID[0] = 3054
studentID[1] = 4674
... etc
studentAnswers[0] = FTFFFTTFFTFT
studentAnswers[1] = FTFTFFTTTFTF
However, if the studentID has a leading or trailing zero, when I print it with System.out.println();, it deletes the leading and trailing zeroes! I have a feeling this is something simple, and I need to like copy the array or something. Thank you :)
Below is my code:
public static String[] getData() throws IOException {
int total = 0;
int[] studentID = new int[127];
String[] studentAnswers = new String[127];
String line = reader.readLine();
String answerKey = line;
StringTokenizer tokens;
while((line = reader.readLine()) != null) {
tokens = new StringTokenizer(line);
studentID[total] = Integer.parseInt(tokens.nextToken());
studentAnswers[total] = tokens.nextToken();
System.out.println(total + " " +studentID[total]);
total++;
}
return studentAnswers;
}
Use String instead of int. As a general rule, use integral types for calculations.
If you want to preserve the zeroes, don't use parseInt on the student IDs; just store them as strings.
The int type has no concept of leading zeroes.
To add leading zeroes for display, use one of the format methods:
System.out.format("%04d", 80);
Integer.parseInt(tokens.nextToken()); will return the integer, so leading zeros will be omitted.
Have a look to the DecimalFormat class to handle parsing/formating of numbers.

Categories