best practice of compare a map of metadata with an object? - java

I have a map of metadata and Expression class:
Map<String , Object> metaDate;
public class Expression{
private List<UserCondition> conditions;
private LogicalOperationEnum; // and , or
getters/stters...
}
public classUserCondition{
private String fieldName;
private MathOperationEnum operation; // > , < , >= , <= , =
private Object value;
getters/setters...
}
Now I hav a method this method in my businessService class:
private boolean evaluate(Map<String, Object> userMetaData, Expression expression) {
if (expression.getLogicalOperation() == LogicalOperationEnum.and) {
for (UserCondition c : expression.getConditions()) {
if (c.getOperation().getName() == ">" && String.valueOf(userMetaData.get(c.getField())).compareTo(String.valueOf(c.getValue())) <= 0) {
return false;
} else if (c.getOperation().getName() == "<" && String.valueOf(userMetaData.get(c.getField())).compareTo(String.valueOf(c.getValue())) >= 0) {
return false;
} else if (c.getOperation().getName() == "<=" && String.valueOf(userMetaData.get(c.getField())).compareTo(String.valueOf(c.getValue())) > 0) {
return false;
} else if (c.getOperation().getName() == ">=" && String.valueOf(userMetaData.get(c.getField())).compareTo(String.valueOf(c.getValue())) < 0) {
return false;
} else{
return true;
}
}
} else if (expression.getLogicalOperation() == LogicalOperationEnum.or) {
for (UserCondition c : expression.getConditions()) {
if (c.getOperation().getName() == ">" && String.valueOf(userMetaData.get(c.getField())).compareTo(String.valueOf(c.getValue())) > 0) {
return true;
} else if (c.getOperation().getName() == "<" && String.valueOf(userMetaData.get(c.getField())).compareTo(String.valueOf(c.getValue())) < 0) {
return true;
} else if (c.getOperation().getName() == "<=" && String.valueOf(userMetaData.get(c.getField())).compareTo(String.valueOf(c.getValue())) <= 0) {
return true;
} else if (c.getOperation().getName() == ">=" && String.valueOf(userMetaData.get(c.getField())).compareTo(String.valueOf(c.getValue())) >= 0) {
return true;
}
}
}
return false;
}
Now i think that this method can implement better than , but i don't know how can i do that?

Related

Java Balanced String not using stack

I need to check if given String is balanced or not.
This is similar task like that one with balanced parenthesis.
Please let me know if this is understand for you.
try this
public class CheckBalance {
public static void main(String[] args) {
System.out.println(check("asdfgh()hgfdsa"));
}
static boolean isClosing(char first, char sec)
{
if (first == '(' && sec == ')') {
return true;
} else if (first == '{' && sec == '}' ) {
return true;
} else if (first == '[' && sec == ']') {
return true;
}//add other the different Chars
else if(first == sec){
return true;
}
return false;
}
static boolean check(String value) {
if (value != null) {
char[] valueAsArray = value.toCharArray();
int size = valueAsArray.length;
if (size == 0) {
return true;
} else if (size == 1) {
return true;
} else {
for (int i = 0; i < size; i++) {
if (!isClosing(valueAsArray[i], valueAsArray[size - (i+1)])){
return false;
}
if (i == (size-1)/2) {
break;
}
}
return true;
}
} else {
return true;
}
}
}
public boolean isBalanced(String s) {
return isBalanced(s, 0, new LinkedList<>());
}
private boolean isBalanced(String s, int index, LinkedList<Character> stack) {
if (index >= s.length())
return stack.isEmpty();
char c = s.charAt(index);
if (!stack.isEmpty() && Character.compare(c, stack.getLast()) == 0) {
stack.removeLast();
if (isBalanced(s, index + 1, stack))
return true;
stack.add(c);
}
stack.add(c);
return isBalanced(s, index + 1, stack);
}
ABAB is wrong right ? it is the same with parenthesis ?
public boolean isBalanceString(String input)
{
int firstIndex =0;
int lastIndex = input.length()-1;
while (firstIndex<lastIndex)
{
if(input.charAt(firstIndex) != input.charAt(lastIndex))
{
return false;
}
firstIndex++;
lastIndex--;
}
return true;
}

Reverse loop TicTacToe

I've got loop:
for (int i = 0; i<3; i++){
System.out.println(i);
}
output:
0
1
2
I need to reverse this loop to output:
2
1
0
I need it because I work on TicTacToe Java game.
Here is my code for check win:
static boolean checkWin(char dot) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[0][i] == dot && i == 2) {
return true;
}
if (map[1][i] == dot && i == 2) {
return true;
}
if (map[2][i] == dot && i == 2) {
return true;
}
if (map[i][0] == dot && i == 2) {
return true;
}
if (map[i][1] == dot && i == 2) {
return true;
}
if (map[i][2] == dot && i == 2) {
return true;
}
if (map[i][i] == dot && i == 2) {
return true;
}
if (map[i][0] == dot && i == 2) {
return true;
}
}
}
return false;
}
last thing that I need to refactor this method:
static boolean checkWin(char dot) {
if (map[0][0] == dot && map[0][1] == dot && map[0][2] == dot) {
return true;
}
if (map[1][0] == dot && map[1][1] == dot && map[1][2] == dot) {
return true;
}
if (map[2][0] == dot && map[2][1] == dot && map[2][2] == dot) {
return true;
}
if (map[0][0] == dot && map[1][0] == dot && map[2][0] == dot) {
return true;
}
if (map[0][1] == dot && map[1][1] == dot && map[2][1] == dot) {
return true;
}
if (map[0][2] == dot && map[1][2] == dot && map[2][2] == dot) {
return true;
}
if (map[0][0] == dot && map[1][1] == dot && map[2][2] == dot) {
return true;
}
if (map[0][2] == dot && map[1][1] == dot && map[2][0] == dot) {
return true;
}
return false;
}
}
I almost done it.
Last thing that i need refactor this part of code:
if (map[0][2] == dot && map[1][1] == dot && map[2][0] == dot) {
return true;
}
to something like
if (map[i][0] == dot && i == 2) {
return true;
}
Main question is here:
for (int i = 0; i<3; i++){
System.out.println(i);
}
output:
0
1
2
I need to reverse this loop to output:
2
1
0
to feel this last part:
if (map[i][0] == dot && i == 2) {
return true;
}
i need to feel this last condition with values 2 1 0
if (map[i][insert here] == dot && i == 2) {
return true;
}
Please Help.
If all you want to do is reverse the loop output, you can just invert what you're doing in the for loop
for (int i = 2; i>=0; i--){
System.out.println(i);
}
output:
2
1
0
As a quick example.

Why won't an IF statement work with rounded floats and doubles?

I am trying to check if a user has inputted a double between 3.5 and 7. I have tried to use the actual double and Math.round();, as well as StrictMath.round(). I have also tried to parse the string inputted as a float but it didn't change anything. Here is the basic code I used:
import java.util.Scanner;
public class IfStatement {
public static void main(String[] args) {
//create a Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter first double: ");
double number = input.nextDouble();
if (isDouble(number)==true) {
double x = Double.parseDouble(number);
if (3.5 <= x <= 7) {
System.out.println("good");
} else {
System.out.println("incorrect input");
}
} else {
System.out.println("incorroct input");
}
}
public static booleen isDouble(String str) {
if (str == null) {
return false;
}
int length = str.length();
if (length == 0) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) {
return false;
}
++i;
}
int integerPartSize = 0;
int exponentPartSize = -1;
while (i < length) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
if (c == '.' && integerPartSize > 0 && exponentPartSize == -1) {
exponentPartSize = 0;
} else {
return false;
}
} else if (exponentPartSize > -1) {
++exponentPartSize;
} else {
++integerPartSize;
}
++i;
}
if ((str.charAt(0) == '0' && i > 1 && exponentPartSize < 1)
|| exponentPartSize == 0 || (str.charAt(length - 1) == '.')) {
return false;
}
return true;
}
}
I have also tried making 3.5 3 in the if statement but got no dice. I just need a loose explanation and not a full solution.
I've edited your code.
boolean spelling
isDouble() takes a String
a <= x <= b chained expressions are not allowed in Java
Also,
if ((str.charAt(0) == '0' && i > 1 && exponentPartSize < 1) || exponentPartSize == 0 || (str.charAt(length - 1) == '.')) {
return false;
}
return true;
can be simplified to
return (str.charAt(0) != '0' || i <= 1 || exponentPartSize >= 1)
&& exponentPartSize != 0 && (str.charAt(length - 1) != '.');
Here's the full code.
class Test {
public static boolean isDouble(String str) {
if (str == null) {
return false;
}
int length = str.length();
if (length == 0) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) {
return false;
}
++i;
}
int integerPartSize = 0;
int exponentPartSize = -1;
while (i < length) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
if (c == '.' && integerPartSize > 0 && exponentPartSize == -1) {
exponentPartSize = 0;
} else {
return false;
}
} else if (exponentPartSize > -1) {
++exponentPartSize;
} else {
++integerPartSize;
}
++i;
}
return (str.charAt(0) != '0' || i <= 1 || exponentPartSize >= 1)
&& exponentPartSize != 0 && (str.charAt(length - 1) != '.');
}
public static void main(String[] args) {
//create a Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter first double: ");
String number = input.nextLine();
if (isDouble(number)) {
double x = Double.parseDouble(number);
if (3.5 <= x && x <= 7) {
System.out.println("good");
} else {
System.out.println("incorrect input");
}
} else {
System.out.println("incorroct input");
}
}
}

Combination Lock assignment in Java

I have an assignment where there is a combination lock box that has a 4-letter word as the combination, and a player has to try to guess the combination by guessing the word one letter at a time. For each round, a clue should be outputted to show how close a person's guess is to the actual word. If the letter in the guess is also in the same position in the combination lock, then the matching letter is outputted. If the letter is in the word but not in the same position, a + is outputted, and when the letter is not present in the word at all, an * is outputted. All of those characters are added to a single string to show the result.
I've tried to code all of this in Java, but it returns an incorrect result with a null before it. Can someone help me fix these issues?
public class CombinationLock
{
private String lock;
private String guess;
private String guessLetter1;
private String guessLetter2;
private String guessLetter3;
private String guessLetter4;
private String lockLetter1;
private String lockLetter2;
private String lockLetter3;
private String lockLetter4;
private String modifiedGuess;
private char combinationLetter;
private char guessLetter;
public CombinationLock(String newLock)
{
lock = newLock;
}
public String getClue(String newGuess)
{
guess = newGuess;
for(int i = 0; i < lock.length(); i++)
{
combinationLetter = lock.charAt(i);
guessLetter = guess.charAt(i);
if(i == 0)
{
lockLetter1 += combinationLetter;
guessLetter1 += guessLetter;
}
else if(i == 1)
{
lockLetter2 += combinationLetter;
guessLetter2 += guessLetter;
}
else if(i == 2)
{
lockLetter3 += combinationLetter;
guessLetter3 += guessLetter;
}
else if(i == 3)
{
lockLetter4 += combinationLetter;
guessLetter4 += guessLetter;
}
if(combinationLetter == guessLetter)
{
modifiedGuess += combinationLetter;
}
else if(guessLetter1 == lockLetter1 || guessLetter1 == lockLetter2 || guessLetter1 == lockLetter3 || guessLetter1 == lockLetter4)
{
modifiedGuess += '+';
}
else if(guessLetter2 == lockLetter1 || guessLetter2 == lockLetter2 || guessLetter2 == lockLetter3 || guessLetter2 == lockLetter4)
{
modifiedGuess += '+';
}
else if(guessLetter3 == lockLetter1 || guessLetter3 == lockLetter2 || guessLetter3 == lockLetter3 || guessLetter3 == lockLetter4)
{
modifiedGuess += '+';
}
else if(guessLetter4 == lockLetter1 || guessLetter4 == lockLetter2 || guessLetter4 == lockLetter3 || guessLetter4 == lockLetter4)
{
modifiedGuess += '+';
}
else
{
modifiedGuess += '*';
}
}
return modifiedGuess;
}
}
public class MyProgram extends ConsoleProgram
{
public void run()
{
CombinationLock c1 = new CombinationLock("bird");
System.out.println(c1.getClue("barn"));
}
}
You need to initialise your modifiedGuess variable, i.e.
public CombinationLock(String newLock)
{
lock = newLock;
modifiedGuess = "";
}
Alternatively, you can look into StringUtils.difference function which may give you a similar result (if allowed for the assignment).
https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#difference(java.lang.String,%20java.lang.String)

Java convert else { if {} } to elseif {}

I'm looking for any existing code or library in java or a similar language to convert given the following (which is not java, but a custom language)
if (i < 0) {
i = 0;
} else {
if (i > 100) {
i = 100;
}
}
into elseif like this:
if (i < 0) {
i = 0;
} else if (i > 100) {
i = 100;
}
This code is not java but I want to convert it using java.
Here is what I tried to acomplish this but it's not working
String elseB = "else {";
int index = output.indexOf(elseB);
while (index != -1) {
output = output.substring(index + 1);
index = output.indexOf(elseB);
if (index != -1) {
int ifAt = index + elseB.length() + 1;
String elseStart = output.substring(ifAt).trim();
if (elseStart.startsWith("if")) {
int closingBracket = findClosingBracket(
output.toCharArray(), index);
int openingBracket = ifAt - 1;
String justBlock = output.substring(openingBracket,
closingBracket).trim();
output = output.substring(0, openingBracket - 1) + justBlock + output.substring(closingBracket);
}
}
}
a more complicated example would be converting this:
if (i == 1) {
} else {
if (i == 2) {
} else {
if (i == 3) {
} else {
if (i == 4) {
} else {
if (i == 5) {
} else {
if (i == 6) {
} else {
if (i == 7) {
} else {
if (i == 8) {
} else {
if (i == 9) {
} else {
if (i == 10) {
} else {
if (i == 22) {
} else {
if (i == 11) {
} else {
if (i == 12) {
} else {
if (i == 25) {
} else {
if (i == 13) {
} else {
if (i == 14) {
} else {
if (i == 15) {
} else {
if (i == 24) {
} else {
if (i == 16) {
} else {
if (i == 17) {
} else {
if (i == 18) {
} else {
if (i == 21) {
} else {
if (i == 19) {
} else {
if (i == 20) {
} else {
if (i == 23) {
} else {
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
into this
if (i == 1) {
} else if (i == 2) {
} else if (i == 3) {
} else if (i == 4) {
} else if (i == 5) {
} else if (i == 6) {
} else if (i == 7) {
} else if (i == 8) {
} else if (i == 9) {
} else if (i == 10) {
} else if (i == 22) {
} else if (i == 11) {
} else if (i == 12) {
} else if (i == 25) {
} else if (i == 13) {
} else if (i == 14) {
} else if (i == 15) {
} else if (i == 24) {
} else if (i == 16) {
} else if (i == 17) {
} else if (i == 18) {
} else if (i == 21) {
} else if (i == 19) {
} else if (i == 20) {
} else if (i == 23) {
} else {
}
I assume you want to change the code, not the compiled bytecode. For that you would look into text replacing. Depending on your editor you can replace all the instances of this text with the elif statement. If your editor can not do it, look into regular expressions. With those you can change the lines in no time.
If trying to test a single value vs various conditions,could try to use switch statemenet instead various if statements.
ie.
switch(i){
case(i <= 0):
i=0;
case(i >= 100):
i=100;
}

Categories