I'm working with data that is a String followed by spaces and then a numeric value.
ncols 10812
nrows 10812
xllcorner -107.0005555556
yllcorner 36.99944444444
cellsize 9.2592592593e-05
I'm trying to just read in just the numeric value. I know that from going to String to Integer or Double I can use the standard type conversions.
Integer.valueOf(stringOfInteger);
Double.valueOf(stringOfDouble);
In order to get just the numeric value I tried this as a test:
BufferedReader br = new BufferedReader(new FileReader(path));
String line = br.readLine();
line.replace("[a-z]","");
line.replace(" ","");
System.out.println(line);
and it output ncols 10812
I'm also worried about reading the cellsize value as it has an exponential.
You can do this for each line:
...
String[] fields = line.split("\\s+");
String name = fields[0];
float value = Float.parseFloat(fields[1]);
...
This code will split each line in fields using the spaces as a separator. The first field is a String so you can use it directly (or ignore it). The second one is a Float value so you have to convert it before using it. You can use Double if you prefer.
Try this one
BufferedReader br = new BufferedReader(new FileReader(path));
String line = null;
while ((line = br.readLine()) != null) {
// split each line based on spaces
String[] words = line.split("\\s+");
//first word is name
String name = words[0];
// next word is actual number
Double value = Double.valueOf(words[1]);
System.out.println(name + ":" + value);
}
// don't forget to close the stream
br.close();
Output:
ncols:10812.0
nrows:10812.0
xllcorner:-107.0005555556
yllcorner:36.99944444444
cellsize:9.2592592593E-5
If all you want all the numeric values do a split on the space and the second item will contain your numeric value. Then you can do any conversions as needed and not have to worry about removing any exponents.
String[] data = new line.split(" ");
//remove all the spaces from the second array for your data
data[1] = data[1].replaceAll("\\s", "");
//parse to whatever you need data[1] to be
You could use the split function in Java as follows:
String [] dataArr = data.split("[ \t]+"); //assumes #data is you data string variable name
The dataArr, then, will look like this:
dataArr[0] = "ncols"
dataArr[1] = "10812"
dataArr[2] = "nrows"
dataArr[3] = "10812"
.
.
.
dataArr[n - 1] = "9.2592592593e-05" // #n is the size of the array
You could, then, use the Integer.parseInt(String str) to parse your numerical data into integers.
Related
I am trying to read data from a text file using a Buffered Reader. I'm trying to split the data into two Arrays, one of them is a double and the other one is a string. Below is the text file content:
55.6
Scholtz
85.6
Brown
74.9
Alawi
45.2
Weis
68.0
Baird
55
Baynard
68.5
Mills
65.1
Gibb
80.7
Grovner
87.6
Weaver
74.8
Kennedy
83.5
Landry.
Basically I'm trying to take all the numbers and put it into the double array, and take all the names and put it into the string array. Any ideas?
You could possibly get the entire string from the buffered reader and then use regex to parse out the digits and other data. A regex like \d+\.*\d should work to parse out the digits. And then a regex like [A-Za-z]+ should get all of the names. Then take each set of data from the regular expressions and split them into their respective arrays using .split("").
Try this:
String file = "path to file";
double dArr[] = new double[100];
String sArr[] = new String[100];
int i = 0, j = 0;
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
Pattern p = Pattern.compile("([0-9]*)\\.[0-9]*"); // should start with any number of 0-9 then "." and then any number of 0-9
Matcher m = p.matcher(line);
if (m.matches()) {
dArr[i] = Double.parseDouble(line);
i++;
} else {
sArr[j] = line;
j++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
Suggestion: Try List instead of array if uncertain about number of elements
55 is treated as String as it is int
I'm doing a million different regex replacements of a string. Thus I decided to save all String regex's and String replacements in a file.txt. I tried reading the file line by line and replacing it but it is not working.
replace_regex_file.txt
aaa zzz
^cc eee
ww$ sss
...
...
...
...
a million data
Coding
String user_input = "assume 100,000 words"; // input from user
String regex_file = "replace_regex_file.txt";
String result="";
String line;
try (BufferedReader reader = new BufferedReader(new FileReader(regex_file)) {
while ((line = reader.readLine()) != null) { // while line not equal null
String[] parts = line.split("\\s+", 2); //split process
if (parts.length >=2) {
String regex = parts[0]; // String regex stored in first array
String replace = parts[1]; // String replacement stored in second array
result = user_input.replaceAll(regex, replace); // replace processing
}
}
} System.out.println(result); // show the result
But it does not replace anything. How can I fix this?
Your current code will only apply the last matching regex, because you don't assign the result of the replacement back to the input string:
result = user_input.replaceAll(regex, replace);
Instead, try:
String result = user_input;
outside the loop and
result = result.replaceAll(regex, replace);
if i have this line in a file: 2 18 4 3
and i want to read it as individual integers, how could i?
i'm using bufferreader:
BufferedReader(new FileReader("mp1.data.txt"));
i have tried to use:
BufferedReader(new RandomAccessFile("mp1.data.txt"));
so i can use the method
.readCahr();
but i got an error
if i use
int w = in.read();
it will read the ASCII, and i want it as it is(in dec.)
i was thinking to read it as a string first, but then could i separate each number?
also i was thinking to let each number in a line, but the file i have is long with numbers
Consider using a Scanner:
Scanner scan = new Scanner(new File("mp1.data.txt"));
You can then use scan.nextInt() (which returns an int, not a String) so long as scan.hasNextInt().
No need for that ugly splitting and parsing :)
However, note that this approach will continue reading integers past the first line (if that's not what you want, you should probably follow the suggestions outlined in the other answers for reading and handling only a single line).
Furthermore, hasNextInt() will return false as soon as a non-integer is encountered in the file. If you require a way to detect and handle invalid data, you should again consider the other answers.
It's important to approach larger problems in software engineering by breaking them into smaller ones. In this case, you've got three tasks:
Read a line from the file
Break it into individual parts (still strings)
Convert each part into an integer
Java makes each of these simple:
Use BufferedReader.readLine() to read the line as a string first
It looks like the splitting is as simple as splitting by a space with String.split():
String[] bits = line.split(" ");
If that's not good enough, you can use a more complicated regular expression in the split call.
Parse each part using Integer.parseInt().
Another option for the splitting part is to use the Splitter class from Guava. Personally I prefer that, but it's a matter of taste.
You can split() the String and then use the Integer.parseInt() method in order to convert all the elements to Integer objects.
try {
BufferedReader br = new BufferedReader(new FileReader("mp1.data.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String[] split = line.split("\\s");
for (String element : split) {
Integer parsedInteger = Integer.parseInt(element);
System.out.println(parsedInteger);
}
}
}
catch (IOException e) {
System.err.println("Error: " + e);
}
Once you read the line using BufferedReader, you can use String.split(regex) method to split the string by space ("\\s").
for(String s : "2 18 4 3".split("\\s")) {
int i = Integer.parseInt(s);
System.out.println(i);
}
If you use Java 7+, you can use this utility method:
List<String> lines = Files.readAllLines(file, Charset.forName("UTF-8"));
for (String line: lines) {
String[] numbers = line.split("\\s+");
int firstNumber = Integer.parseInt(numbers[0]);
//etc
}
Try this;
try{
// Open the file that is the first
FileInputStream fstream = new FileInputStream("textfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
//split line by whitespace
String[] ints = strLine.split(" ");
int[] integers = new int[ints.length];
// to convert from string to integers - Integer.parseInt ("123")
for ( int i = 0; i < ints.length; i++) {
integers[i] = Integer.parseInt(ints[i]);
}
// now do what you want with your integer
// ...
}
in.close();
} catch (Exception e) {//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
Supoose I have input a line "MOVE R1, R2", I split the words with respect to white space and store them individually into an array token[] as follows:
String[] token = new String[0];// array initialization
FileInputStream fstream = new FileInputStream("a.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
//Read file line by line and storing data in the form of tokens
While((strLine = br.readLine()) != null){
token = strLine.split(" ");// split w.r.t spaces
}
So the elements at each index are as follows:
token[0]=MOVE
token[1]=R1,
token[2]=R2
But what I want is as follows:
token[0]=MOVE
token[1]=1
token[2]=2
I want to store only the numerical values in the token[i] where i>0, trimming off R and comma(,).
I m unable to figure out how to use relaceAll() with arrays. How can I do that? Thanks in advance.
Try this code:
str = str.replaceAll("[^\\d.]", "");
This will trim off the non-numeric stuff.
Replace before tokenizing:
while((strLine = br.readLine()) != null){
strLine = strLine.replaceAll("[^\\d]","");
token = strLine.split(" ");// split w.r.t spaces
}
s=s.replaceAll("\\D", "");
for your individual split string elements in the while loop..
I want to make something read from inputstream to store in an int[] when I type "read 1 2 3 4". what should i do?
I do not know the size of the array, everything is dynamic...
Here is the current code:
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
String line = stdin.readLine();
StringTokenizer st = new StringTokenizer(line);
String command = st.nextToken();
if (command.equals("read")) {
while (st.nextToken() != null) {
//my problem is no sure the array size
}
}
You need to build something to parse the input stream. Assuming it's literally as uncomplex as you've indicated the first thing you need to do is get the line out of the InputStream, you can do that like this:
// InputStream in = ...;
// read and accrue characters until the linebreak
StringBuilder sb = new StringBuilder();
int c;
while((c = in.read()) != -1 && c != '\n'){
sb.append(c);
}
String line = sb.toString();
Or you can use a BufferedReader (as suggested by comments):
BufferedReader rdr = new BufferedReader(new InputStreamReader(in));
String line = rdr.readLine();
Once you have a line to process you need to split it into pieces, then process the pieces into the desired array:
// now process the whole input
String[] parts = line.split("\\s");
// only if the direction is to read the input
if("read".equals(parts[0])){
// create an array to hold the ints
// note that we dynamically size the array based on the
// the length of `parts`, which contains an array of the form
// ["read", "1", "2", "3", ...], so it has size 1 more than required
// to hold the integers, thus, we create a new array of
// same size as `parts`, less 1.
int[] inputInts = new int[parts.length-1];
// iterate through the string pieces we have
for(int i = 1; i < parts.length; i++){
// and convert them to integers.
inputInts[i-1] = Integer.parseInt(parts[i]);
}
}
I'm sure some of these methods can throw exceptions (at least read and parseInt do), I'll leave handling those as an exercise.
You either use a storing structure with nodes, that you can easily append one after another, or, if you really must use arrays, you need to allocate space periodically, as it becomes necessary.
Parse-out the data and keyword from your string then push it into something like this:
public static Integer[] StringToIntVec( String aValue )
{
ArrayList<Integer> aTransit = new ArrayList<Integer>();
for ( String aString : aValue.split( "\\ ") )
{
aTransit.add( Integer.parseInt( aString ) );
}
return aTransit.toArray( new Integer[ 0 ] );
}