CountBale's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
23 (0.04 per day)
Joined:
22-January 13
Profile Views:
324
Last Active:
User is offline Dec 12 2013 07:24 AM
Currently:
Offline

Previous Fields

Country:
GB
OS Preference:
Windows
Favorite Browser:
Chrome
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Your Car:
Who Cares
Dream Kudos:
0
Icon   CountBale has not set their status

Posts I've Made

  1. In Topic: Popping the top value of a stack and adding it to a queue

    Posted 12 Dec 2013

    The whole class is here:

    package lab9;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Stack;
    
    public class StringCalculator 
    {
    	String evalString;
    	String acceptInput = "0123456789+-/*";
    	String theNumbers = "0123456789";
    	String theOperators = "+-/*";
    	
    	Queue<Object> outputQueue = new LinkedList<Object>();
    	
    	Stack<Character> operatorStack = new Stack<Character>();
    	Stack<Integer> priorityStack = new Stack<Integer>();
    	Stack<Object> outputStack = new Stack<Object>();
    	
    	String[] splitString;
    	
    	ArrayList<Integer> calcOperands = new ArrayList<Integer>();
    	ArrayList<Character> calcOperators = new ArrayList<Character>();
    	ArrayList<Integer> priorityList = new ArrayList<Integer>();
    	
    	int strLen;
    	int numOperands;
    	int numOperators;
    	
    	public StringCalculator(String inputString)
    	{
    		evalString = inputString;
    		splitString = evalString.split(" ");
    		strLen  = splitString.length;
    	}
    	
    	public boolean checkInput()
    	{
    		int x = 0;
    		
    		while(x < strLen)
    		{
    			if(acceptInput.contains(splitString[x]) == true)
    			{
    				x++;
    			}
    			else
    			{
    				return false;
    			}	
    			
    		}
    		return true;
    	}
    	
    	public boolean parseInput()
    	{
    		int x = 0;
    		while(x < strLen)
    		{
    			if(theNumbers.contains(splitString[x]))
    			{
    				calcOperands.add(Integer.parseInt(splitString[x]));
    			}
    			else if(theOperators.contains(splitString[x]))
    			{
    				calcOperators.add(splitString[x].charAt(0));
    			}
    			else
    			{
    				return false;
    			}
    			x++;
    		}
    		numOperands  = calcOperands.size();
    		return true;
    	}
    	
    	public boolean calculatePriority()
    	{
    		int x = 0;
    		char y;
    		numOperators = calcOperators.size();
    		while(x < numOperators)
    		{
    			y = calcOperators.get(x);
    			switch(y)
    			{
    			case '+': 
    			case '-': 
    				priorityList.add(x, 2);
    				break;
    			case '*':
    			case '/':
    				priorityList.add(x, 3);
    				break;
    			default: 
    				return false;
    		
    			}
    			x++;
    		}
    		return true;
    	}
    	
    	public boolean shuntingYard()
    	{
    		int x = 0;
    		int stackPriority = 0;
    		char tempOperator;
    		while(x < (numOperators))
    		{
    			outputQueue.add(calcOperands.get(x));
    			if((operatorStack.empty() != true)&&(priorityList.get(x) <= stackPriority))
    			{
    				outputQueue.add(operatorStack.pop());
    			}
    			operatorStack.push(calcOperators.get(x));
    			priorityStack.push(priorityList.get(x));
    			stackPriority = (Integer)priorityStack.pop();
    			x++;
    		}
    		
    		outputQueue.add(calcOperands.get(numOperands - 1));
    		while(operatorStack.empty() != true)
    		{
    			tempOperator = operatorStack.pop();
    			System.out.println(tempOperator);
    			outputQueue.add(tempOperator);
    		}
    		
    		/*for(int i = 0; i < outputQueue.size(); i++)
    		{
    			System.out.println(outputQueue.remove());
    		}*/
    
    		return true;
    	}
    	
    	public int calculateResult()
    	{
    		int queueLength = outputQueue.size();
    		int x = 0;
    		int output = 0;
    		Object currDigit;
    		while(x < queueLength)
    		{
    			currDigit = outputQueue.remove();
    			if(currDigit instanceof Integer)
    			{
    				outputStack.push(currDigit);
    			}
    			else if(currDigit == "+")
    			{
    				outputStack.push((Integer) outputStack.pop() + (Integer) outputStack.pop());
    			}
    			else if(currDigit == "-")
    			{
    				outputStack.push(((Integer) outputStack.pop() * -1) + (Integer) outputStack.pop());
    			}
    			else if (currDigit == "*")
    			{
    				outputStack.push((Integer) outputStack.pop() * (Integer) outputStack.pop());
    			}
    			else if (currDigit == "/")
    			{
    				outputStack.push((1/(Integer) outputStack.pop()) * (Integer) outputStack.pop());
    			}
    			x++;
    		}
    		output = (Integer) outputStack.pop();
    		return output;
    	}
    	
    	
    
    }
    
    


    and the main class that calls it is:

    package lab9;
    import java.io.Console;
    import java.util.Scanner;
    import java.util.concurrent.TimeUnit;
    
    public class ProjectCalculator 
    {
    	public static void main(String[] args)
    	{
    		String currChar = "";
    		String lastChar = "";
    		
    		Scanner scanner = new Scanner (System.in);
    		
    		int x = 0;
    		
    		System.out.println("Please enter your expression: ");
    		String inputString = scanner.nextLine();
    		
    		
    		
    		String[] splitString = inputString.split(" ");
    		int strLen = splitString.length;
    		
    		/*while(x < strLen)
    		{
    			lastChar = currChar;
    			currChar = splitString[x];
    			if((currChar == " ")&&(lastChar == " "));
    			{
    				System.out.println("Please enter an expression with only one space separating the terms");
    				try 
    				{
    					TimeUnit.SECONDS.sleep(5);
    				} 
    				catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				System.exit(1);
    			}
    			x++;
    		}*/
    		
    		StringCalculator StrCalc = new StringCalculator(inputString);
    		if(StrCalc.checkInput() == true)
    		{
    			StrCalc.parseInput();
    			StrCalc.calculatePriority();
    			StrCalc.shuntingYard();
    			System.out.println(StrCalc.calculateResult());
    		}
    		try 
    		{
    			TimeUnit.SECONDS.sleep(5);
    		} 
    		catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		System.exit(1);
    		
    	}
    
    }
    
    
  2. In Topic: Popping the top value of a stack and adding it to a queue

    Posted 11 Dec 2013

    Here is the specific method that is being run, if you want I can post the whole class.

    public boolean shuntingYard()
    	{
    		int x = 0;
    		int stackPriority = 0;
    		while(x < (numOperators))
    		{
    			outputQueue.add(calcOperands.get(x));
    			if((operatorStack.empty() != true)&&(priorityList.get(x) <= stackPriority))
    			{
    				outputQueue.add(operatorStack.pop());
    			}
    			operatorStack.push(calcOperators.get(x));
    			priorityStack.push(priorityList.get(x));
    			stackPriority = (Integer)priorityStack.pop();
    			x++;
    		}
    		
    		outputQueue.add(calcOperands.get(numOperands - 1));
    		while(operatorStack.empty() != true)
    		{
    			System.out.println("Hello");
    			outputQueue.add(operatorStack.pop());
    		}
    		
    		/*for(int i = 0; i < outputQueue.size(); i++)
    		{
    			System.out.println(outputQueue.remove());
    		}*/
    
    		return true;
    	}
    
  3. In Topic: Exception in thread "main" java.lang.NullPointerException

    Posted 7 Dec 2013

    What should I do to fix that? It's weird that that would be the issue since that is part of the original code, not something I've added or changed.
  4. In Topic: Exception in thread "main" java.lang.NullPointerException

    Posted 7 Dec 2013

    The console after the program fails to run is:

    Exception in thread "main" java.lang.NullPointerException
    at lab8.exercise1.Notebook.storeNote(Notebook.java:32)
    at lab8.exercise1.notepadTester.main(notepadTester.java:9)
  5. In Topic: SDL Runtime terminated in unusual way

    Posted 17 Apr 2013

    View PostButchDean, on 16 April 2013 - 05:39 AM, said:

    You need to place breakpoint in your routine(s) that close down your game and step through the code. You're probably releasing a resource more than once, or releasing a resource that was never allocated in the first place. Who knows.


    In my image loading function, I dont free optimizedImage, is there a way I can free that? If I free it before it is returned then the function is pointless but if I cant free it after it is returned.

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Location:
Britain
Interests:
Gaming
Anime
Game Development
Years Programming:
2
Programming Languages:
C++
C

Contact Information

E-mail:
Private

Friends

CountBale hasn't added any friends yet.

Comments

CountBale has no profile comments yet. Why not say hello?