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

Thread: Java code math problem to find missing paranthesis

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java code math problem to find missing paranthesis

    What have you tried?
    Be sure to wrap any posted code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java code math problem to find missing paranthesis

    Quote Originally Posted by Norm View Post
    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
    Last edited by JadenCCW; February 4th, 2018 at 10:11 PM.

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Java code math problem to find missing paranthesis

    Problem solved ?
    Whatever you are, be a good one

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] math problem in java...
    By weirddan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 17th, 2014, 03:53 PM
  2. I cannot find the problem with this code.
    By LiveH in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 11th, 2013, 10:53 PM
  3. i need help writing a code for a math problem
    By djjava in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 6th, 2013, 01:13 PM
  4. Help With Putting This Math Problem Into Code
    By Pettsa in forum Object Oriented Programming
    Replies: 23
    Last Post: May 28th, 2012, 06:48 PM
  5. Basic Math Expression Java Problem
    By andyluvskrissy in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 15th, 2011, 03:22 AM

Tags for this Thread