Problems getting into an "if" statement, JAVA - java

I've got a program that goes through and reads "tokens" that can either be a String (Symbol) or a number. It uses postfix and a stack to evaluate simple commands.
For example:
/x 100 def
/y 200 def
x y add
should return 300. The first line defines a variable called "x" and sets it to 100. To do this a reader adds "/x" and "100" on the stack and stops when it gets to the "def" operator, which tells it to go make a token with a Symbol called "x" and its value being 100. The stack is then empty, and next time "x" would be pushed, the interpreter should automatically replace it with its value. This is where my problem lies.
This is my interpreter:
while ( r.hasMoreTokens() ) {
Token t = r.nextToken();
if ( !t.isSymbol() ) {
operands.push( t );
} else if (env.contains(t.getSymbol())) {
Token tmp = env.get(t.getSymbol());
operands.push(tmp);
} else if (t.getSymbol().startsWith("/")) {
operands.push(t);
} else if ( t.getSymbol().equals( "def" ) ){
execute_def();
} else if ( t.getSymbol().equals( "add" ) ) {
execute_add();
} else if ( t.getSymbol().equals( "sub" ) ) {
execute_sub();
} else if ( t.getSymbol().equals( "mul" ) ) {
execute_mul();
} else if ( t.getSymbol().equals( "exch" ) ) {
execute_exch();
} else if ( t.getSymbol().equals( "dup" ) ) {
execute_dup();
} else if ( t.getSymbol().equals( "pop" ) ) {
execute_pop();
} else if ( t.getSymbol().equals( "pstack" ) ) {
execute_pstack();
} else if ( t.getSymbol().equals( "moveto" ) ) {
execute_moveto();
} else if ( t.getSymbol().equals( "lineto" ) ) {
execute_lineto( g );
} else if ( t.getSymbol().equals( "arc" ) ) {
execute_arc( g );
} else if ( t.getSymbol().equals( "quit" ) ) {
execute_quit();
} else {
System.out.println( "ILLEGAL SYMBOL: " + t );
}
}
Once the variables get defined correctly, I cant get into that first else if and change the value. Because I can't do this, I never push anything on the stack and end up with an empty stack error. Here are the methods contains() and get() from env (environment):
public boolean contains(String key) {
Elem tmp = top;
for (int i = 0; i < size; i++) {
if (tmp.key == key) {
return true;
} else {
tmp = tmp.next;
}
}
return false;
}
public Token get(String key) {
Elem tmp = top;
int counter = 0;
boolean found = false;
for (int i = 0; i < size; i++) {
if (tmp.key == key) {
found = true;
break;
} else {
tmp = tmp.next;
}
counter++;
}
if (found == true) {
tmp = top;
for (int i = 0; i <= counter; i++) {
tmp = tmp.next;
}
return tmp.value;
} else {
throw new BadKeyQueryException();
}
}
I'm using linked elements in the environment to keep track of symbols. Elem is a nested class in Environment:
private static class Elem {
private String key;
private Token value;
private Elem next;
private Elem(String key, Token value, Elem next) {
this.key = key;
this.value = value;
this.next = next;
}
}
Thanks for any help from you guys!

Strings in java are Objects, rather than primitives.
When you say:
int i = 5;
i stores the value "5".
When you say:
String s = "string";
s stores the value of a reference to "string".
Comparing s to "string" would return false, even though they contain the same value when you print there. This is because the computer compares a reference to memory containing "string" to another reference to memory containing "string". Same values, but different references.
Also, you're setting "t" to multiple different values in your code. Try setting t once, before everything, and a precomputed t value against your if-else-if block.

You can only call getSymbol() once, so you just need to store the value from it. Here is the start of your statements, you should be able to change the others the same way
if ( !t.isSymbol() ) {
operands.push( t );
continue;
}
String symbol = t.getSymbol();
if (env.contains(symbol)) {
Token tmp = env.get(symbol);
operands.push(tmp);
} else if (symbol.startsWith("/")) {
operands.push(t);
...

Related

Making a password verification into a class

I have a program grabbing a password from the user, then it checks if the conditions are met or not then outputs "Valid Password" or "Invalid Password". This works, and I was able to turn the verification aspect into a method in the same program and it works, but I want to make it into a class where I can just say if( validate(pw) == true ) ... or at least if( v.getValidation() == true ) ... in any program and it will test my conditions. I've used custom classes before but for some reason everything I try does not work on this one, I've been at it for days.
Here's my method:
public boolean validate( String pw )
{
boolean l = false, u = false, lo = false, d = false, r = true;
if( pw.length() >= 6 )
{ l = true; }
for( int i = 0; i < pw.length(); i++ )
{
if( Character.isUpperCase( pw.charAt(i) ) )
{ u = true; }
if( Character.isLowerCase( pw.charAt(i) ) )
{ lo = true; }
if( Character.isDigit( pw.charAt(i) ) )
{ d = true; }
}
if( l == false || u == false || lo == false || d == false )
{ r = false; }
return r;
}
Edit:
Thank you all for your input, this is what it came out to in the end:
public class Password
{
public static boolean validate( String pw )
{
boolean result = false;
int upper = 0, lower = 0, digit = 0;
if( pw.length() >= 6 )
{
for( int i = 0; i < pw.length(); i++ )
{
if( Character.isUpperCase( pw.charAt(i) ) )
{ upper++; }
if( Character.isLowerCase( pw.charAt(i) ) )
{ lower++; }
if( Character.isDigit( pw.charAt(i) ) )
{ digit++; }
}
}
if( upper >= 1 && lower >= 1 && digit >= 1 )
{ result = true; }
return result;
}
}
You do not need to make a whole class for this. You can do something like:
public static void main(String[] args) {
boolean valid = validate("PassWord22");
}
public static boolean validate( String pw ) {}
Also some notes on your method:
You don't need to do l == true or l == false in your if statement. You can simply do:
if( !l || !u || !lo || !d )
{ r = false; }
In fact you can just return
return l && u && lo && d;
If the length is not 6 or greater, simply return false. This will save checking all the letters in the String
I would come up with better variable names. Single/two letter variable names makes it very hard to tell what they represent, and easy to mix up. (instead of l you could have length and instead of u you could have upper)
Also this can be easier solved with regex and String.matches():
public static boolean validate(String pw) {
String pattern = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).+$";
return (pw.length() > 5 && pw.matches(pattern));
}
I got it working, turned out to be an error in the code that the compiler was not picking up. I wanted to delete the question but they wont let me for some reason. So in case you're curious this is what my class looks like functioning:
public class Password
{
private String pw;
public Password()
{
pw = "";
}
public Password( String pw )
{
this.pw = pw;
}
public boolean getPassword( String pw )
{
boolean l = false, u = false, lo = false, d = false, r = true;
if( pw.length() >= 6 )
{ l = true; }
for( int i = 0; i < pw.length(); i++ )
{
if( Character.isUpperCase( pw.charAt(i) ) )
{ u = true; }
if( Character.isLowerCase( pw.charAt(i) ) )
{ lo = true; }
if( Character.isDigit( pw.charAt(i) ) )
{ d = true; }
}
if( l == false || u == false || lo == false || d == false )
{ r = false; }
return r;
}

Object Assignment Problems

I am having a problem when i assign objects, the object produces a reference duplicate that changes the value in original object.
This is my original object
Node root = new Node();
root.filingState("input.txt");
After pushing my root in deque data structure, i retrieve it in a tmp variable
Node tmp = deque.pop();
After that, i want my tmp variable to apply all the operators(up, down, left, right, which ever ones are possible) and check for the best result.
if ( !tmp.goalTest(goal) ) {
if ( tmp.ifDown() ) {
if ( !visited[tmp.getX()+1][tmp.getY()] ) {
newDown = new Node(tmp);
newDown.moveDown();
} else newDown = null;
} // #EndOfDown!
if ( tmp.ifUp() ) {
if ( !visited[tmp.getX()-1][tmp.getY()] ) {
newUp = new Node(tmp);
newUp.moveUp();
} else newUp = null;
} // #EndOfUp!
if ( tmp.ifLeft() ) {
if ( !visited[tmp.getX()][tmp.getY()-1] ) {
newLeft = new Node(tmp);
newLeft.moveLeft();
} else newLeft = null;
} // #EndOfMoveLeft!
if ( tmp.ifRight() ) {
if ( !visited[tmp.getX()][tmp.getY()+1] ) {
newRight = new Node(tmp);
newRight.moveRight();
} else newRight = null;
} // #EndOfMoveRight!
} else break;
Operator functions
public boolean ifUp ( ) { return y_blank-1 >= 0; }
public boolean ifDown ( ) { return y_blank+1 <= 4; }
public boolean ifLeft ( ) { return x_blank-1 >= 0; }
public boolean ifRight ( ) { return x_blank+1 <= 4; }
/* Movement functions
Allows a user to move his blank on board
*/
public void moveUp ( ) {
board[y_blank][x_blank] = board[y_blank-1][x_blank];
board[y_blank-1][x_blank] = 'B';
y_blank -= 1;
} // #EndOfMoveUp!
public void moveLeft ( ) {
board[y_blank][x_blank] = board[y_blank][x_blank-1];
board[y_blank][x_blank-1] = 'B';
x_blank -= 1;
} // #EndOfMoveLeft!
public void moveRight ( ) {
board[y_blank][x_blank] = board[y_blank][x_blank+1];
board[y_blank][x_blank+1] = 'B';
x_blank += 1;
} // #EndOfMoveRight!
public void moveDown ( ) {
board[y_blank][x_blank] = board[y_blank+1][x_blank];
board[y_blank+1][x_blank] = 'B';
y_blank += 1;
} // #EndOfMoveDown!
newDown fails of 1st iteration since it doesn't fulfill the condition by my other Nodes pass the condition.
after my 1st newUp applies its operator, it changes the tmp Node aswell. Allowing newLeft, newRight to apply operators to the newly tmp Node values.
class Node.java
private char[][] board;
private int x_blank = -1;
private int y_blank = -1;
private Node parent = null;
private Node up = null;
private Node left = null;
private Node right = null;
private Node down = null;
/* Constructor
Allocates board to a 5x5 2D Array
*/
public Node ( ) { board = new char[5][5]; }
i tried creating a copy constructor but doesn't solve the problem.
Copy constructor
public Node ( Node tmp ) {
this.board = new char[5][5];
for ( int i=0; i<5; ++i ) {
for ( int j=0; j<5; ++j ) this.board[i][j] = tmp.board[i][j];
}
this.x_blank = tmp.x_blank;
this.y_blank = tmp.y_blank;
if ( tmp.parent != null ) this.parent = tmp.parent;
if ( tmp.up != null ) this.up = tmp.up;
if ( tmp.left != null ) this.left = tmp.left;
if ( tmp.right != null ) this.right = tmp.right;
if ( tmp.down != null ) this.down = tmp.down;
}
so my question is that, how can i create an Object that can hold the values of some other Object, but when i apply the operator on the newly created object, it doesn't change the value of the original Object.
Your copy constructor won't automatically be used on assignment of an instance of that object to a new variable. It is intended in Java that object assignments are by reference. You need to explicitly use your copy constructor in the assignment:
Node tmp = new Node(deque.pop());
I think the best way is to implement a clone function that will create a new object with the same value, then you will not change the value of the original Object.
for example:
clone(Node mynode){
return new Node(mynode.getValue());
}

compare between two queue list

I have two queue lists and I want to check if q2 is reverse for q1.
This is my code.
public static boolean reverse ( LinkedQueue q1, LinkedQueue q2 ) {
for ( int i = 0; i < q1.length() ; i ++)
if ( q1.serve () == q2.gettail().getdata() ) {
q1.enqueue(q1.serve ());
return true ;
} else {
return false;
}
return false;
}
I just check if the head of q1 equal the tail of q2 but my problem is I want to check for all elements that they are equal and I don't know how.
The way one normally approaches it is to return true in the end. Iterate through and only return false if i+n i+length-n.
Assuming everything else is working it will be like this.
public static boolean reverse ( LinkedQueue q1, LinkedQueue q2 ) {
for ( int i = 0; i < q1.length() ; i ++) {
if ( q1.serve () == q2.gettail().getdata() ) {
q1.enqueue(q1.serve ());
} else {
return false;
}
}
return true;
}
Note: You also missed curly brackets around your for.

Recursively Find and Remove Node from LinkedList

Given a string to search for, I want to write a recursive function that takes in only one parameter (The string to search for). The function will search for the value recursively and if it is found then it will remove the item and return it. If it is not found then, the function will reach the end of the list and return null. What I have so far I think is the right idea except it is not functioning properly:
Main Test Class
public static void main(String[] args) {
RecLinkedList list = new RecLinkedList();
list.add("A");
list.add("B");
list.add("D");
list.add("C", 2);
list.add("E", 4);
list.add("G", 6); //this should be invalid
System.out.println( list );
System.out.println( list.remove( 1 ).getValue() );
System.out.println( list.remove("D").getValue() );
System.out.println( list.remove("G").getValue() );
System.out.println( list.size() );
System.out.println( list );
}
Linked List Class (Showing only what I need help on)
public class RecLinkedList {
private Node first;
private int size = 0;
public RecLinkedList(){
first = null;
}
public boolean isEmpty() {
return first == null;
}
public Node remove( String s ){
return remove( s, 0, first );
}
private Node remove( String s, int count, Node list ){
if( list == null ){
return null;
}else if( s.equals(s) ){
first = list.getNext();
return list;
}else if( s.equals(count+1) ){
Node n = list.getNext();
if( list.getNext() != null ){
list.setNext( list.getNext().getNext() );
}
return n;
}else{
return remove( s, count+1, list.getNext() );
}
}
So far, I am able to remove the item but as of now the item "A" is getting removed when it should not be. The final list should be A,C,E. (G should return and print null because it does not exist). I think I am close, but off by something minor, but I can not seem to figure it out.
There are several errors in your code (see comments below) :
private Node remove( String s, int count, Node list ){
if( list == null ){
return null;
}else if( s.equals(s) ){ // comparing s to itself means you always remove
// the first element from the list (since this
// condition is always true)
first = list.getNext();
return list;
}else if( s.equals(count+1) ){ // comparing the String s to an int - makes
// no sense, will never be true
Node n = list.getNext();
if( list.getNext() != null ){
list.setNext( list.getNext().getNext() );
}
return n;
}else{
return remove( s, count+1, list.getNext() );
}
}
It seems to me like there is some ambiguity in your question. I understand that your method should search for an element, remove it if present, and return the same object. If the element is not present, the method should return null. That seems pretty straight-forward since most of your utility methods are already implemented in LinkedList. I thus recommend to extend that class:
public class RecLinkedList<E>
extends LinkedList<E>
{
public E removeAndReturn(E element)
{
E result;
if (this.contains(element)) {
remove(element);
result = element;
}
else {
result = null;
}
return result;
}
}
I don't see why you would want to implement this recursively.
This could clearly be written more concisely, but the explicit if-else should make it clearer.
EDIT: The more concise and probably better implementation would be:
public E removeAndReturn(E element)
{
return remove(element) ? element : null;
}

Performing if else check and setting value for later use. Looking for a cleaner way

I'm writing a customer parser to extract key parts of information from application logs to help with debugging issues.
Within the parser code I have lots of examples of the following sort of logic:
String element = "";
if( rawLogText.contains("RequestType") ) {
element = "RequestType";
}
else if( rawLogText.contains("ResponseType") ) {
element = "ResponseType";
}
if( element.equals("") ) {
return;
}
I feel like there is a cleaner way to do this sort of check-set-return-process logic.
Can anyone come up with a cleaner way to do it?
Here's what I had initially, but the multiple contains checks bugged me.
String element = "";
if( rawLogText.contains("RequestType") || rawLogText.contains("ResponseType") ) {
if( rawLogText.contains("RequestType") ) {
element = "RequestType";
}
else if( rawLogText.contains("ResponseType") ) {
element = "ResponseType";
}
}
else {
return;
}
I'd also like to avoid ternary statements.
What about this instead of checking element.equals():
String element = null;
if( rawLogText.contains("RequestType") ) {
element = "RequestType";
}
else if( rawLogText.contains("ResponseType") ) {
element = "ResponseType";
} else {
return;
}
//continue the process
I think that #GuillermoMerino's solution is probably the best.
But if you repeated this pattern a lot in your parsing you could abstract it as a helper method; e.g.
public String matchWord(String input, String... words) {
for (String word: word) do
if (input.contains(word) ) {
return word;
}
}
return "";
}
and use it like this:
String element = matchWord(rawLogText, "RequestType", "ResponseType");
if (element.equals("")) {
return;
}
Let's say you put all the types in a string array:
String[] possibleTypes = {"RequestType", "ResponseType"}; // and such
Now you can use a for loop to check and set value
String element = ""
for (String type : possibleTypes) {
if (rawLogText.contains(type)) {
element = type;
break;
}
}
if( element.equals("") ) {
return;
}

Categories

Resources