I have to update a table with two columns and I have created a class
public class Country {
private String url;
private String search;
public Country(String url, String search) {
this.url = url;
this.search = search;
}
// ...
}
List<Country> countries = new ArrayList<Country>();
countries.add(new Country(urls, txt));
...
Countries has a data {java.com.main#yfxse34567}
Could be {www.google.com, main string...}
How can I put a proper data into countries list
Override the toString() method
Example
List<Country> countries = new ArrayList<Country>() {
#Override
public String toString() {
String result = "{";
for (int index= 0; index < size(); index++){
result = result.concat(this.get(index).url);
if (index != size()-1) {
result = result.concat(", ");
}
}
result = result.concat("}");
return result;
}
};
For me your solution should work, however try to instantiate the parent first and then add the instance ... Something like
Country instanceCountry = new Country();
instanceCountry.setUrl("www.google.com");
instanceCountry.setSearch("xpto");
countries.add(instanceCountry);
Do not forget to generate the Getters and Setters
Related
For a college assignment I need to create an app that retrieves product data from the API of a well known Dutch online store. I need to store the title, summary, price and image URLs of each product into a new Product object. These Products are stored into an ArrayList and the ArrayList is then returned.
Each product within the products array has a nested array called "images", which contains 6 product images. These images need to stored into my Product object's HashMap attribute, with the image size as key and the URL as value. However, I can't seem to get it right.
JSON data with the query "pokemon": https://api.bol.com/catalog/v4/search/?apikey=25C4742A92BF468EB2BD888FC8FBFF40&format=json&q=pokemon
Product class:
package com.example.bolcombrowser.domain;
import java.util.Map;
public class Product {
// Attributes
private String mTitle;
private String mSummary;
private double mPrice;
private Map < String, String > mImageUrls;
// Constructor
public Product(String mTitle, String mSummary, double mPrice, Map < String, String > mImageUrls) {
this.mTitle = mTitle;
this.mSummary = mSummary;
this.mPrice = mPrice;
this.mImageUrls = mImageUrls;
}
// Getters and Setters
public String getmTitle() {
return mTitle;
}
public void setmTitle(String mTitle) {
this.mTitle = mTitle;
}
public String getmSummary() {
return mSummary;
}
public void setmSummary(String mSummary) {
this.mSummary = mSummary;
}
public double getmPrice() {
return mPrice;
}
public void setmPrice(double mPrice) {
this.mPrice = mPrice;
}
public Map < String, String > getImageUrls() {
return mImageUrls;
}
public void setImageUrls(Map < String, String > imageUrls) {
this.mImageUrls = imageUrls;
}
}
parseJson method:
public static ArrayList < Product > parseJson(String productJsonStr) throws JSONException {
/* JSON array names. */
final String BOL_PRODUCTS = "products";
final String BOL_IMAGES = "images";
final String BOL_OFFERS = "offers";
/* JSON key names. */
final String BOL_TITLE = "title";
final String BOL_SUMMARY = "summary";
final String BOL_OFFERDATA = "offerData";
final String BOL_PRICE = "price";
final String BOL_KEY = "key";
final String BOL_URL = "url";
/* Variables to store product data into, and is then used to create new Product objects. */
String title;
String summary;
double price;
Map < String, String > imageUrls = new HashMap < > ();
/* ArrayList to store products into. */
ArrayList < Product > productList = new ArrayList < > ();
JSONObject productsJson = new JSONObject(productJsonStr);
JSONArray productsArray = productsJson.getJSONArray(BOL_PRODUCTS);
for (int i = 0; i < productsArray.length(); i++) {
JSONObject product = productsArray.getJSONObject(i);
/* Retrieve the title and summary of each product. */
title = product.getString(BOL_TITLE);
summary = product.getString(BOL_SUMMARY);
JSONArray imagesArray = product.getJSONArray(BOL_IMAGES);
for (int j = 0; j < imagesArray.length(); j++) {
JSONObject image = imagesArray.getJSONObject(j);
/* Retrieve each product's image sizes and URLs and store them into a HashMap. */
String imageSize = image.getString(BOL_KEY);
String imageUrl = image.getString(BOL_URL);
imageUrls.put(imageSize, imageUrl);
}
JSONObject offerData = product.getJSONObject(BOL_OFFERDATA);
JSONArray offers = offerData.getJSONArray(BOL_OFFERS);
JSONObject offer = offers.getJSONObject(0);
price = offer.getDouble(BOL_PRICE);
productList.add(new Product(title, summary, price, imageUrls));
}
return productList;
}
onPostExecute method:
#Override
protected void onPostExecute(String productData) {
if (productData != null) {
ArrayList < Product > productList;
try {
productList = JsonUtils.parseJson(productData);
for (Product product: productList) {
String title = product.getmTitle();
String summary = product.getmSummary();
double price = product.getmPrice();
String hashMap = product.getImageUrls().toString();
mTextViewOutput.append(title + "\n\n" + summary + "\n\n" + price + "\n\n" +
hashMap + "\n\n\n\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
When I test out my app, it seems to have stored the image URLs of the last product into every product's HashMap:
I've been staring at my code for hours and I can't seem to find out why it does this. I'm probably making a very silly mistake but I just can't seem to figure out what it is exactly.
Your Map<String, String> imageUrls = new HashMap<>(); is in the wrong place. It should be inside your first for loop, otherwise you are using the same Map for all your products.
...
for (int i = 0; i < productsArray.length(); i++) {
Map<String, String> imageUrls = new HashMap<>();
...
By the way, I suggest using gson library. It will make your code less boilerplate
I need to filter elements and then sort based on certain column. Post that I would need to find the unique entries based on combination of columns. Since it is file processing, pipe(|) is used as delimiter to denote the column value.
String s1= "12|Thor|Asgaurd|1000000|Avenger|Active"
String s2= "234|Iron man|New York|9999999|Avenger|Active"
String s3= "420|Loki|Asgaurd|||Inactive"
String s4= "12|Thor|Asgaurd Bank|1000000|Avenger HQ|Active"
Data first needs to be filtered based on the Active/Inactive status. Then it needs to be sorted based on 4th column. Lastly, the uniqueness needs to be maintained by combining column 1,2,3.
Expected Output =
"234|Iron man|New York|9999999|Avenger|Active"
"12|Thor|Asgaurd|1000000|Avenger|Active"
Creating a model class and parsing the string is the way to go, but if for some reaseon you don't want to do that you can do it this way:
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
List<String> result = Stream.of(s1, s2, s3, s4)
.filter(s -> s.split("\\|")[5].equals("Active"))
.sorted(Comparator.comparing(e -> e.split("\\|")[4]))
.collect(Collectors.toList());
First of all you should create an Object which represents your String data. Something like this:
public class MyObject {
private int id;
private String name;
private String location;
private Integer value;
private String category;
private String state;
public MyObject(String entry) {
String[] parts = entry.split("\\|");
if (parts.length != 6) {
throw new IllegalArgumentException("entry has not 6 parts");
}
id = Integer.parseInt(parts[0]);
name = parts[1];
location = parts[2];
try {
value = Integer.parseInt(parts[3]);
} catch (NumberFormatException ignored) {
}
category = parts[4];
state = parts[5];
}
// getters
#Override
public String toString() {
return String.join("|", String.valueOf(id), name, location, String.valueOf(value), category, state);
}
}
With this you can create a Stream of objects from your Strings and to the filter, sort and distinct operations afterwards:
Collection<MyObject> result = Stream.of(s1, s2, s3, s4)
.map(MyObject::new)
.filter(o -> "Active".equals(o.getState()))
.sorted(Comparator.comparing(MyObject::getValue).reversed())
.collect(Collectors.toMap(o -> Arrays.asList(o.getId(), o.getName()),
Function.identity(), (o1, o2) -> o1, LinkedHashMap::new))
.values();
result.forEach(System.out::println);
After the map operation you filter the values by state and sort them by column 4 (value in my case). At the end you collect all the values in a map for the distinct operation. Add all values you need distinction for to the Arrays.asList(). As values the map takes all the original values (Function.identity()). For duplicates we keep the first value ((o1, o2) -> o1) and we are using a LinkedHashMap to keep the order of the items. At the end wee use only the values of the map.
If you need a List instead of a Collection use new ArrayList(result).
The result will be this:
234|Iron man|New York|9999999|Avenger|Active
12|Thor|Asgaurd|1000000|Avenger|Active
It seems like you're unable to filter while everything is string only.
Try this,
create a new model class which can hold your columns.
Ex:
class MyData{
private String name;
private String city;
private String distance;
private String organization;
private String status;
//And create Getter Setter method for all above fields.
}
Now came to your main class where you can play with your code stuff.
Map<MyData> map = new HashMap<MyData>();
MyData myData = new MyData();
myData.setName("Thor");
myData.setCity("Asgaurd");
myData.setDistance("1000000");
myData.setOrganization("Avenger");
myData.setStatus("Active");
map.put(12, myData);
//Same thing for all other data (note: use the loop for data insertion in map)
Map<String, MyData> sorted = map.entrySet().stream().sorted(comparingByValue()).collect(toMap(e -> e.getKey(), e -> e.getValue().getName(), (e1, e2) -> e2,LinkedHashMap::new));
System.out.println("map after sorting by values: " + sorted);
You can solve your task this way:
Firstly, just create POJO(Plain Old Java Object) and override the toString() method.
class MarvelPerson {
private Integer id;
private String name;
private String origin;
private Integer point = null;
private String faction;
private String status;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public Integer getPoint() {
return point;
}
public void setPoint(Integer point) {
this.point = point;
}
public String getFaction() {
return faction;
}
public void setFaction(String faction) {
this.faction = faction;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
#Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(id);
builder.append("|");
builder.append(name);
builder.append("|");
builder.append(origin);
builder.append("|");
if(point != null) {
builder.append(point);
}
builder.append("|");
if(faction != null) {
builder.append(faction);
}
builder.append("|");
builder.append(status);
return builder.toString();
}
}
Then, you should write the parser from string to MarvelPerson. Side note: Carefully, my implementation is pretty basic, and I suppose it should be modified because I may not have foreseen some corner cases.
class PersonParser {
static MarvelPerson parse(String data) {
MarvelPerson person = new MarvelPerson();
String[] array = data.split("\\|", -1);
person.setId(Integer.parseInt(array[0]));
person.setName(array[1]);
person.setOrigin(array[2]);
if(!array[3].isEmpty()) {
person.setPoint(Integer.parseInt(array[3]));
}
if(!array[4].isEmpty()) {
person.setFaction(array[4]);
}
person.setStatus(array[5]);
return person;
}
}
And then your solution:
public class Test {
public static void main(String[] args) {
List<MarvelPerson> list = new ArrayList<>();
list.add(PersonParser.parse("12|Thor|Asgaurd|1000000|Avenger|Active"));
list.add(PersonParser.parse("234|Iron man|New York|9999999|Avenger|Active"));
list.add(PersonParser.parse("420|Loki|Asgaurd|||Inactive"));
list.add(PersonParser.parse("12|Thor|Asgaurd Bank|1000000|Avenger HQ|Actie"));
list.stream()
.filter(marvelPerson -> marvelPerson.getStatus().equals("Active"))
.sorted((o1, o2) -> o1.getPoint() <= o2.getPoint() ? 1 : -1)
.forEach(marvelPerson -> {
System.out.println(marvelPerson.toString());
});
}
}
The output to be printed:
234|Iron man|New York|9999999|Avenger|Active
12|Thor|Asgaurd|1000000|Avenger|Active
We are trying to access the array of object that placed inside a map.
Can any one guide us to get length of the array as well as fetching each element from the list. Sample map object given bellow.
{
storeId = 1,
ShipToStreetLine1 = test 123,
ShipToCity = Seattle,
OrderDetails = [{
distributor_name = SS,
product_name = HORN 80897 AM GUN 300BO 125 HP 50/10
}]
}
We need to get the size of orderDetails array and if data present, then I want to fetch product_name.
You can try this:
Create a POJO which is type of what you are getting in orderDetails
Like
public class OrderDetailElement{
private String distributor_name;
private String product_name;
public String getDistributor_name() {
return distributor_name;
}
public void setDistributor_name(String distributor_name) {
this.distributor_name = distributor_name;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
}
in your logic class you can do is
ArrayList<OrderDetailElement> orderDetails = yourMap.get("OrderDetails");
List<String> products = new ArrayList<String>();
if (orderDetails.size() > 0) {
for (OrderDetailElement orderDetailElement : orderDetails) {
products.add(orderDetailElement.getProduct_name());
}
}
I have collection table in database which have fields:
published,name,descrption and collection_type.
And this collection_type can be three different String values :Collection,Trend,Occasion.
Showing whole elements with this working.
public List<Collection> list() {
QueryParams queryParams = new QueryParams();
queryParams.setWhere("published = true");
return list(queryParams);
}
But showing specific elements for example Occasions failed. How to fix the code to show the elements?
public List<Collection> occasions() {
QueryParams queryParams = new QueryParams();
final StringBuilder sb = new StringBuilder();
sb.append("published = true ");
sb.append("AND collection_type = '");
sb.append("CollectionType.OCCASION.getName()'");
queryParams.setWhere(sb.toString());
return list(queryParams);
}
public enum CollectionType {
COLLECTION("COLLECTION"), TREND("TREND"), OCCASION("OCCASION");
private String name;
private CollectionType(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Suppose, the problem is, that you don't set a ENUM's name, but a "CollectionType.OCCASION.getName()'" string. Try to change your code to:
public List<Collection> occasions() {
QueryParams queryParams = new QueryParams();
final StringBuilder sb = new StringBuilder();
sb.append("published = true ");
sb.append("AND collection_type = '");
sb.append(CollectionType.OCCASION.getName());
sb.append("'");
queryParams.setWhere(sb.toString());
return list(queryParams);
}
I'm making a mysql database connector with java to show all the data.
When I run the code, I get an NullPointerException in my getData() function.
here is my code.
public String[][] getData() {
String values[][];
try {
rs = st.executeQuery("SELECT * FROM adresses");
int i = 0;
while(rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String adress = rs.getString("email_adress");
String catagory = rs.getString("catarogy");
values[i][0] = id;
values[i][1] = name;
values[i][2] = adress;
values[i][3] = catagory;
i++;
}
return values;
} catch (SQLException e) {
e.printStackTrace();
return values;
}
}
When the value of the String values is nothing I get The error. But if I give the String allready a value it says nothing .
public String[][] getData() {
String values[][] = {{"","","",""},
{"","","",""},
{"","","",""},};
try {
rs = st.executeQuery("SELECT * FROM adresses");
int i = 0;
while(rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String adress = rs.getString("email_adress");
String catagory = rs.getString("catarogy");
values[i][0] = id;
values[i][1] = name;
values[i][2] = adress;
values[i][3] = catagory;
i++;
}
return values;
} catch (SQLException e) {
e.printStackTrace();
return values;
}
}
I want more data than that in my data String. how can I let it automatically do that??
Tnx.
PS.
The function is called in my class FrameGUI and has to change to Object
public class FrameGUI extends JFrame {
public JTable dataHolder;
Mysql mysql = new Mysql();
public String[] columnNames = {
"ID", "Name", "Adress", "Catagory"
};
-> public Object[][] data = mysql.getData();
public FrameGUI() {
init();
mysql.getData();
}
}
You do not initialize String values[][] so it is null. You either need to initialize it first or use a more appropriate datastructure like a List.
You should define a class and use a List (e.g. the ArrayList) instead.
e.g. if you want to call it User -
public class User {
private String id;
private String name;
//...
}
and a list
List<User> users = new ArrayList<User>();
and then instantiate the User class for each row and add the new instance to the list -
User currUser = new User();
users.add(currUser);
//set values from result set
The list can grow automatically when needed and the code is much more readable than using the array.
You get an index out of bounds in the first example because a String[][] (or String Matrix) gets initialized as a zero-length array.
In the second instance, you initialized the array to a size of 3x4 - that works so long as you only get 3 results back.
What you really need is a data structure with a dynamic size. Arrays aren't automatically sized dynamically. Try using a collection implementation like ArrayList or LinkedList or Vector.
Also, instead of saving your values to a String[], try creating a bean class that can hold your result. Create a new instance of it for each result that you get back instead of initializing a new array.
Because you didn't initialized your array, that is why you get NPE. Actually I suggest you to use List for your purposes:
public ArrayList<ArrayList<String>> getData() {
ArrayList<ArrayList<String>> values = new ArrayList<>();
try {
rs = st.executeQuery("SELECT * FROM adresses");
while(rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String adress = rs.getString("email_adress");
String catagory = rs.getString("catarogy");
ArrayList<String> list = new ArrayList<>();
list.add(id);
list.add(name);
list.add(adress);
list.add(catagory);
values.add(list);
}
return values;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
The main problem in you code is you are using arrays to save variable number of data. Arrays is fixed sized after they are created so you can't add (or remove) elements to them dynamically.
Instead of using arrays you should use an ArrayList object which have methods to add more elements. Also instead of creating a multidimensional array it looks like a better idea to create a class for the data you get from you database.
So lets first create a Address class:
public class Address {
public String id, name, adress, catagory;
public Address(String id, String name, String adress, String catagory) {
this.id = id;
this.name = name;
this.adress = adress;
this.catagory = catagory;
}
}
Now you can write you code as:
public List<Address> getData() {
List<Address> values = new ArrayList<Address>();
try {
rs = st.executeQuery("SELECT * FROM adresses");
int i = 0;
while(rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String adress = rs.getString("email_adress");
String catagory = rs.getString("catarogy");
values.add(new Address(id, name, adress, catagory));
}
return values;
} catch (SQLException e) {
e.printStackTrace();
return values;
}
}
The returned list will contain a list of Address objects which have the values from you database. Also, the size of the list is always the same as the content you put into it.