I have a String in the following format:
[{"HostName":"taskmanager1","Rack":"/default-rack","State":"RUNNING","NodeId":"taskmanager1:45454","NodeHTTPAddress":"taskmanager1:8042","LastHealthUpdate":1519568501615,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},{"HostName":"datanode2","Rack":"/default-rack","State":"RUNNING","NodeId":"datanode2:45454","NodeHTTPAddress":"datanode2:8042","LastHealthUpdate":1519260876106,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},{"HostName":"taskmanager3","Rack":"/default-rack","State":"RUNNING","NodeId":"taskmanager3:45454","NodeHTTPAddress":"taskmanager3:8042","LastHealthUpdate":1519568502251,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},{"HostName":"datanode3","Rack":"/default-rack","State":"RUNNING","NodeId":"datanode3:45454","NodeHTTPAddress":"datanode3:8042","LastHealthUpdate":1519260871527,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},{"HostName":"taskmanager2","Rack":"/default-rack","State":"RUNNING","NodeId":"taskmanager2:45454","NodeHTTPAddress":"taskmanager2:8042","LastHealthUpdate":1519568502259,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},{"HostName":"datanode1","Rack":"/default-rack","State":"RUNNING","NodeId":"datanode1:45454","NodeHTTPAddress":"datanode1:8042","LastHealthUpdate":1519260875647,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024}]
I want to split it into multiple (here 6) JSON format, but my pattern cannot split that as desired.
I want something like this:
{"HostName":"taskmanager1","Rack":"/default-rack","State":"RUNNING","NodeId":"taskmanager1:45454","NodeHTTPAddress":"taskmanager1:8042","LastHealthUpdate":1519568501615,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},
{"HostName":"datanode2","Rack":"/default-rack","State":"RUNNING","NodeId":"datanode2:45454","NodeHTTPAddress":"datanode2:8042","LastHealthUpdate":1519260876106,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},
{"HostName":"taskmanager3","Rack":"/default-rack","State":"RUNNING","NodeId":"taskmanager3:45454","NodeHTTPAddress":"taskmanager3:8042","LastHealthUpdate":1519568502251,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},
{"HostName":"datanode3","Rack":"/default-rack","State":"RUNNING","NodeId":"datanode3:45454","NodeHTTPAddress":"datanode3:8042","LastHealthUpdate":1519260871527,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024}
,{"HostName":"taskmanager2","Rack":"/default-rack","State":"RUNNING","NodeId":"taskmanager2:45454","NodeHTTPAddress":"taskmanager2:8042","LastHealthUpdate":1519568502259,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},
{"HostName":"datanode1","Rack":"/default-rack","State":"RUNNING","NodeId":"datanode1:45454","NodeHTTPAddress":"datanode1:8042","LastHealthUpdate":1519260875647,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024}
Using the code:
List<String> res = Arrays.asList(temp.replace('[', ' ').replace(']',' ').trim()).split(",");
It will be split for every , character and using the pattern split("},\\}") will remove } and { character, too.
How can I split that as desire to make Json objects?
Using the Java pattern (\\{.+}) will group whole string.
You can parse the JSON as an array and treat the contents as individual strings. Here is sample code:
import org.json.JSONArray;
public class orgJson1Main {
private static final String sample = "[{\"HostName\":\"taskmanager1\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"taskmanager1:45454\",\"NodeHTTPAddress\":\"taskmanager1:8042\",\"LastHealthUpdate\":1519568501615,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"datanode2\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"datanode2:45454\",\"NodeHTTPAddress\":\"datanode2:8042\",\"LastHealthUpdate\":1519260876106,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"taskmanager3\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"taskmanager3:45454\",\"NodeHTTPAddress\":\"taskmanager3:8042\",\"LastHealthUpdate\":1519568502251,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"datanode3\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"datanode3:45454\",\"NodeHTTPAddress\":\"datanode3:8042\",\"LastHealthUpdate\":1519260871527,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"taskmanager2\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"taskmanager2:45454\",\"NodeHTTPAddress\":\"taskmanager2:8042\",\"LastHealthUpdate\":1519568502259,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"datanode1\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"datanode1:45454\",\"NodeHTTPAddress\":\"datanode1:8042\",\"LastHealthUpdate\":1519260875647,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024}]";
public static void main(String[] args) {
JSONArray array = new JSONArray(sample);
for(int i=0; i < array.length(); i++){
System.out.println(array.get(i));
}
}
}
OUTPUT:
{"NodeManagerVersion":"2.8.3","Rack":"/default-rack","LastHealthUpdate":1519568501615,"HealthReport":"","State":"RUNNING","AvailableMemoryMB":1024,"NodeId":"taskmanager1:45454","UsedMemoryMB":0,"NodeHTTPAddress":"taskmanager1:8042","HostName":"taskmanager1","NumContainers":0}
{"NodeManagerVersion":"2.8.3","Rack":"/default-rack","LastHealthUpdate":1519260876106,"HealthReport":"","State":"RUNNING","AvailableMemoryMB":1024,"NodeId":"datanode2:45454","UsedMemoryMB":0,"NodeHTTPAddress":"datanode2:8042","HostName":"datanode2","NumContainers":0}
{"NodeManagerVersion":"2.8.3","Rack":"/default-rack","LastHealthUpdate":1519568502251,"HealthReport":"","State":"RUNNING","AvailableMemoryMB":1024,"NodeId":"taskmanager3:45454","UsedMemoryMB":0,"NodeHTTPAddress":"taskmanager3:8042","HostName":"taskmanager3","NumContainers":0}
{"NodeManagerVersion":"2.8.3","Rack":"/default-rack","LastHealthUpdate":1519260871527,"HealthReport":"","State":"RUNNING","AvailableMemoryMB":1024,"NodeId":"datanode3:45454","UsedMemoryMB":0,"NodeHTTPAddress":"datanode3:8042","HostName":"datanode3","NumContainers":0}
{"NodeManagerVersion":"2.8.3","Rack":"/default-rack","LastHealthUpdate":1519568502259,"HealthReport":"","State":"RUNNING","AvailableMemoryMB":1024,"NodeId":"taskmanager2:45454","UsedMemoryMB":0,"NodeHTTPAddress":"taskmanager2:8042","HostName":"taskmanager2","NumContainers":0}
{"NodeManagerVersion":"2.8.3","Rack":"/default-rack","LastHealthUpdate":1519260875647,"HealthReport":"","State":"RUNNING","AvailableMemoryMB":1024,"NodeId":"datanode1:45454","UsedMemoryMB":0,"NodeHTTPAddress":"datanode1:8042","HostName":"datanode1","NumContainers":0}
EDIT:
First, I removed the JSONTokener from the above code. Second, for completeness I'm adding the following code that shows how to find the individual JSON objects within the sample string using a regex as originally asked.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class orgJson1Main {
private static final String sample = "[{\"HostName\":\"taskmanager1\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"taskmanager1:45454\",\"NodeHTTPAddress\":\"taskmanager1:8042\",\"LastHealthUpdate\":1519568501615,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"datanode2\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"datanode2:45454\",\"NodeHTTPAddress\":\"datanode2:8042\",\"LastHealthUpdate\":1519260876106,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"taskmanager3\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"taskmanager3:45454\",\"NodeHTTPAddress\":\"taskmanager3:8042\",\"LastHealthUpdate\":1519568502251,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"datanode3\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"datanode3:45454\",\"NodeHTTPAddress\":\"datanode3:8042\",\"LastHealthUpdate\":1519260871527,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"taskmanager2\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"taskmanager2:45454\",\"NodeHTTPAddress\":\"taskmanager2:8042\",\"LastHealthUpdate\":1519568502259,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"datanode1\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"datanode1:45454\",\"NodeHTTPAddress\":\"datanode1:8042\",\"LastHealthUpdate\":1519260875647,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024}]";
public static void main(String[] args) {
Matcher matcher = Pattern.compile("\\{[^}]*\\}").matcher(sample);
while(matcher.find()){
System.out.println(matcher.group());
}
}
}
To split on }, {, but retain the curly brackets in the tokens, split on this regex:
"(?<=\\}), (?=\\{)"
Which uses a look behind and a look ahead to assert the curly brackets preceed and follow the comma, but not consume them in the split.
The whole line then becomes:
List<String> res = Arrays.asList(temp.replaceAll("^.|.$", "").split("(?<=\\}), (?=\\{)");
Note also the simplified trimming of leading [ and trailing ] but more-simply removing the first and last character in one operation.
If your purpose to use this List as list of MyJsonObject I would recommend to reuse brilliant google gson library.
There is easy way to convert String to List without intermediate manipulation with List.
What you need to follow followed steps.
1) Create your POJO class:
public class POJO
{
String HostName;
String Rack;
String State;
String NodeId;
String NodeHTTPAddress;
String LastHealthUpdate;
String HealthReport;
String NodeManagerVersion;
String NumContainers;
String UsedMemoryMB;
String AvailableMemoryMB;
... getters/setters here ....
}
2) Create gson converter:
Gson gson = (new GsonBuilder() ).create();
3) Create typeToken for list of your POJOs:
Type type = new TypeToken< List<POJO> >(){}.getType();
4) Convert String to desire collection:
List<MyJsonObject> list = gson.fromJson( json, type );
Hello friends i am having problem in parsing jSON web services data
I have the following set of data i got from my web services
[{"store_id":"81","store_name":"Mayo - Castlebar McDrive","store_type":"Drive-Thru",
"vouchers_available":"Vouchers available","store_limit":"10","distance":"8123.33 km",
"latitude":"53.8501090162671","longitude":"-9.29713726043701","image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/stores\/default.png",
"voucher_count":"2","is_open":"Restaurant Open","attributes":[{"attribute_name":"Wi-Fiiiii",
"image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/attributes\/t_wifi_icon.gif"},{"attribute_name":"Cashless",
"image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/attributes\/t_cashless_icon.gif"},
{"attribute_name":"McDrive","image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/attributes\/car_icon.png"}]}]
and i m using this code to parse the DATA but i am getting the error
Please friends i am new in JSON Web services guide me what am i doing wrong.
CODE:
JSONObject jObject = new JSONObject(data);
JSONArray array = jObject.getJSONArray("attributes");
ERROR:
07-19 23:43:02.437: WARN/System.err(674): org.json.JSONException: A JSONObject text must begin with '{' at character 2 of
M waiting for some positive response and guidelines
Thanks
If you're going to work with JSON data, I recommend reviewing the JSON introduction at http://json.org, until it is thoroughly understood. Luckily, JSON is a relatively simple data format, and becoming comfortable with it comes quickly.
To the specific problem in the original question, note that the outer-most structure of the JSON data is an array. So, it needs to be read as an array -- not as an object.
Here's a brief description of the complete JSON structure.
An array with one element that is an unnamed object. The unnamed object has twelve elements, eleven of which are strings, and one of which, named "attributes", is an array of three objects. Each object in the "attributes" array has two string elements.
So, if you want the "attributes" array, first read in the entire contents as an array, then get the first component of the array as an object, then get the "attributes" element from that object as an array. Following is an example of doing this.
import java.math.BigDecimal;
import java.net.URI;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class Foo
{
public static void main(String[] args) throws Exception
{
JSONArray outerArray = new JSONArray("[{\"store_id\":\"81\",\"store_name\":\"Mayo - Castlebar McDrive\",\"store_type\":\"Drive-Thru\",\"vouchers_available\":\"Vouchers available\",\"store_limit\":\"10\",\"distance\":\"8123.33 km\",\"latitude\":\"53.8501090162671\",\"longitude\":\"-9.29713726043701\",\"image_name\":\"http:\\/\\/www.mcfinder.ie\\/admin\\/images\\/stores\\/default.png\",\"voucher_count\":\"2\",\"is_open\":\"Restaurant Open\",\"attributes\":[{\"attribute_name\":\"Wi-Fiiiii\",\"image_name\":\"http:\\/\\/www.mcfinder.ie\\/admin\\/images\\/attributes\\/t_wifi_icon.gif\"},{\"attribute_name\":\"Cashless\",\"image_name\":\"http:\\/\\/www.mcfinder.ie\\/admin\\/images\\/attributes\\/t_cashless_icon.gif\"},{\"attribute_name\":\"McDrive\",\"image_name\":\"http:\\/\\/www.mcfinder.ie\\/admin\\/images\\/attributes\\/car_icon.png\"}]}]");
JSONObject object = outerArray.getJSONObject(0);
JSONArray attributes = object.getJSONArray("attributes");
for (int i = 0, length = attributes.length(); i < length; i++)
{
JSONObject attribute = attributes.getJSONObject(i);
System.out.printf("attribute name=%s, image=%s\n", attribute.getString("attribute_name"), attribute.getString("image_name"));
}
}
}
If you're not stuck using the built-in JSON API that Android provides, I highly recommend switching to Jackson, which makes it very easy to read and write arbitrarily complex JSON with Java. Following is an example of using it.
import java.io.File;
import java.math.BigDecimal;
import java.net.URI;
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
public class Foo
{
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Store[] stores = mapper.readValue(new File("input.json"), Store[].class);
Store store = stores[0];
List<Attribute> attributes = store.attributes;
for (Attribute attribute : attributes)
{
System.out.printf("attribute name=%s, image=%s\n", attribute.attribute_name, attribute.image_name);
}
// output:
// attribute name=Wi-Fiiiii, image=http://www.mcfinder.ie/admin/images/attributes/t_wifi_icon.gif
// attribute name=Cashless, image=http://www.mcfinder.ie/admin/images/attributes/t_cashless_icon.gif
// attribute name=McDrive, image=http://www.mcfinder.ie/admin/images/attributes/car_icon.png
}
}
class Store
{
public String store_id;
public String store_name;
public String store_type;
public String vouchers_available;
public String store_limit;
public String distance;
public BigDecimal latitude;
public BigDecimal longitude;
public URI image_name;
public int voucher_count;
public String is_open;
public List<Attribute> attributes;
}
class Attribute
{
public String attribute_name;
public URI image_name;
}
The answer by #Richard aka cyberkiwi for this question should be the answer for your question.
He says:
You may be passing the STRING to JSONObject with leading spaces. Try trimming
JSONObject allCDs = new JSONObject(objectString.replace(/^\s+/,""));
EDIT: I thought this was javascript. Try trimming it using Java code instead
JSONObject allCDs = new JSONObject(objectString.trim());
If that still doesn't work, then show what the first character from the string is:
System.out.println((int)objectString.trim().charAt(0));
You should be expecting 123, the curly braces. In fact, check the entire content
System.out.println((int)objectString); // or
System.out.println((int)objectString.trim());
You could also try cutting everything before the { in the string
JSONObject allCDs = new JSONObject(objectString.substring(objectString.indexOf('{')));
If you're taking the Udacity android course and encountering this error for the quakereport/DidUfeelIt app then change the URL and try with some other URL your problem will be solved. Eg:- The URL provided by during the course was "http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6"
Then I was getting the same error that is "problem parsing the JSON" So I tried different URL: https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson
And it worked..!! Always try to get the latest URL's from the USGS website during the course.
ID Name Department Salary Designation
---------------------------------------
1 Kent Engineering 100000$ senior-engineer
2 Smith null 50,000$ administrator
These are the records of a big file I have. I need to parse this file to retrieve salary and designation. The key has to be ID,Name,Department and if that fails then use ID,Name ..
I can only use Java/groovy to do this... How do I get <Salary,Designation> back... ?
What's the most efficient way to grep it given that i can use only java/groovy
Create a BufferedReader, this will allow you to read the file one line at a time. Then call split("[\\\s]") on each of the lines, this will split it by whitespace.
You may also have to massage the salary field so it can be parsed into a number.
Split each line on whitespace.
scala> val a = "1 Kent Engineering 100000$ senior-engineer"
a: java.lang.String = 1 Kent Engineering 100000$ senior-engineer
scala> a.split("\\s+")
res1: Array[java.lang.String] = Array(1, Kent, Engineering, 100000$, senior-engineer)
Then take the raw string values and parse those. E.g. to convert 50,000$ into the number 50000:
double salary = Double.parseDouble(array[SALARY_INDEX].replaceAll("[$,]",""))
java.lang.Double.parseDouble("50,000$".replaceAll("[$,]",""))
res6: Double = 50000.0
You can parse the file into a HashMap and keep it around as long as you need it.
It has a high up-front cost, but if you're going to use it a lot, then it makes sense.
Then create your key and value objects. Be sure to override equals for your key.
public class Key{
int id;
String name;
#Override
public boolean equals(Object o){...}
}
I'll assume that you're aware of how to read files line-by-line using a BufferedReader as that's an obvious prerequisite for this sort of thing - if not, shout.
The key is the thing that you've failed to specify. What, exactly, is the format of the data - in particular, what are the exact rules for determining where one field ends and the other begins?
If the data is separated by tab characters (and said characters are forbidden from occurring in the data, even if escaped) then the solution is simple:
// Ignoring general error handling and EOF-checking here
final String line = bufferedReader.readLine();
final String[] fields = line.split("\t");
Now you have an array containing the fields on the line, so can just look up fields[3] and fields[4].
If the separator is "any number of spaces", and spaces are entirely disallowed, then you've got a similarly easy situation, where your regex is line.split(" *").
In more complex situations, including ones where separators are allowed to appear if escaped or quoted, you may be better off simply iterating over the line character-by-character and manually building up the set of fields according to the separator rules.
With a pinch of Guava library, the solution is simple and elegant. We can improve the below code by objectifying the key, handling errors etc etc but you can do that yourself
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
public class FileProcessor
{
private static final Splitter SPLITTER = Splitter.on(CharMatcher.WHITESPACE);
private static final Joiner KEY_BUILDER = Joiner.on("_").skipNulls();
#SuppressWarnings("unchecked")
public static void main(final String[] args) throws IOException
{
Map<String, SalaryAndDesignation> result = Files.readLines(new File("c:/1.txt"), Charsets.ISO_8859_1, new LineProcessor() {
private final Map<String, SalaryAndDesignation> result = Maps.newHashMap();
public Object getResult()
{
return result;
}
public boolean processLine(final String line) throws IOException
{
Iterator<String> columns = SPLITTER.split(line).iterator();
String id = columns.next();
String name = columns.next();
String dept = columns.next();
String key = KEY_BUILDER.join(id, name, "null".equals(dept) ? null : dept);
result.put(key, new SalaryAndDesignation(columns.next(), columns.next()));
return true;
}
});
System.out.println(result.size());
}
final static class SalaryAndDesignation
{
String salary;
String designation;
public SalaryAndDesignation(final String salary, final String designation)
{
super();
this.salary = salary;
this.designation = designation;
}
}
}
I see using groovy results in lesser code as sample below (copy/pasted from here) but never used it myself
new File("simple.tab").withReader{r->
line = r.readLine();
println "first line: $line"
r.splitEachLine("\t"){fields->
println "fields on line: $fields"
}
}