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

# Thread: Java code math problem to find missing paranthesis

1. ## Java code math problem to find missing paranthesis

Hi, I need help for with full java code for the following Problem:
```Problem: Given string data representing a mathematical expression, determine the possible location(s) of the missing parenthesis. Although we show spaces between characters for readability the input strings have no spaces. All operands will be single digits.

Given (2+3*6+1. A right parenthesis is missing. It could be correctly places in several locations.

(2+3)*6+1 Location 5
(2+3*6)+1 Location 7
(2+3*6+1) Location 9

Input: There will be five lines of input. Each line will contain a string of characters with no spaces representing a mathematical expression. Each expression will have either a single left or right parenthesis. The operators used will be: +, -, * and /.

Output: For each line of input, list all the locations in that expression where the missing left or right parenthesis can be correctly placed. Note: Single digits are never enclosed.

Sample Input         Sample Output
1. (2+3*6+1           1. 5,7.9
2. 2-5*(6+1           2. 9
3. 5+5-2)*5           3. 1,3
4. 3*5 + (8/4*2       4. 9,11
5. 2+8/4*5)           5. 1,3,5```  Reply With Quote

2. ## Re: Java code math problem to find missing paranthesis

What have you tried?
Be sure to wrap any posted code in code tags.  Reply With Quote

3. ## Re: Java code math problem to find missing paranthesis Originally Posted by Norm What have you tried?
Be sure to wrap any posted code in code tags.

```package ACSL2017_2018;

import java.util.*;
import java.io.*;

public class C2_ACSLEnclosure {

public static void main(String[] args) throws IOException{
Scanner in = new Scanner(new File("1718c2.in"));

for(int i = 0; i < 5; i++){
String result = "";
String expression = in.next();
String p1 = "(";
String p2 = ")";
int n = expression.indexOf(p1)+5;
int p = expression.indexOf(p2)-4;

//if(expression.contains(p1)){
//result += Integer.toString(expression.indexOf(p1)+5);
//}
if(expression.contains(p1))
{
result = n+" ";
for(int k=n+2 ; k<=expression.length() + 1; k++){
result += k  +" " ;
}
/*for(int k = n; k < expression.length()-1; k+=2){
if(expression.contains(p1))
result += ", " + Integer.toString(n+2);

}*/
//System.out.println(expression);

/*if(expression.contains(p2)){
result += Integer.toString(expression.indexOf(p2)-4);
}
for(int e = p; e > expression.length()-1; e+=2){
if(expression.contains(p2))
result += ", " + Integer.toString(p - 2);

}*/

if(expression.contains(p2))
{
result = p+" " ;
for(int k=p-2 ; k<=expression.length() + 1; k++){
result += k  +" " ;
}

System.out.println(result);

}

}
}
}}```

i don't get whats wrong with it

--- Update ---

```package ACSL2017_2018;

import java.util.*;
import java.io.*;

public class C2_ACSLEnclosure {

public static void main(String[] args) throws IOException{
Scanner in = new Scanner(new File("1718c2.in"));

for(int i = 0; i < 5; i++){
String result = "";
String expression = in.next();
String p1 = "(";
String p2 = ")";
int n = expression.indexOf(p1)+5;
int p = expression.indexOf(p2)-4;

//if(expression.contains(p1)){
//result += Integer.toString(expression.indexOf(p1)+5);
//}
if(expression.contains(p1))
{
//result = n+" ";
for(int k=n ; k<=expression.length() + 1; k+=2){
result += k  +" " ;
}
}
/*for(int k = n; k < expression.length()-1; k+=2){
if(expression.contains(p1))
result += ", " + Integer.toString(n+2);

}*/
//System.out.println(expression);

/*if(expression.contains(p2)){
result += Integer.toString(expression.indexOf(p2)-4);
}
for(int e = p; e > expression.length()-1; e+=2){
if(expression.contains(p2))
result += ", " + Integer.toString(p - 2);

}*/

if(expression.contains(p2))
{
result = p+" " ;
for(int k=p ; k>=expression.length() + 1; k-=2){
result += k  +" " ;
}

}
System.out.println(result);

}
}
}```

fixed my code more  Reply With Quote

4. ## Re: Java code math problem to find missing paranthesis

Problem solved ?  Reply With Quote

5. ## Re: Java code math problem to find missing paranthesis

i don't get whats wrong with it
Please explain why you think there is something wrong.
Post any output that shows what you are talking about and add some comments explaining what is wrong.  Reply With Quote

#### Tags for this Thread 