I have the following class imp with inner class ScoringSummaryImplementation, when I tested the mean() method alone it works correctly, but when I test it as I show in the main method the answer goes incorrectly, I need to know the reason:
public class imp implements Normalizer {
public static List<BigDecimal> dataToBeNormalized = new ArrayList<>();
#Override
public ScoringSummary zscore(Path csvPath, Path destPath, String colToStandardize) {
ScoringSummary scoringObejct = new ScoringSummaryImplementation();
if (!Files.exists(csvPath)) {
throw new IllegalArgumentException("source file not found");
}
try {
readDataToBeNormalized(csvPath, destPath, colToStandardize);
} catch (IOException ex) {
Logger.getLogger(imp.class.getName()).log(Level.SEVERE, null, ex);
} catch (CsvValidationException ex) {
Logger.getLogger(imp.class.getName()).log(Level.SEVERE, null, ex);
}
//Z-score
for (int i = 0; i < dataToBeNormalized.size(); i++) {
BigDecimal sub = (dataToBeNormalized.get(i).subtract(scoringObejct.mean()));
try {
BigDecimal divide = sub.divide(scoringObejct.standardDeviation(), 2, RoundingMode.HALF_EVEN);
dataToBeNormalized.set(i, divide);
} catch (ArithmeticException e) {
}
}
return scoringObejct;
}
private void readDataToBeNormalized(Path csvPath, Path destPath, String colToStandardize) throws FileNotFoundException, IOException, CsvValidationException {
int indexOfTheCol = -1;
CSVReader reader = new CSVReader(new FileReader(csvPath.toString()));
String[] header = reader.readNext();
for (int i = 0; i < header.length; i++) {
if (header[i].equalsIgnoreCase(colToStandardize)) {
indexOfTheCol = i;
}
}
if (indexOfTheCol == -1) {
throw new IllegalArgumentException("column " + colToStandardize + " not found");
}
String[] values;
while ((values = reader.readNext()) != null) {
dataToBeNormalized.add(new BigDecimal(values[indexOfTheCol]));
}
}
//Inner class to implement ScoringSummary
public static class ScoringSummaryImplementation implements ScoringSummary {
#Override
public BigDecimal mean() {
BigDecimal sum = new BigDecimal("0");
BigDecimal sizeOfTheList = new BigDecimal(dataToBeNormalized.size());
for (int i = 0; i < dataToBeNormalized.size(); i++) {
sum = sum.add(dataToBeNormalized.get(i));
}
sum.divide(sizeOfTheList, 2, RoundingMode.HALF_EVEN);
return sum.divide(sizeOfTheList, 2, RoundingMode.HALF_EVEN);
}
}
}
// test in the main:
public class Test {
public static void main(String [] args){
Path p = Paths.get("C:\\Users\\DELL\\Documents\\GitHub\\J2EE\\demo\\src\\main\\java\\com\\mycompany\\demo\\d.csv");
Path p2 = Paths.get("C:\\Users\\DELL\\Documents\\GitHub\\J2EE\\demo\\src\\main\\java\\com\\mycompany\\demo\\des.csv");
Normalizer n1 = new imp ();
ScoringSummary n2 = n1.zscore(p, p2, "age");
BigDecimal mean = n2.mean();
System.out.println(mean);
}
}
Here is output:
0.82
Here is age column:
|age |
|5434|
|42423|
|54534|
|3333|
I'm storing Data in Map and Fetching the Field values now instead of Map I want to store data in Redis. I'm new to Redis and using below code to store in Redis :
public class CachingRedis {
private static HashMap<String, UserFields> Cache;
private static JedisPool pool = null;
private static final String redisHost = "localhost";
private static final Integer redisPort = 6379;
static Jedis jedis = null;
static User u;
public CachingRedis() {
pool = new JedisPool(redisHost, redisPort);
this.Cache = new HashMap<String, UserFields>();
}
public static void main(String[] args) throws ExecutionException {
CachingRedis gt = new CachingRedis();
gt.addSets();
}
private void addSets() {
InputStream in = ClassLoader.class.getResourceAsStream("/users.csv");
u = new User(in);
String[] consideredUserFields = { "Area","FirstName","LastName","Contact","Level"};
List<String[]> users = p.getUsers();
jedis = pool.getResource();
int count1 = 0;
String token = null;
String fieldName = null;
String fieldVal = null;
for (int i = 0; i < users.size(); i++) {
UserFields uFields = new UserFields();
String tmpId = Integer.toString(p.getUserId(i));
String[] tmpFields = Users.get(i);
for (int j = 0; j < tmpFields.length; j++) {
fieldName = consideredUsersFields[j];
fieldVal = tmpFields[j];
if (Cache != null && Cache.containsKey(tmpId)) {
uFields = Cache.get(tmpId);
uFields.setFieldKeyValues(fieldName, fieldVal);
Cache.put(tmpId, uFields);
**jedis.hsetnx( tmpId,fieldName,fieldVal );**
} else {
uFields.setFieldKeyValues(fieldName, fieldVal);
Cache.put(tmpId, pFields);
**jedis.hsetnx( tmpId,fieldName,fieldVal );**
}
}
}
} }
I'm getting the following error
Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: WRONGTYPE Operation against a key holding the wrong kind of value
at redis.clients.jedis.Protocol.processError(Protocol.java:117)
at redis.clients.jedis.Protocol.process(Protocol.java:142)
at redis.clients.jedis.Protocol.read(Protocol.java:196)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:288)
at redis.clients.jedis.Connection.getIntegerReply(Connection.java:213)
at redis.clients.jedis.Jedis.lpush(Jedis.java:814)
at net.cintell.CachingRedis.addSets(CachingRedis.java:85)
at net.cintell.CachingRedis.main(CachingRedis.java:48)
Can anyone tell where I am doing wrong? I want to store the csv file into redis and fetch respective fields?
public class CachingRedis {
private static HashMap<String, UserFields> Cache;
private static JedisPool pool = null;
private static final String redisHost = "localhost";
private static final Integer redisPort = 6379;
static Jedis jedis = null;
static User u;
public CachingRedis() {
pool = new JedisPool(redisHost, redisPort);
this.Cache = new HashMap<String, UserFields>();
}
public static void main(String[] args) throws ExecutionException {
CachingRedis gt = new CachingRedis();
gt.addSets();
}
private void addSets() {
InputStream in = ClassLoader.class.getResourceAsStream("/users.csv");
u = new User(in);
String[] consideredUserFields = { "Area","FirstName","LastName","Contact","Level"};
List<String[]> users = p.getUsers();
jedis = pool.getResource();
int count1 = 0;
String token = null;
String fieldName = null;
String fieldVal = null;
for (int i = 0; i < users.size(); i++) {
UserFields uFields = new UserFields();
String tmpId = Integer.toString(p.getUserId(i));
String[] tmpFields = Users.get(i);
for (int j = 0; j < tmpFields.length; j++) {
fieldName = consideredUsersFields[j];
fieldVal = tmpFields[j];
if (Cache != null && Cache.containsKey(tmpId)) {
uFields = Cache.get(tmpId);
uFields.setFieldKeyValues(fieldName, fieldVal);
Cache.put(tmpId, uFields);
} else {
uFields.setFieldKeyValues(fieldName, fieldVal);
Cache.put(tmpId, pFields);
}
}
}
Map<String, String> Properties = new HashMap<String, String>();
for (Map.Entry<String, PersonaFields> entry : Cache.entrySet()) {
Properties.put("Id", entry.getKey());
Properties.put("Area", entry.getValue()
//rest of the fields
jedis.hmset("Users"+ entry.getKey(), Properties);
}
} }
I have loaded entire cache map into redis by loading each key value into other map so that I can retrieve based on same key value
from redis
I'm working on my first java project and I have a question. The question should be quiet simple (though the the code is not that short, but there's no reason to be intimidated :) ). I create a basic roleplaying-game and I have an abstract class "Character" that defines each character. Among its subclasses you can find Mage, who has a Spellbook (Map). Spellbook class offers methods like addToSpellbook, that works fine. In addition I have an Inventory class that has addToInventory method, which is quit identical to addToSpellbook.
My question is as follows - why can I use In the main method addToSpellbook and can't use AddToInventory?
I guess the reason is that Map doesn't have AddToInventory, so I should override put, but still, how can I use addToSpellbook ?
public class Game {
public static void main(String[] args) throws IOException {
CharacterCreator heroCreator = new CharacterCreator();
CharacterCreator.showAllClasses();
Scanner sc = new Scanner(System.in);
int scan = sc.nextInt();
String chosenClass = CharacterCreator.getCharacterClass(scan);
Character hero = CharacterCreator.createCharacter(chosenClass);
try {
hero.displayCharacter();
}catch (Exception e){
System.out.println("Problem displaying character data");
}
hero.getInventory().addToInventory("Long sword");
CharacterCreator heroCreator2 = new CharacterCreator();
CharacterCreator.showAllClasses();
Scanner sc2 = new Scanner(System.in);
int scan2 = sc.nextInt();
String chosenClass2 = CharacterCreator.getCharacterClass(scan2);
Character hero2 = CharacterCreator.createCharacter(chosenClass2);
try {
hero2.displayCharacter();
}catch (Exception e){
System.out.println("Wrong input");
}
if(hero instanceof Mage) {
((Mage)hero).getSpellBook().addToSpellBook("Magic Missiles");
((Mage)hero).getSpellBook().addToSpellBook("Fireball");
((Mage)hero).getSpellBook().addToSpellBook("Mage Armor");
((Mage)hero).getSpellBook().showSpellBook();
((Mage)hero).getSpellBook().getSpellFromSpellbook("Fireball").castSpell(hero, hero2);
((Mage)hero).getSpellBook().getSpellFromSpellbook("Magic Missiles").castSpell(hero, hero2);
((Mage)hero).getSpellBook().getSpellFromSpellbook("Mage Armor").castSpell(hero, hero);
}
}
}
abstract public class Character {
private Equipment equipment;
private Map<String, Integer> inventory;
protected Character(String name){
equipment = new Equipment();
inventory = new HashMap<String, Integer>();
}
protected Character(String name, int lvl){
equipment = new Equipment();
inventory = new HashMap<String, Integer>();
}
}
public Equipment getEquipment() { return equipment; }
public Map getInventory() { return inventory; }
}
public class Inventory {
private Map<String,Integer> inventory;
Inventory() {
inventory = new HashMap<String, Integer>();
}
public void addToInventory(String item) {
boolean found = false;
try {
for (Iterator<Map.Entry<String, Integer>> iter = inventory.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, Integer> newItem = iter.next();
if (newItem.getKey() == item) {
inventory.put(item, inventory.get(newItem) + 1);
break;
}
}
}catch (Exception e) {
System.out.println(item + " : adding failed");
}
if (!found) {
inventory.put(item,1);
}
}
public void showInventory() {
System.out.println("Show Inventory: ");
for (Map.Entry<String,Integer> entry: inventory.entrySet()) {
System.out.println( entry.getKey() + ", quantity: " + entry.getValue() );
}
System.out.println("");
}
}
public class Mage extends Character {
private SpellBook spellBook;
public Mage(String name) {
super(name);
SpellBook spellbook = new SpellBook();
}
protected Mage(String name, int lvl){
super(name, lvl);
spellBook = new SpellBook();
}
public SpellBook getSpellBook() { return spellBook; }
}
}
public class SpellBook {
private Map<String, Spell> spellBook;
SpellBook() {
spellBook = new HashMap<String, Spell>();
}
public Map getSpellBook() { return spellBook; }
public void addToSpellBook(String spellName) {
Spell newSpell = null;
try {
if (DamageSpell.getSpell(spellName) != null) {
newSpell = DamageSpell.getSpell(spellName);
} else if (ChangeStatSpell.getSpell(spellName) != null) {
newSpell = ChangeStatSpell.getSpell(spellName);
}
System.out.println(newSpell.getSpellName() + " has been added to the spellbook");
spellBook.put(newSpell.getSpellName(), newSpell);
} catch (Exception e){
System.out.println("Adding " + spellName +"to spellbook has failed");
}
}
public void showSpellBook() {
System.out.println("Show spellbook: ");
for (Iterator<String> iter = spellBook.keySet().iterator(); iter.hasNext(); ) {
String spell = iter.next();
System.out.println(spell);
}
System.out.println("");
}
public Spell getSpellFromSpellbook(String spellName) {
Spell spl = null;
//Spell splGet = spellBook.get(spellName); /* straight forward implementation*/
// System.out.println("The spell " + splGet.getSpellName() + " has been retrived from the spellbook by using get method");
try {
for (Iterator<Map.Entry<String, Spell>> iter = spellBook.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, Spell> spell = iter.next();
if (spell.getKey() == spellName) {
spl = spell.getValue();
}
}
}catch (Exception e) {
System.out.println(spellName + " : no such spell in spellbook");
}
return spl;
}
}
getInventory() returns a Map and Map doesn't have the addToInventory() method.
getInventory() should add an Inventory instance.
I'm fairly new to JSON parsing in Java but when I try and parse this JSON String & find out it's "ID", it repeats the same one twice.
[
{"id":"{ID1}","time":123},
{"id":"{ID2}","time":124}
]
This is my Java code:
// v = json string, c = "id"
String output = v.replace("[", "").replace("]", "");
JSONObject obj = new JSONObject(output);
ArrayList<String> list = new ArrayList<String>();
for(int i = 0 ; i < obj.length(); i++){
System.out.println(obj.getString(c));
list.add(obj.getString(c));
}
return list.get(1);
it returns ID1 twice or more. Please help
Your JSON represents an array - so that's how you should parse it. You can then easily get the id property from each JSONObject within the array. For example:
import org.json.*;
public class Test {
public static void main(String[] args) throws JSONException {
String json =
"[{\"id\":\"{ID1}\",\"time\":123}, {\"id\":\"{ID2}\",\"time\":124}]";
JSONArray array = new JSONArray(json);
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
System.out.println(o.getString("id"));
}
}
}
Output:
{ID1}
{ID2}
I fixed my code by using it as a JSONArray(Thanks #HotLicks)
JSONArray obj = new JSONArray(v);
ArrayList<String> list = new ArrayList<String>();
for(int i = 0 ; i < obj.length(); i++){
Logger.WriteOutput(obj.getJSONObject(i).getString(c), Logger.LogLevel.Info);
}
Try this :
// This line is useless
// String output = v.replace("[", "").replace("]", "");
JSONArray arr = new JSONArray(output);
ArrayList<String> list = new ArrayList<String>();
for(int i = 0 ; i < arr.length(); i++){
System.out.println(arr.getJSONObject(i).getString(c));
list.add(arr.getJSONObject(i).getString(c));
}
First create a java bean for your json (for example here):
public class Item {
#JsonProperty("id")
private String id;
#JsonProperty("time")
private Integer time;
public final String getId() {
return id;
}
public final void setId(String id) {
this.id = id;
}
public final Integer getTime() {
return time;
}
public final void setTime(Integer time) {
this.time = time;
}
}
If you are using Jackson Java JSON-processor, you can create a List from JSON-String this way:
ObjectMapper objectMapper = new ObjectMapper();
try {
List<Item> items = objectMapper.readValue(
yourJSONString,
objectMapper.getTypeFactory().constructCollectionType(List.class, Item.class));
for (Item item : items) {
System.out.println(item.getId());
}
} catch (IOException e) {
e.printStackTrace();
}
use below code
String v = "[{\"id\":\"ID1\",\"time\":123},{\"id\":\"ID2\",\"time\":124}]";
String c = "id";
JSONArray obj = null;
try {
obj = new JSONArray(v);
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < obj.length(); i++) {
JSONObject j = (JSONObject) obj.get(i);
System.out.println(j.getString(c));
list.add(j.getString(c));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
note that i have slightly corrected the json structure too
before
[
{"id":"{ID1}","time":123},
{"id":"{ID2}","time":124}
]
after
[
{"id":"ID1","time":123},
{"id":"ID2","time":124}
]
have a string like;
{uniqueKey=test20745,content={acostumbrado={tf=1,df=1},al={tf=1,df=6945},ante={tf=1,df=42},co={tf=1,df=9187},ello={tf=1,df=2},está={tf=2,df=21},falcao={tf=1,df=105},guardia={tf=1,df=2},http={tf=1,df=9893},nada={tf=1,df=24},no={tf=1,df=2493},osa={tf=1,df=429},para={tf=1,df=6382},partido={tf=1,df=50},pretorian={tf=1,df=1},que={tf=1,df=358},sebcfkeı={tf=1,df=1},su={tf=1,df=7368},t={tf=1,df=14423},tuvo={tf=1,df=3},un={tf=1,df=8511}}}
how to parse this string to a map(key, list) like below;
acostumbrado->tf=1,df=1
al ->tf=1,df=6945
ante ->tf=1,df=42
...
edit:
so what I have done up to now;
String delims = "[{},]";
HashMap<String, List<String>> valueMap = new HashMap<String, List<String>>();
for (int i = 0; i < text.size(); i++) {
String[] tokens = val.toString().split(delims);
ArrayList<String> tfAndDfValues = new ArrayList<String>();
tfAndDfValues.add(tokens[4].substring(3));
tfAndDfValues.add(tokens[5].substring(3));
if (valueMap.containsKey(removeLastChar(tokens[3]))) {
valueMap.get(removeLastChar(tokens[3])).addAll(tfAndDfValues);
} else {
valueMap.put(removeLastChar(tokens[3]), tfAndDfValues);
}
}
Have you thought about State pattern? See usage example:
public class StatePatternProgram {
public static void main(String[] args) {
String example = "{uniqueKey=test20745,content={acostumbrado={tf=1,df=1},al={tf=1,df=6945},ante={tf=1,df=42},co={tf=1,df=9187},ello={tf=1,df=2},está={tf=2,df=21},falcao={tf=1,df=105},guardia={tf=1,df=2},http={tf=1,df=9893},nada={tf=1,df=24},no={tf=1,df=2493},osa={tf=1,df=429},para={tf=1,df=6382},partido={tf=1,df=50},pretorian={tf=1,df=1},que={tf=1,df=358},sebcfkeı={tf=1,df=1},su={tf=1,df=7368},t={tf=1,df=14423},tuvo={tf=1,df=3},un={tf=1,df=8511}}}";
ParseContext context = new ParseContext();
Map<String, List<Integer>> map = context.parseContent(example);
System.out.println(map);
}
}
class ParseContext {
private Map<String, List<Integer>> contentMap;
private String currentKey;
private List<Integer> currentList;
private boolean continueParsing = true;
private ParseState state = new StartParseState();
public Map<String, List<Integer>> parseContent(String content) {
StringTokenizer tokenizer = new StringTokenizer(content, "{}=,", true);
while (continueParsing) {
state.parse(tokenizer);
}
return contentMap;
}
interface ParseState {
void parse(StringTokenizer tokenizer);
}
class StartParseState implements ParseState {
#Override
public void parse(StringTokenizer tokenizer) {
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if (!"content".equals(token)) {
continue;
}
contentMap = new LinkedHashMap<String, List<Integer>>();
tokenizer.nextToken();
tokenizer.nextToken();
state = new KeyParseState();
break;
}
}
}
class KeyParseState implements ParseState {
#Override
public void parse(StringTokenizer tokenizer) {
if (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if (",".equals(token)) {
token = tokenizer.nextToken();
}
if ("}".equals(token)) {
state = new EndParseState();
return;
}
currentKey = token;
tokenizer.nextToken();
state = new ListParseState();
}
}
}
class ListParseState implements ParseState {
#Override
public void parse(StringTokenizer tokenizer) {
currentList = new ArrayList<Integer>();
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if ("}".equals(token)) {
break;
}
if ("=".equals(token)) {
currentList.add(Integer.valueOf(tokenizer.nextToken()));
}
}
contentMap.put(currentKey, currentList);
state = new KeyParseState();
}
}
class EndParseState implements ParseState {
#Override
public void parse(StringTokenizer tokenizer) {
continueParsing = false;
}
}
}
This program prints:
{acostumbrado=[1, 1], al=[1, 6945], ante=[1, 42], co=[1, 9187], ello=[1, 2], está=[2, 21], falcao=[1, 105], guardia=[1, 2], http=[1, 9893], nada=[1, 24], no=[1, 2493], osa=[1, 429], para=[1, 6382], partido=[1, 50], pretorian=[1, 1], que=[1, 358], sebcfkeı=[1, 1], su=[1, 7368], t=[1, 14423], tuvo=[1, 3], un=[1, 8511]}
This solution is using regex group capturing:
private static final String SAMPLE = "{uniqueKey=test20745,content={acostumbrado={tf=1,df=1},al={tf=1,df=6945},ante={tf=1,df=42},co={tf=1,df=9187},ello={tf=1,df=2},está={tf=2,df=21},falcao={tf=1,df=105},guardia={tf=1,df=2},http={tf=1,df=9893},nada={tf=1,df=24},no={tf=1,df=2493},osa={tf=1,df=429},para={tf=1,df=6382},partido={tf=1,df=50},pretorian={tf=1,df=1},que={tf=1,df=358},sebcfkeı={tf=1,df=1},su={tf=1,df=7368},t={tf=1,df=14423},tuvo={tf=1,df=3},un={tf=1,df=8511}}}";
private static final String CONTENT = "(\\p{L}*)=\\{((tf=\\d*),(df=\\d*))\\}";
public static void main(String[] args) {
Pattern p = Pattern.compile(CONTENT);
Matcher m = p.matcher(SAMPLE);
Map<String, List<String>> result = new HashMap<String, List<String>>();
while (m.find()) {
String key = m.group(1);
List<String> values = new ArrayList<String>();
values.add(m.group(3));
values.add(m.group(4));
result.put(key, values);
}
System.out.println(result);
}
If your input pattern is unique all the time, try this
String delims = "[{},=]";
Map map = new HashMap() ;
String[] tokens = text.toString().split(delims);
for( int i=5;i<tokens.length - 7;i = i+ 7) {
map.put(tokens[i], tokens[i+2]+"->"+tokens[i+3]+","+tokens[i+4]+"->"+tokens[i+5]);
}
System.out.println(map);