Extracting values from file - java

I've got around 10 lines of data in a text file below containing the following
X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001
X-Value = -0.755030, Y-Value = 7.861651, Z-Value = 6.016289, Timestamp(milliseconds) = 23208
The code I have right now uses a BufferedReader reading every line of the file but what I really want to do is extract the X-Value, Y-Value, Z-Value and Timestamp(milliseconds) values from each line. Could this be done with using simple String methods such as substring or would this suit the use of regular expressions?

You can first split the strings by ,s, then split each part by =, then trim leading/trailing spaces as necessary.
You can use String.split() or java.util.StringTokenizer for this.

You can use String.split to split your string on , and =
String str = "X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001";
ArrayList<String> final_data = new ArrayList<String>();
String[] data = str.split(",");
for(String S : data)
final_data.add(S.trim().split("=")[1]);
for(String s : final_data)
System.out.println(s.trim());
Output =
-0.525108
7.746691
5.863008
23001

You can use scanner like this to extract your values:
String str = "X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001";
Scanner scanner = new Scanner(str);
if (scanner.findInLine("^X-Value\\s*=\\s*([^,]*),\\s*Y-Value\\s*=\\s*([^,]*),\\s*Z-Value\\s*=\\s*([^,]*),\\s*Timestamp\\(milliseconds\\)\\s+=\\s+([^,]*)\\s*$") != null) {
MatchResult result = scanner.match();
System.out.printf("x=[%s]; y=[%s]; z=[%s]; ts=[%s]%n", result.group(1), result.group(2), result.group(3), result.group(4));
}
scanner.close();
OUTPUT:
x=[-0.525108]; y=[7.746691]; z=[5.863008]; ts=[23001]

String s = "X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001";
s = s.replaceAll(" ", "");
String[] split = s.split("=|,");
BigDecimal x = new BigDecimal(split[1]);
BigDecimal y = new BigDecimal(split[3]);
BigDecimal z = new BigDecimal(split[5]);
String ts = split[7];

Why play around with split(), just go for a regex!
X-Value\s*=\s*([\d.+-]*).*Y-Value([\d.+-]*).*Z-Value\s*=\s*([\d.+-]*).*Timestamp\(milliseconds\)\s*=\s*(\d*)

Related

split an already split array string in Java

I have a csv that has been read and split in 3 different csvs. The csv was pipe separated and the split variable is saved in a string variable. I want to split the new string as comma separated string but as soon as I do that, it gives an exception.`
try(BufferedReader br1 = new BufferedReader(new FileReader(newcsvCategory))){
String line;
while ((line = br1.readLine()) != null) {
String[] value1 = line.split("\\|,",-1);
String Id = value1[0];
String CatId=value1[1];
["Active Catalog Detail (Network Id "|" Category Ids "]
["209"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["174"|"4900,10082,10119,10358,10039,5132,10011"]
["200"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"]
["181"|"4900,10358,10011"]
["240"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["206"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"]
["255"|"4900,10368,11093,11581,10082,10206,11621,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["251"|"4900,10368,11093,11581,10082,10206,11621,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["231"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["179"|"4900,10368,11618,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"]
["184"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"]
["187"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"]
["247"|"4900,10368,11093,11581,10082,10206,11621,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["215"|"10358"]
["216"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["238"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["224"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
I want split the first column and second column as pipe separated and then further separate the second column as comma separated.
I'd appreciate any help as I'm a newbie.
added code that is splitting CatId:
String[] temp = CatId.split(",",-1);
System.out.println(temp[1]);
Really, can't realise the questions, but give some notes.
// this source string: serveral columsn with different separators
String str = "209|4900,10368,11093,11581";
According to your code, you try to put all separate number into string array, with two steps:
String[] arr = str.split("\\|"); // not line.split("\\|,",-1)
// arr[0] = 209
// arr[1] = [4900,10368,11093,11581]
String[] tmp = arr[1].split(",")
// tmp[0] = 4900
// tmp[1] = 10368
// tmp[2] = 11093
// tmp[3] = 11581
If so, you can do it with one step:
String[] arr = str.split("[\\|,]");
// arr[0] = 209
// arr[1] = 4900
// arr[2] = 10368
// arr[3] = 11093
// arr[4] = 11581
You want to set the Limit of .split(..) to 2.
while ((line = br1.readLine()) != null) {
String[] value1 = line.split("\\|",2);
String Id = value1[0];
String CatId=value1[1]
};
To further split the contet of "CatId" use:
// if you need to replace unwanted chars first, you could just use the simple .replace:
CatId = CatId.replace("\"", "").replace("[", "").replace("]", "");
// Then, split the array just by ,
String[] catIdArray = CatId.split("\\,");

How do I extract only right hand side and left hand side values of "=" operator from a line in text file?

Like for eg., in “int bot = 235;” from a line in text file, I want to extract only “bot” and “235” and store it in a HashMap in Java.
You could use regexp:
String detail = "int bot = 235";
String pattern = "(\\w+) = (\\w+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(detail);
HashMap<String, String> result = new HashMap<>();
while (m.find()) {
result.put(m.group(1), m.group(2));
}
System.out.println(result);
gives
{bot=235}
You can use the string function split, like this:
String[] s = string.split("=");
String s1 = string[0]; // "int bot "
String s2 = parts[1]; // " 235;"

Parse the string

Can any one suggest how to parse the the below string?
Added Active10000000044: {activityId=Active1, schedule=1 22 * * 0, isEnabled=Y, type=global, runAtHost=null}
I want Active10000000044 part out to use further next step..
If you want the right of the ":" then you can use
String str = "Added Active10000000044: {activityId=Active1, schedule=1 22 * * 0, isEnabled=Y, type=global, runAtHost=null}:";
System.out.println(str.split(":")[1]);
The left can be found using
System.out.println(str.split(":")[0]);
It could be as simple as:
String str = str.replaceFirst("Added ","").replaceFirst(" .*","");
depending on whether you've given us the full suite of test data :-)
If you want the second word regardless of the first, you could try:
String str = str.replaceFirst("[^ ]+ +","").replaceFirst(" .*","");
Both those suggestions rely on the fact that the first word is not preceded by spaces and that the white space is actually spaces. Any deviation from that will require some slight tweaks.
Try this,
String str = "Added Active10000000044: {activityId=Active1, schedule=1 22 * * 0, isEnabled=Y, type=global, runAtHost=null}:";
String[] parts = str.split(":");
String part1 = parts[0]; // value "Added Active10000000044"
String[] SetU_need = part1.split(" ");
String u_need = SetU_need[1]; // value "Active10000000044"
try this
Splitter class is from Google guava library
String text = "Added Active10000000044: {activityId=Active1, schedule=1 22 * * 0, isEnabled=Y, type=global, runAtHost=null}:";
int indexOfOpenBrace = text.indexOf("{");
int indexOfCloseBrace = text.indexOf("}");
String valuesAsText = text.substring(indexOfOpenBrace+1, indexOfCloseBrace);
List<String> splitToList = Splitter.on(",").omitEmptyStrings().splitToList(valuesAsText);
Map<String, String> map = new HashMap<>();
for (String keyValues : splitToList) {
List<String> splitToKeyAndValues = Splitter.on("=").omitEmptyStrings().splitToList(keyValues);
map.put(splitToKeyAndValues.get(0), splitToKeyAndValues.get(1));
}
Set<String> keySet = map.keySet();
for (String key : keySet) {
System.out.println(key+":"+map.get(key));
}
Output
activityId:Active1
schedule:1 22 * * 0
type:global
runAtHost:null
isEnabled:Y

regex to match and replace two characters between string

I have a string String a = "(3e4+2e2)sin(30)"; and i want to show it as a = "(3e4+2e2)*sin(30)";
I am not able to write a regular expression for this.
Try this replaceAll:
a = a.replaceAll("\) *(\\w+)", ")*$1");
You can go with this
String func = "sin";// or any function you want like cos.
String a = "(3e4+2e2)sin(30)";
a = a.replaceAll("[)]" + func, ")*siz");
System.out.println(a);
this should work
a = a.replaceAll("\\)(\\s)*([^*+/-])", ") * $2");
String input = "(3e4+2e2)sin(30)".replaceAll("(\\(.+?\\))(.+)", "$1*$2"); //(3e4+2e2)*sin(30)
Assuming the characters within the first parenthesis will always be in similar pattern, you can split this string into two at the position where you would like to insert the character and then form the final string by appending the first half of the string, new character and second half of the string.
string a = "(3e4+2e2)sin(30)";
string[] splitArray1 = Regex.Split(a, #"^\(\w+[+]\w+\)");
string[] splitArray2 = Regex.Split(a, #"\w+\([0-9]+\)$");
string updatedInput = splitArray2[0] + "*" + splitArray1[1];
Console.WriteLine("Input = {0} Output = {1}", a, updatedInput);
I did not try but the following should work
String a = "(3e4+2e2)sin(30)";
a = a.replaceAll("[)](\\w+)", ")*$1");
System.out.println(a);

Cut ':' && " " from a String with a tokenizer

right now I am a little bit confused. I want to manipulate this string with a tokenizer:
Bob:23456:12345 Carl:09876:54321
However, I use a Tokenizer, but when I try:
String signature1 = tok.nextToken(":");
tok.nextToken(" ")
I get:
12345 Carl
However I want to have the first int and the second int into a var.
Any ideas?
You have two different patterns, maybe you should handle both separated.
Fist you should split the space separated values. Only use the string split(" "). That will return a String[].
Then for each String use tokenizer.
I believe will works.
Code:
String input = "Bob:23456:12345 Carl:09876:54321";
String[] words = input.split(" ")
for (String word : words) {
String[] token = each.split(":");
String name = token[0];
int value0 = Integer.parseInt(token[1]);
int value1 = Integer.parseInt(token[2]);
}
Following code should do:
String input = "Bob:23456:12345 Carl:09876:54321";
StringTokenizer st = new StringTokenizer(input, ": ");
while(st.hasMoreTokens())
{
String name = st.nextToken();
String val1 = st.nextToken();
String val2 = st.nextToken();
}
Seeing as you have multiple patterns, you cannot handle them with only one tokenizer.
You need to first split it based on whitespace, then split based on the colon.
Something like this should help:
String[] s = "Bob:23456:12345 Carl:09876:54321".split(" ");
System.out.println(Arrays.toString(s ));
String[] so = s[0].split(":", 2);
System.out.println(Arrays.toString(so));
And you'd get this:
[Bob:23456:12345, Carl:09876:54321]
[Bob, 23456:12345]
If you must use tokeniser then I tink you need to use it twice
String str = "Bob:23456:12345 Carl:09876:54321";
StringTokenizer spaceTokenizer = new StringTokenizer(str, " ");
while (spaceTokenizer.hasMoreTokens()) {
StringTokenizer colonTokenizer = new StringTokenizer(spaceTokenizer.nextToken(), ":");
colonTokenizer.nextToken();//to igore Bob and Carl
while (colonTokenizer.hasMoreTokens()) {
System.out.println(colonTokenizer.nextToken());
}
}
outputs
23456
12345
09876
54321
Personally though I would not use tokenizer here and use Claudio's answer which splits the strings.

Categories