Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4232390
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:05:18+00:00 2026-05-21T02:05:18+00:00

I have the following content in a file that I need to parse. ((-2,

  • 0

I have the following content in a file that I need to parse.

((-2, -1 ), ( 4, 2) ) ((-1.2, 0), (0, 0)) 
((0, 0), (10, -1)) 
((5, 3), (5, 4)) ((5, 1) , (5, 5)) 
((8, 3), (10, 3)) 
((8, 5), (11.5, 5)) 

So far, I have scanned through the file and taken the input line by line, saving them as strings, so one string would be ((-2, -1 ), ( 4, 2) ) ((-1.2, 0), (0, 0)). My question is where to go from here. How do I extract the doubles from this String. I have tried to use the parentheses as a delimiter, as well as changing all of the parentheses to commas, and then using a comma as a delimiter, but both of these ways give me errors. Any ideas?

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 1 at java.util.regex.Pattern.error(Unknown Source) at
    java.util.regex.Pattern.accept(Unknown Source) at
    java.util.regex.Pattern.group0(Unknown Source) at
    java.util.regex.Pattern.sequence(Unknown Source) at
    java.util.regex.Pattern.expr(Unknown Source) at
    java.util.regex.Pattern.compile(Unknown Source) at
    java.util.regex.Pattern.<init>(Unknown Source) at
    java.util.regex.Pattern.compile(Unknown Source) at
    java.lang.String.replaceAll(Unknown Source)

The whole purpose of this is to create points from these doubles. For example, ((0, 0), (10, -1)) would create two points, (0,0) and (10, -1). I have created the point class and it takes in two doubles in the constructor.
Here is what I tried:

      String toParse = "((0, 0), (10, -1))";
    toParse.replaceAll("(", ",");
    toParse.replaceAll(")", ",");
    Scanner stringScanner = new Scanner(toParse);
    stringScanner.useDelimiter(",");
    while(stringScanner.hasNextDouble()){

ect

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:05:18+00:00Added an answer on May 21, 2026 at 2:05 am

    The error message you get is because ( and ) are special regex characters: so whenever you want to replace (or match) them, you need to escape them with a backslash:

    toParse.replaceAll("\\(", ",");
    ...
    

    That said, you might want to use a Pattern/Matcher approach here:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Test {
        public static void main(String[] args) {
    
            String source =
                    "((-2, -1 ), ( 4, 2) ) ((-1.2, 0), (0, 0))  \n" +
                    "((0, 0), (10, -1))                         \n" +
                    "((5, 3), (5, 4)) ((5, 1) , (5, 5))         \n" +
                    "((8, 3), (10, 3))                          \n" +
                    "((8, 5), (11.5, 5))                        \n";
    
            Matcher m = Pattern.compile("-?\\d+(\\.\\d+)?").matcher(source);
    
            while(m.find()) {
                double value = Double.parseDouble(m.group());
                System.out.println("value=" + value);
            }
        }
    }
    

    which produces:

    value=-2.0
    value=-1.0
    value=4.0
    value=2.0
    value=-1.2
    value=0.0
    value=0.0
    value=0.0
    value=0.0
    value=0.0
    value=10.0
    value=-1.0
    value=5.0
    value=3.0
    value=5.0
    value=4.0
    value=5.0
    value=1.0
    value=5.0
    value=5.0
    value=8.0
    value=3.0
    value=10.0
    value=3.0
    value=8.0
    value=5.0
    value=11.5
    value=5.0
    

    What the code does: it searches for the pattern -?\d+(\.\d+)? and parses each string that matches said pattern to a double. The pattern itself means:

    -?         # an optional minus sign
    \d+        # followed by one or more digits
    (\.\d+)?   # followed by an optional decimal DOT with one or more digits
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple text file, that has following content word1 word2 I need
I have a file containing the following content 1000 line in the following format:
I need to process an input file, and copy its content (line by ilne)
For example I have file config.xml in that file I have following xml content
I have a PHP script that does the following: Uses file_get_contents() to get content
I have a file ( myenv.sh ) with the following content export MYVAR=HELLO And
I have an .htaccess file with following content: AddHandler x-httpd-php5 .php For some reason,
I have the following contents in my XML file: <items> <item id=1><content><![CDATA[<p>string</p>]]></content></item> </items> I
I have a xml file main.xml with following markup and data. main.xml <xml> <content>
I have an application that creates an audio file. I need to provide a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.