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 8 of 8

Thread: Why does my code throw an exception when there aren't square brackets around the delimiter for the String split method?

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why does my code throw an exception when there aren't square brackets around the delimiter for the String split method?

    I am currently trying to make a calculator in Java. I want to use the String split method to tokenize the string of characters inputted. I thought I was using the String split method wrongly, because I had surrounded the characters I wanted to delimit with square brackets. However, when I removed the square brackets, the code threw an exception when I pressed the equal button. The exception was a PatternSyntaxException exception. Am I using the String split method wrongly? And why is the exception thrown? Here is my code:

    import javax.swing.*;//import the packages needed for gui
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Arrays;
    import java.util.List;
    import static java.lang.Math.*;
    public class CalculatorCopy {
        public static void main(String[] args) {
    	JFrame window = new JFrame("Window");//makes a JFrame
    	    window.setSize(300,415); 
    	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	JPanel panel = new JPanel (new FlowLayout());//makes the panel, textfield and buttons and gives the buttons actioncommands
        final JTextField textField = new JTextField(20);
    	JButton openbracket = new JButton("(");
    		openbracket.setActionCommand("(");
    	JButton closebracket = new JButton(")");
    	closebracket.setActionCommand(")");
    	JButton clearbutton = new JButton("C");
    	clearbutton.setActionCommand("C");
    	JButton arcsin = new JButton("arcsin");
    	arcsin.setActionCommand("arcsin");
    	JButton arccos = new JButton("arccos");
    	arccos.setActionCommand("arccos");
    	JButton arctan = new JButton("arctan");
    	arctan.setActionCommand("arctan");
    	JButton sin = new JButton("sin");
    	sin.setActionCommand("sin");
    	JButton cos = new JButton("cos");
    	cos.setActionCommand("cos");
    	JButton tan = new JButton("tan");
    	tan.setActionCommand("tan");
    	JButton log = new JButton("log");
        log.setActionCommand("log");
    	JButton seven = new JButton("7");
    	seven.setActionCommand("seven");
    	JButton eight = new JButton("8");
    	eight.setActionCommand("eight");
    	JButton nine = new JButton("9");
    	nine.setActionCommand("nine");
    	JButton four = new JButton("4");
    	four.setActionCommand("four");
    	    JButton five = new JButton("5");
    	    five.setActionCommand("five");
    	    JButton six = new JButton("6");
    	    six.setActionCommand("six");
    	    JButton one = new JButton("1");
    	    one.setActionCommand("one");
    	    JButton two = new JButton("2");
    	    two.setActionCommand("two");
    	    JButton three = new JButton("3");
    	    three.setActionCommand("three");
    	    JButton zero = new JButton("0");
    	    zero.setActionCommand("zero");
    	    JButton radixpoint = new JButton(".");
    	    radixpoint.setActionCommand("radixpoint");
    	    JButton plus = new JButton("+");
    	    plus.setActionCommand("plus");
    	    JButton subtract = new JButton("-");
    	    subtract.setActionCommand("subtract");
    	    JButton multiply = new JButton("x");
    	    multiply.setActionCommand("multiply");
    	    JButton divide = new JButton("/");
    	    divide.setActionCommand("divide");
    	    JButton equal = new JButton("=");
    	    equal.setActionCommand("equal");
    	    final String values = " ";
    	    class Listener implements ActionListener {	
    	    	String out = "";
    	     public void actionPerformed(ActionEvent e) {   		  
    		    String output = e.getActionCommand();
    		    if(output.equals("(")) {
                   out = out + "(";
                   textField.setText(out);   
    		    } else if (output.equals(")")) {
    		    out = out + ")";
    		    textField.setText(out);	
    		    }	else if (output.equals("C")) {
    		    	textField.setText(" ");
    		    	out = "";
    		    }  else if (output.equals("arcsin")) {
    		    	out = out + "asin(";
    		    	textField.setText(out);	
    		    }	else if (output.equals("arccos")) {
    		    	out = out + "acos(";
    		    	textField.setText(out);
    		    }	 else if (output.equals("arctan")) {
    		    	out = out + "atan(";
    		    	textField.setText(out);
    		    }   else if (output.equals("log")) {
    		    	out = out + "log(";
    		    	textField.setText(out);	
    		    }   else if (output.equals("seven")) {
    		    	out = out + "7";
    		    	textField.setText(out);
    		    }   else if (output.equals("eight")) {
    		    	out = out + "8";
    		    	textField.setText(out);
    		    }   else if (output.equals("nine")) {
    		    	out = out + "9";
    		    	textField.setText(out);
    		    }   else if (output.equals("four")) {
    		    	out = out + "4";
    		    	textField.setText(out);
    		    }   else if (output == "five") {
    		    	out = out + "5";
    		    	textField.setText(out);
    		    }   else if (output.equals("six")) {
    		    	out = out + "6";
    		    	textField.setText(out);
    		    }   else if (output.equals("one")) {
    		    	out = out + "1";
    		    	textField.setText(out);
    		    }   else if (output.equals("two")) {
    		    	out = out + "2";
    		    	textField.setText(out);
    		    }   else if (output.equals("three")) {
    		    	out = out + "3";
    		    	textField.setText(out);
    		    }   else if (output.equals("zero")) {
    		    	out = out + "0";
    		    	textField.setText(out);
    		    }   else if (output.equals("radixpoint")) {
    		    	out = out + ".";
    		    	textField.setText(out);
    		    }   else if (output.equals("equal")) {
    		    	String[] numbers = out.split("+-x/doubleintarclg", 0); //separate the numbers from the operations
    		    	String[] operations = out.split("1234567890.()", 0);
    		    	double[] realnumbers = new double[100]; //make an array of doubles
    		    	double answer = 0;
    		    	for ( String number: numbers ) {
    		    	for (double realnumber : realnumbers ) {
    		    	realnumber =  Double.parseDouble(number); //make the string number a double value
    		    	}		
    		    	}	
                    for (String op : operations ) {
                    	if (op.equals("x")) {
                    	  answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]*realnumbers[Arrays.asList(operations).indexOf(op)+1]; 
                    	}
                    	if (op.equals("/")) {
                    	answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]/realnumbers[Arrays.asList(operations).indexOf(op)+1];
                    	}
                    	if (op.equals("+")) {
                    	answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]+realnumbers[Arrays.asList(operations).indexOf(op)+1];	
                    	}
                    	if (op.equals("-")) {
                    	answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]-realnumbers[Arrays.asList(operations).indexOf(op)+1];
                    	}
                    	String stringofanswer = String.valueOf(answer);
                    	textField.setText(stringofanswer);
     
                    }
    		    }   else if (output.equals("plus")) {
    		    	out = out + "+";
    		    	textField.setText(out);
    		    }   else if (output.equals("subtract")) {
    		    	out = out + "-";
    		    	textField.setText(out);
    		    }   else if (output.equals("multiply")) {
    		    	out = out + "x";
    		    	textField.setText(out);
    		    }   else if (output.equals("divide")) {
    		    	out = out + "/";
    		    	textField.setText(out);
    		    }
    		}
    	}
    	Listener listener = new Listener(); //makes an object of the actionlistener
     panel.add(textField);//adding all the things
    	    window.add(panel);
    	openbracket.addActionListener(listener);    
    	panel.add(openbracket);
    	closebracket.addActionListener(listener);
    	panel.add(closebracket);
    	clearbutton.addActionListener(listener);
    	panel.add(clearbutton);
    	arcsin.addActionListener(listener);
    	panel.add(arcsin);
    	arccos.addActionListener(listener);
    	panel.add(arccos);
    	arctan.addActionListener(listener);
    	panel.add(arctan);
    	sin.addActionListener(listener);
    	panel.add(sin);
    	cos.addActionListener(listener);
    	panel.add(cos);
    	tan.addActionListener(listener);
    	panel.add(tan);
    	log.addActionListener(listener);
    	panel.add(log);
    	nine.addActionListener(listener);
    	panel.add(nine);
    	eight.addActionListener(listener);
    	panel.add(eight);
    	seven.addActionListener(listener);
    	panel.add(seven);
    	six.addActionListener(listener);
    	panel.add(six);
    	five.addActionListener(listener);
    	panel.add(five);
    	four.addActionListener(listener);
    	panel.add(four);
    	three.addActionListener(listener);
    	panel.add(three);
    	two.addActionListener(listener);
    	panel.add(two);
    	one.addActionListener(listener);
    	panel.add(one);
    	zero.addActionListener(listener);
    	panel.add(zero);
    	radixpoint.addActionListener(listener);
    	panel.add(radixpoint);
    	plus.addActionListener(listener);
    	panel.add(plus);
    	subtract.addActionListener(listener);
    	panel.add(subtract);
    	multiply.addActionListener(listener);
    	panel.add(multiply);
    	divide.addActionListener(listener);
    	panel.add(divide);
    	equal.addActionListener(listener);
    	panel.add(equal);
    	window.setVisible(true);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does my code throw an exception when there aren't square brackets around the delimiter for the String split method?

    the code threw an exception
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Is your question about how to write a regular expression to use with the split() method?
    Can you write a short, simple program for testing that shows the problem? Provide data as needed in the test program.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code throw an exception when there aren't square brackets around the delimiter for the String split method?

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Is your question about how to write a regular expression to use with the split() method?
    Can you write a short, simple program for testing that shows the problem? Provide data as needed in the test program.
    This is the full error message:

    Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
    +-x/doubleintarclg
    ^
    	at java.util.regex.Pattern.error(Pattern.java:1955)
    	at java.util.regex.Pattern.sequence(Pattern.java:2123)
    	at java.util.regex.Pattern.expr(Pattern.java:1996)
    	at java.util.regex.Pattern.compile(Pattern.java:1696)
    	at java.util.regex.Pattern.<init>(Pattern.java:1351)
    	at java.util.regex.Pattern.compile(Pattern.java:1028)
    	at java.lang.String.split(String.java:2367)
    	at CalculatorCopy$1Listener.actionPerformed(CalculatorCopy.java:125)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6525)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6290)
    	at java.awt.Container.processEvent(Container.java:2234)
    	at java.awt.Component.dispatchEventImpl(Component.java:4881)
    	at java.awt.Container.dispatchEventImpl(Container.java:2292)
    	at java.awt.Component.dispatchEvent(Component.java:4703)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    	at java.awt.Container.dispatchEventImpl(Container.java:2278)
    	at java.awt.Window.dispatchEventImpl(Window.java:2739)
    	at java.awt.Component.dispatchEvent(Component.java:4703)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    	at java.awt.EventQueue.access$400(EventQueue.java:97)
    	at java.awt.EventQueue$3.run(EventQueue.java:697)
    	at java.awt.EventQueue$3.run(EventQueue.java:691)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    	at java.awt.EventQueue$4.run(EventQueue.java:719)
    	at java.awt.EventQueue$4.run(EventQueue.java:717)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does my code throw an exception when there aren't square brackets around the delimiter for the String split method?

    Can you make a short program that shows the problem? Just a few Strings, a call to split() and a call to the Arrays class's toString() method to show the contents of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code throw an exception when there aren't square brackets around the delimiter for the String split method?

    Quote Originally Posted by Norm View Post
    Can you make a short program that shows the problem? Just a few Strings, a call to split() and a call to the Arrays class's toString() method to show the contents of the array.
    Here is the program:

    import java.util.Arrays;
    public class Program  {
    	public static void main(String[] args) {
    		String str = "22-22";
    		String str2 = "33?33";
    		String str3 = "12,12";
    		String[] array = str.split("[-]", 0);
    		String[] array2 = str2.split("?", 0);
    		String[] array3 = str3.split("[,]", 0);
    String astring = Arrays.toString(array);
    String astring2 = Arrays.toString(array2);
    String astring3 = Arrays.toString(array3);
    System.out.println(astring);
    System.out.println(astring2);
    System.out.println(astring3);
    	}
    }

    This exception appears when the program is run, and it refers the the line of code that defines an array with square brackets around its delimiter:

    Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0
    ?
    ^
    	at java.util.regex.Pattern.error(Pattern.java:1955)
    	at java.util.regex.Pattern.sequence(Pattern.java:2123)
    	at java.util.regex.Pattern.expr(Pattern.java:1996)
    	at java.util.regex.Pattern.compile(Pattern.java:1696)
    	at java.util.regex.Pattern.<init>(Pattern.java:1351)
    	at java.util.regex.Pattern.compile(Pattern.java:1028)
    	at java.lang.String.split(String.java:2367)
    	at Program.main(Program.java:8)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does my code throw an exception when there aren't square brackets around the delimiter for the String split method?

    Dangling meta character '?' near index 0
    ?
    ^
    refers the the line of code that defines an array with square brackets around its delimiter:
    I don't see that from the error message. The message refers to the pattern with the ?, not the []s

    at Program.main(Program.java:8)
    What statement is at line 8?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code throw an exception when there aren't square brackets around the delimiter for the String split method?

    The statement at line 8 is:

    String[] array2 = str2.split("?", 0);

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does my code throw an exception when there aren't square brackets around the delimiter for the String split method?

    Regular expressions have some meta characters that need to be escaped if you want to use the character as is, not as instructions for the regexp. ? is one of them.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    CodeBro (September 29th, 2014)

Similar Threads

  1. [SOLVED] Problem with string split method to string array
    By Helplessdrowningpuppy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2014, 04:16 PM
  2. Throw exception in repository, catch in UI
    By Undefined in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2013, 06:44 AM
  3. [SOLVED] how could I throw an exception in this case
    By mia_tech in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 2nd, 2012, 09:14 PM
  4. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  5. [SOLVED] why casting int to String is not possible through brackets method
    By voltaire in forum Java Theory & Questions
    Replies: 2
    Last Post: May 2nd, 2010, 04:00 PM

Tags for this Thread