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 9186147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:27:10+00:00 2026-06-17T19:27:10+00:00

I’m trying to read a math expression like 3+9-2*10/5 from JOptionPane and get its

  • 0

I’m trying to read a math expression like 3+9-2*10/5 from JOptionPane and get its result — taking into account order of operations, of course. I split up the string into just digits and just operands using String.split() and created a for loop that is looking for either multiplication signs or division signs — in this case it is detecting the string “*” since it comes up first in the string.

public static void main(String[] args)
{
    String mathString = JOptionPane.showInputDialog("Please type a simple math expression (i.e., without parentheses).");

    String[] parsedIntegers = mathString.split("\\D");
    String[] parsedOperands = mathString.split("\\d+");
    parsedOperands[0] = null;
    System.out.println(Arrays.toString(parsedIntegers));
    System.out.println(Arrays.toString(parsedOperands));

      for (int index = 1; index <= parsedOperands.length; index = index + 1)
      {

          if (parsedOperands[index].equals("*"))
           {
                 System.out.println("The multiplication sign is at index " + index + ".");
                 int multResult = Character.getNumericValue(parsedIntegers[index - 1].charAt(index - 1)) * Character.getNumericValue(parsedIntegers[index].charAt(index));
                 System.out.println(multResult);
           }
      }
}

The string array parsedOperands looks like this: [null, +, -, *, /].
The string array parsedIntegers looks like this: [3, 9, 2, 10, 5].

However, when I look for “*” which is at index 3 in parsedOperands and then try to multiply what is in (index – 1) and (index) in parsedIntegers, Java returns an IndexOutOfBoundsException. Why does this happen? Am I missing something?

Here is the error:

[3, 9, 2, 10, 5]

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2

[null, +, -, *, /]

The multiplication sign is at index 3.
    at java.lang.String.charAt(String.java:658)
    at programmingpractice.SolveMathExpression.main(SolveMathExpression.java:49)
Java Result: 1
  • 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-17T19:27:11+00:00Added an answer on June 17, 2026 at 7:27 pm

    Each element in your array parsedIntegers is a single-character string, so when you use charAt, it should just be charAt(0):

    int multResult = Character.getNumericValue(parsedIntegers[index - 1].charAt(0)) *
                     Character.getNumericValue(parsedIntegers[index].charAt(0));
    

    Using charAt(index) or charAt(index - 1) there tries to read past the end of the one-character string and throws the StringIndexOutOfBoundsException you got.

    However, a more robust way to do this is probably to use Integer.parseInt so you can have multi-digit integers:

    int multResult = Integer.parseInt(parsedIntegers[index - 1]) *
                     Integer.parseInt(parsedIntegers[index]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.