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

  • Home
  • SEARCH
  • 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 8075623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:03:10+00:00 2026-06-05T15:03:10+00:00

I have a class called: ComplexNumber and I have a string that I need

  • 0

I have a class called: ComplexNumber and I have a string that I need to convert into a ComplexNumber (Using Java).

If I have: “5+3i” or “6-2i”, how do I appropriately parse these strings. I need to get it to 2 variables and I can do the rest.

String complexNum = "5+3i"; 

I would need to split the previous string into two double type variables
double real = 5;
double imag = 3;

String complexNum = "6-2i";

I would need to split the previous string into two double type variables
double real = 6;
double imag = -2;

Can anyone give example code as to how they would go about doing this? There aren’t any spaces to use as delimeters and I don’t completely understand regular expressions (i’ve read a bunch of tutorials but it still doesn’t click)


EDIT:

If regex is the best bet, i just have a hard time understanding how to create an appropriate expression.

I have the following code prepared:

String num = "5+2i";
String[] splitNum = num.split();

And i’m trying to figure out how to write the appropriate regex.

  • 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-06-05T15:03:11+00:00Added an answer on June 5, 2026 at 3:03 pm

    Alternative 1

    How about somewhat like this?

    String complexNum = "5+3i"; 
    String regex = "(\\d+)[+-](\\d+)i";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(complexNum);
    
    if(matcher.find()){
       int real = Integer.parseInt(matcher.group(1));
       int imag = Integer.parseInt(matcher.group(2));
    }
    

    If you need to make the sign part of the number, then change the regular expression to

    String regex = "(\\d+)([+-]\\d+)i"
    

    This will make the sign part of the second matching group.

    Alternative 2

    Alternatively, if you are positively sure that the string is properly formatted and you do not care about the sing of the imaginary part, you could do something like this:

    Scanner sc = new Scanner(complexNum).useDelimiter("[i+-]");
    int real = sc.nextInt();
    int imag = sc.nextInt();
    

    Which is simpler.

    Alternative 3

    And if you’re not sure of the format of the string, you can still use the regex to validate it:

    if(complexNum.matches("(\\d+)[+-](\\d+)i")) {
      //scanner code here
    } else {
       //throw exception or handle the case
    }
    

    Alternative 4

    String[] tokens = complexNum.split("[i+-]");
    int real = Integer.parseInt(tokens[0]);
    int imag = Integer.parseInt(tokens[1]);
    System.out.println(real +  " " + imag);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Partner, that I need to Submit all the info
I have a class called UserInfo that contains details about a given user. There
I have a class called Question that has a property called Type. Based on
I have a class called Sprite, and ballSprite is an instance of that class.
I have a class called Image and a class that extends it called ImageSheet
I have a class called XMLtoXML.java and this is one of it's methods... import
I have a class of buttons called Keys.java which returns a panel of buttons
I have a class called GraphView that extends UIView that basically draws a small
I have a class called PlayerList, in in that class i have an ArrayList
I have a class called LocalConstants.... public static class LocalConstants { public static string

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.