snippetjavaMinor
Create Java object from from page content or plain text
Viewed 0 times
fromcreatetextjavapagecontentobjectplain
Problem
My job is to make a specified java objects from web page that contains following text:
Companies:
Materials:
This code took me a lot of time to be written and seems to be very complicated and non-efficient in my opinion. Here is source code with my description
This is the basic method that taking page content (described in the beginning). Splits it to array by "," sign then removes all special characters like "{}[] then making 2d array from them by splitting with ":" char. There is an algorithm works "parallely" that every nth step (whe
Companies:
[{"companyName":"Tire Systems","companyID":1},{"companyName":"IT Enterprise","companyID":2},{"companyName":"Car Manufacture","companyID":3},{"companyName":"Electro Market","companyID":4}]Materials:
[{"name":"Winter Tire","ID":1,"companyID":1},{"name":"Summer Tire","ID":2,"companyID":1},{"name":"Tire","ID":3,"companyID":1},{"name":"Reporting Software","ID":4,"companyID":2},{"name":"Text Editor","ID":5,"companyID":2},{"name":"Passenger Car","ID":6,"companyID":3},{"name":"Truck","ID":7,"companyID":3},{"name":"Pickup","ID":8,"companyID":3},{"name":"Laptop","ID":9,"companyID":4},{"name":"Smartphone","ID":10,"companyID":4},{"name":"Tablet","ID":11,"companyID":4}]This code took me a lot of time to be written and seems to be very complicated and non-efficient in my opinion. Here is source code with my description
private static String[][] pageContentToArray(String url, int attr) throws Exception {
// getPageContent returns code like this [{"attribute":"value","attrubute":value},{"attr":"val","attr":val},...]
String[] pageContent = getPageContent(url).split(",");
//len is attributes amount multipled by objects amount
int len = pageContent.length;
String[][] splitedPageContent = new String[len][attr];
// len/attr returns number of objects
String[][] groupedPageContent = new String[len/attr][attr];
int savePoint = 0;
for (int i = 0; i = 0; --j) {
groupedPageContent[savePoint][k] = splitedPageContent[i-j][1];
++k;
}
++savePoint;
}
}
return groupedPageContent;
}This is the basic method that taking page content (described in the beginning). Splits it to array by "," sign then removes all special characters like "{}[] then making 2d array from them by splitting with ":" char. There is an algorithm works "parallely" that every nth step (whe
Solution
You're working too hard.
Although your post title says,
"Create Java object from page content or plain text",
actually it appears to be from JSON, specifically.
Since it's such a common format,
and it seems you're not actually trying to reinventing-the-wheel,
you'd be much better off using one of the existing libraries for the task.
For example Jackson seems popular.
Other than rewriting your program using Jackson or similar,
some big issues that stand out:
-
Catching
-
It seems you're overusing exceptions.
For example in
and in any case it looks like you should be handling unexpected data with conditionals, not with exceptions.
Although your post title says,
"Create Java object from page content or plain text",
actually it appears to be from JSON, specifically.
Since it's such a common format,
and it seems you're not actually trying to reinventing-the-wheel,
you'd be much better off using one of the existing libraries for the task.
For example Jackson seems popular.
Other than rewriting your program using Jackson or similar,
some big issues that stand out:
-
Catching
Exception: you should catch the most specific exception type that can be thrown, Exception is way to generic. It doesn't give a clue about what can go wrong with the code, and it can catch other exception types that are truly unexpected.-
It seems you're overusing exceptions.
For example in
pageToCompanies, it's not clear what can throw that exception you're catching,and in any case it looks like you should be handling unexpected data with conditionals, not with exceptions.
Context
StackExchange Code Review Q#91521, answer score: 5
Revisions (0)
No revisions yet.