Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Adding operands and operators to Arrays.

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding operands and operators to Arrays.

    Hi, I'm building a calculator and I need to take input from the user, and put the operators and operands in separate arrays. So far I have managed to split the input up by using operators and regex.

    Here is my code:

    ]import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Scanner;
     
    public class calcTester {
     
    		public static String userInput()
    		{
    			System.out.println("Please enter a string to be evaluated: ");
    			String stringEntered;
    			Scanner keyboardInput = new Scanner(System.in);
    			stringEntered = keyboardInput.nextLine();
    			keyboardInput.close();
    			return stringEntered;
    		}
     
    		public static String stringCalculator(String userInput )
    		{
    			return (userInput.replaceAll("\\s+", "").trim());	//removes space from user input
    		}
     
     
    		public static String parseInput(String stringCalculator)
    		{
    			String stringCalc = stringCalculator;
    			String[] splitCalc = stringCalc.split("(?<=[-+*/])|(?=[-+*/])");
    			return Arrays.toString(splitCalc);
    		}
     
    		ArrayList<int[]> calcOperands = new ArrayList<int[]>();
    		ArrayList<String> calcOperators = new ArrayList<String>();
     
     
    		public static void main(String[] args)
    		{
    			System.out.println(parseInput(stringCalculator(userInput())));
    		}
    }

    When I run it and enter, for example - "10+5*3" what is returned is "[10, +, 5, *, 3]"

    So what I am trying to do now is add the integers to the ArrayList calcOperands and the operators to the ArrayList calcOperators but I'm struggling to do this. Any help is greatly appreciated.

    Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding operands and operators to Arrays.

    add the integers to the ArrayList calcOperands and the operators to the ArrayList calcOperators
    For the operators you could compare the Strings against the possibilities.
    For the operands, you could use a parse method inside a try/catch block to see if the String was a valid number.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2013
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding operands and operators to Arrays.

    Sorry, I'm not sure exactly what you mean. I've already got the input split in to an array so what I really want to do is take all the characters that are +-/* from the array and put them in the operator array and all the integers and put them in the operand array but I don't know how to do this? Can you point me in the right direction?

    Thanks!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding operands and operators to Arrays.

    Go through the array from the split() and use the techniques listed in post#2 to decide where to copy the value.
    Compare the Strings to test for the operators.
    parse the Strings to test for numbers

    There might be a way to use Patterns and regular expressions to determine if the String is an operator or operand.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2013
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding operands and operators to Arrays.

    I've spent some time researching how to do it and I've come up with something but I've got a few issues. Here's the code:

     
    		public static Object[] parseInput(String stringCalculator)
    		{
    			String stringCalc = stringCalculator;
    			String[] splitNumbers = stringCalc.split("[-+*/]");  //splits on operator but leaves the operand in
    			String[] splitSymbols = stringCalc.split("[0-9]");    //splits on operand but leaves operator in
     
     
    			ArrayList<String[]> calcOperators = new ArrayList<String[]>();
    			calcOperators.add(splitSymbols);
     
    			ArrayList<String[]> calcOperands = new ArrayList<String[]>();
    			calcOperators.add(splitNumbers);
     
    			return new Object[]{calcOperators, calcOperands};
    		}

    The problems I'm having is that I don't know how to test to see if the operators and operands have actually been added to the array because if I try and print them I get an error like this "[Ljava.lang.Object;@544b02".

    I also need to be able to use both arrays in other methods and I've tried to return both, but then I still don't understand how I can use them in other methods.

    Thanks for the help!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding operands and operators to Arrays.

    You can Print out the contents of an array by using this method:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2013
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding operands and operators to Arrays.

    When I'm trying to print using that I'm getting the error "The method toString(long[]) in the type Arrays is not applicable for the arguments (ArrayList<String[]>)"

    Eclipse is suggesting I change to deepToString but when I do that there is still an error and it tell me to change back to toString, what it was before.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding operands and operators to Arrays.

    What is the arg to the toString() method? It needs to be an array, not an ArrayList.
    The only arrays I see are: splitNumbers and splitSymbols
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2013
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding operands and operators to Arrays.

    Sorry I'm not sure I understand you when you say it needs to be an array not an arrayList? Surely it's better to use an ArrayList as I don't have to define how big it is?

    Here is my code incase I've made some error and that's why the print isn't working:

    		public static Object[] parseInput(String stringCalculator)
    		{
    			String stringCalc = stringCalculator;
    			String[] splitNumbers = stringCalc.split("[-+*/]");  //splits on operator but leaves the operand in
    			String[] splitSymbols = stringCalc.split("[0-9]");   //splits on operand but leaves the operator in
     
     
    			ArrayList<String[]> calcOperators = new ArrayList<String[]>();
    			calcOperators.add(splitSymbols);
     
    			ArrayList<String[]> calcOperands = new ArrayList<String[]>();
    			calcOperators.add(splitNumbers);
     
    			System.out.println(Arrays.toString(calcOperators));
    			System.out.println(Arrays.toString(calcOperands));
     
    			return new Object[]{calcOperators, calcOperands};
    		}

    Thanks!

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding operands and operators to Arrays.

    System.out.println(Arrays.toString(calcOperators));
    the arg to the toString() method needs to be an array,
    Read the API doc for the Array class's toString() method to see what I am talking about.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Dec 2013
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding operands and operators to Arrays.

    Ok, i've read what you said and realised that you can't print an ArrayList like that. I've also read about printing an arrayList using a for loop and I've tried that but it's still printing correctly.

    Here's my new code:
    		public static Object[] parseInput(String stringCalculator)
    		{
    			String stringCalc = stringCalculator;
    			String[] splitNumbers = stringCalc.split("[-+*/]");
    			String[] splitSymbols = stringCalc.split("[0-9]");
     
     
    			ArrayList<String[]> calcOperators = new ArrayList<String[]>();
    			calcOperators.add(splitSymbols);
     
    			ArrayList<String[]> calcOperands = new ArrayList<String[]>();
    			calcOperators.add(splitNumbers);
     
    			int n = calcOperators.size();
    			for (int i = 0; i < n; i++)
    				System.out.println(calcOperators.get(i));
     
     
    			return new Object[]{calcOperators, calcOperands};
    		}

    And when I run it this is what prints out:
    [Ljava.lang.String;@18c5b4f
    [Ljava.lang.String;@1beb7ec
    [Ljava.lang.Object;@1cfb56


    I also added this line in to see how long the ArrayList was:
    System.out.println(calcOperands.size());
    System.out.println(calcOperators.size());

    And no matter what the input was, be it, 1+1+1+1+1+1+1+1+1 or 3/4*5+7-6+2 then length of the operands ArrayList would always be 0 and the length of the Operators arrayList would always be 2. Does that means that the ArrayLists aren't working as they should?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding operands and operators to Arrays.

    [Ljava.lang.String;@18c5b4f
    That is the String from a String array.

    Have you tried printing the contents of these arrays: splitNumbers and splitSymbols
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Dec 2013
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding operands and operators to Arrays.

    Quote Originally Posted by Norm View Post
    That is the String from a String array.

    Have you tried printing the contents of these arrays: splitNumbers and splitSymbols
    I think I've finally figured it out, but I'm not completely sure. I think I've got both the operands and operators in arrays, but as I've used different ways to get them in to the arrays I'm not sure if they're both actually in Arrays. Would it be OK if you had a quick look at the code for me?

    	public static void parseInput(String stringCalculator)
    	{
    		String[] operand= stringCalculator.split("([-+*/])");			
    		String operator = stringCalculator.replaceAll("[0-9]", "");
     
    		System.out.println("Arraylist operands contains: " + Arrays.toString(operand));
     
    		ArrayList<String> calcOperators = new ArrayList<String>();
    		calcOperators.add(operator);
    		System.out.println("Arraylist operators contains: " + calcOperators.toString());  
     
     
    	}

    When the input of "1+2*3-4/5+6" is inputted the output is:
    Arraylist operands contains: [1, 2, 3, 4, 5, 6]
    Arraylist operators contains: [+*-/+]

    Thanks for all the help!

Similar Threads

  1. Adding two arrays
    By kingsta in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2013, 06:14 PM
  2. Operators
    By Skybear in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 11th, 2012, 01:53 PM
  3. Large number while adding arrays
    By LoganC in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 6th, 2012, 09:27 PM
  4. [Help] Problem with Class arrays, adding/editing and deleting
    By Grant_Searle in forum Object Oriented Programming
    Replies: 7
    Last Post: December 16th, 2011, 10:10 AM
  5. Logical Operators
    By truebluecougarman in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 06:26 PM